OCRC_STO.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OCRC_STO.H
  21. // Description : store of crc of objects
  22. #include <ONATIONA.h>
  23. #include <OUNIT.h>
  24. #include <OFIRMA.h>
  25. #include <OTOWN.h>
  26. #include <OBULLET.h>
  27. #include <OREBEL.h>
  28. #include <OSPY.h>
  29. #include <OTALKRES.h>
  30. #include <OREMOTE.h>
  31. #include <OCRC_STO.h>
  32. #include <CRC.h>
  33. CrcStore::CrcStore() :
  34. nations(0), units(0), firms(0), towns(0), bullets(0), rebels(0), spies(0), talk_msgs(0)
  35. {
  36. }
  37. void CrcStore::init()
  38. {
  39. deinit();
  40. }
  41. void CrcStore::deinit()
  42. {
  43. nations.clear();
  44. units.clear();
  45. firms.clear();
  46. towns.clear();
  47. bullets.clear();
  48. rebels.clear();
  49. spies.clear();
  50. talk_msgs.clear();
  51. }
  52. void CrcStore::record_nations()
  53. {
  54. nations.clear();
  55. *(short *)nations.reserve(sizeof(short)) = nation_array.size();
  56. for( short nationRecno = 1; nationRecno <= nation_array.size(); ++nationRecno)
  57. {
  58. CRC_TYPE checkNum = 0;
  59. if( !nation_array.is_deleted(nationRecno) )
  60. checkNum = nation_array[nationRecno]->crc8();
  61. *(CRC_TYPE *)nations.reserve(sizeof(CRC_TYPE)) = checkNum;
  62. }
  63. }
  64. void CrcStore::record_units()
  65. {
  66. units.clear();
  67. *(short *)units.reserve(sizeof(short)) = unit_array.size();
  68. for( short unitRecno = 1; unitRecno <= unit_array.size(); ++unitRecno)
  69. {
  70. CRC_TYPE checkNum = 0;
  71. if( !unit_array.is_deleted(unitRecno) )
  72. checkNum = unit_array[unitRecno]->crc8();
  73. *(CRC_TYPE *)units.reserve(sizeof(CRC_TYPE)) = checkNum;
  74. }
  75. }
  76. void CrcStore::record_firms()
  77. {
  78. firms.clear();
  79. *(short *)firms.reserve(sizeof(short)) = firm_array.size();
  80. for( short firmRecno = 1; firmRecno <= firm_array.size(); ++firmRecno)
  81. {
  82. CRC_TYPE checkNum = 0;
  83. if( !firm_array.is_deleted(firmRecno) )
  84. checkNum = firm_array[firmRecno]->crc8();
  85. *(CRC_TYPE *)firms.reserve(sizeof(CRC_TYPE)) = checkNum;
  86. }
  87. }
  88. void CrcStore::record_towns()
  89. {
  90. towns.clear();
  91. *(short *)towns.reserve(sizeof(short)) = town_array.size();
  92. for( short townRecno = 1; townRecno <= town_array.size(); ++townRecno)
  93. {
  94. CRC_TYPE checkNum = 0;
  95. if( !town_array.is_deleted(townRecno) )
  96. checkNum = town_array[townRecno]->crc8();
  97. *(CRC_TYPE *)towns.reserve(sizeof(CRC_TYPE)) = checkNum;
  98. }
  99. }
  100. void CrcStore::record_bullets()
  101. {
  102. bullets.clear();
  103. *(short *)bullets.reserve(sizeof(short)) = bullet_array.size();
  104. for( short bulletRecno = 1; bulletRecno <= bullet_array.size(); ++bulletRecno)
  105. {
  106. CRC_TYPE checkNum = 0;
  107. if( !bullet_array.is_deleted(bulletRecno) )
  108. checkNum = bullet_array[bulletRecno]->crc8();
  109. *(CRC_TYPE *)bullets.reserve(sizeof(CRC_TYPE)) = checkNum;
  110. }
  111. }
  112. void CrcStore::record_rebels()
  113. {
  114. rebels.clear();
  115. *(short *)rebels.reserve(sizeof(short)) = rebel_array.size();
  116. for( short rebelRecno = 1; rebelRecno <= rebel_array.size(); ++rebelRecno)
  117. {
  118. CRC_TYPE checkNum = 0;
  119. if( !rebel_array.is_deleted(rebelRecno) )
  120. {
  121. checkNum = rebel_array[rebelRecno]->crc8();
  122. }
  123. *(CRC_TYPE *)rebels.reserve(sizeof(CRC_TYPE)) = checkNum;
  124. }
  125. }
  126. void CrcStore::record_spies()
  127. {
  128. spies.clear();
  129. *(short *)spies.reserve(sizeof(short)) = spy_array.size();
  130. for( short spyRecno = 1; spyRecno <= spy_array.size(); ++spyRecno)
  131. {
  132. CRC_TYPE checkNum = 0;
  133. if( !spy_array.is_deleted(spyRecno) )
  134. {
  135. checkNum = spy_array[spyRecno]->crc8();
  136. }
  137. *(CRC_TYPE *)spies.reserve(sizeof(CRC_TYPE)) = checkNum;
  138. }
  139. }
  140. void CrcStore::record_talk_msgs()
  141. {
  142. talk_msgs.clear();
  143. *(short *)talk_msgs.reserve(sizeof(short)) = talk_res.talk_msg_count();
  144. for( short talkRecno = 1; talkRecno <= talk_res.talk_msg_count(); ++talkRecno)
  145. {
  146. CRC_TYPE checkNum = 0;
  147. if( !talk_res.is_talk_msg_deleted(talkRecno) )
  148. {
  149. checkNum = talk_res.get_talk_msg(talkRecno)->crc8();
  150. }
  151. *(CRC_TYPE *)talk_msgs.reserve(sizeof(CRC_TYPE)) = checkNum;
  152. }
  153. }
  154. void CrcStore::record_all()
  155. {
  156. record_nations();
  157. record_units();
  158. record_firms();
  159. record_towns();
  160. record_bullets();
  161. record_rebels();
  162. record_spies();
  163. record_talk_msgs();
  164. }
  165. void CrcStore::send_all()
  166. {
  167. char *charPtr;
  168. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_NATION, nations.length() );
  169. memcpy(charPtr, nations.queue_buf, nations.length() );
  170. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_UNIT, units.length() );
  171. memcpy(charPtr, units.queue_buf, units.length() );
  172. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_FIRM, firms.length() );
  173. memcpy(charPtr, firms.queue_buf, firms.length() );
  174. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_TOWN, towns.length() );
  175. memcpy(charPtr, towns.queue_buf, towns.length() );
  176. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_BULLET, bullets.length() );
  177. memcpy(charPtr, bullets.queue_buf, bullets.length() );
  178. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_REBEL, rebels.length() );
  179. memcpy(charPtr, rebels.queue_buf, rebels.length() );
  180. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_SPY, spies.length() );
  181. memcpy(charPtr, spies.queue_buf, spies.length() );
  182. charPtr = (char *)remote.new_send_queue_msg(MSG_COMPARE_TALK, talk_msgs.length() );
  183. memcpy(charPtr, talk_msgs.queue_buf, talk_msgs.length() );
  184. }
  185. // return 0 if equal
  186. // otherwise not equal
  187. int CrcStore::compare_remote(DWORD remoteMsgId, char *dataPtr)
  188. {
  189. VLenQueue *vq = NULL;
  190. char *arrayName;
  191. switch(remoteMsgId)
  192. {
  193. case MSG_COMPARE_NATION:
  194. vq = &nations;
  195. arrayName = "nation_array";
  196. break;
  197. case MSG_COMPARE_UNIT:
  198. vq = &units;
  199. arrayName = "unit_array";
  200. break;
  201. case MSG_COMPARE_FIRM:
  202. vq = &firms;
  203. arrayName = "firm_array";
  204. break;
  205. case MSG_COMPARE_TOWN:
  206. vq = &towns;
  207. arrayName = "town_array";
  208. break;
  209. case MSG_COMPARE_BULLET:
  210. vq = &bullets;
  211. arrayName = "bullet_array";
  212. break;
  213. case MSG_COMPARE_REBEL:
  214. vq = &rebels;
  215. arrayName = "rebel_array";
  216. break;
  217. case MSG_COMPARE_SPY:
  218. vq = &spies;
  219. arrayName = "spy_array";
  220. break;
  221. case MSG_COMPARE_TALK:
  222. vq = &talk_msgs;
  223. arrayName = "talk_res";
  224. break;
  225. default:
  226. err_here();
  227. return 0;
  228. }
  229. err_when(vq->length() < sizeof(short));
  230. int rc = memcmp(vq->queue_buf, dataPtr, vq->length() );
  231. if( rc )
  232. {
  233. char *p1;
  234. char *p2;
  235. int diffOffset;
  236. // found out where is the first difference
  237. for( diffOffset = 0, p1 = vq->queue_buf, p2 = dataPtr;
  238. diffOffset < vq->length() && *p1 == *p2;
  239. ++diffOffset, ++p1, ++p2);
  240. // ###### patch begin Gilbert 23/1 #######//
  241. // err.run("%s discrepency, offset : %d", arrayName, diffOffset);
  242. crc_error_string = arrayName;
  243. crc_error_string += "discrepency, offset : ";
  244. crc_error_string += diffOffset;
  245. // ###### patch end Gilbert 23/1 #######//
  246. diffOffset = 0; // dummy code
  247. }
  248. return rc;
  249. }