OU_MONS.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 : OU_MONS.CPP
  21. //Description: Unit Monster
  22. #include <OWORLD.h>
  23. #include <OSITE.h>
  24. #include <OF_MONS.h>
  25. #include <ONEWS.h>
  26. #include <OTOWN.h>
  27. #include <OMONSRES.h>
  28. #include <OU_MONS.h>
  29. //--------- Begin of function UnitMonster::UnitMonster --------//
  30. UnitMonster::UnitMonster()
  31. {
  32. monster_action_mode = MONSTER_ACTION_STOP;
  33. }
  34. //---------- End of function UnitMonster::UnitMonster --------//
  35. //--------- Begin of function UnitMonster::set_monster_action_mode --------//
  36. void UnitMonster::set_monster_action_mode(char monsterActionMode)
  37. {
  38. monster_action_mode = monsterActionMode;
  39. }
  40. //---------- End of function UnitMonster::set_monster_action_mode --------//
  41. //--------- Begin of function UnitMonster::unit_name ---------//
  42. //
  43. // [int] withTitle - whether return a string with the title of the unit
  44. // or not. (default: 1)
  45. //
  46. char* UnitMonster::unit_name(int withTitle)
  47. {
  48. static String str;
  49. char* monsterName = monster_res[get_monster_id()]->name; // contribution is used for storing the monster id. temporary
  50. str = monsterName;
  51. switch( rank_id )
  52. {
  53. case RANK_KING:
  54. #if(defined(SPANISH))
  55. str = "Gran Todo ";
  56. str += monsterName;
  57. #elif(defined(FRENCH))
  58. str = "Tout Puissant ";
  59. str += monsterName;
  60. #elif(defined(GERMAN))
  61. str = "Hochkönig ";
  62. str += monsterName;
  63. #else
  64. // GERMAN US
  65. str = "All High ";
  66. str += monsterName;
  67. #endif
  68. break;
  69. case RANK_GENERAL:
  70. #if(defined(SPANISH) || defined(FRENCH))
  71. str = "Ordo ";
  72. str += monsterName;
  73. #else
  74. // GERMAN US
  75. str += " Ordo";
  76. #endif
  77. break;
  78. }
  79. return str;
  80. }
  81. //--------- End of function UnitMonster::unit_name ---------//
  82. //--------- Begin of function UnitMonster::process_ai --------//
  83. //
  84. void UnitMonster::process_ai()
  85. {
  86. //----- when it is idle -------//
  87. if( !is_visible() || !is_ai_all_stop() )
  88. return;
  89. if( info.game_date%15 == sprite_recno%15 )
  90. {
  91. random_attack(); // randomly attacking targets
  92. /*
  93. switch(m.random(2))
  94. {
  95. case 0:
  96. random_attack(); // randomly attacking targets
  97. break;
  98. case 1:
  99. assign_to_firm(); // assign the monsters to other monster structures
  100. break;
  101. }
  102. */
  103. }
  104. }
  105. //---------- End of function UnitMonster::process_ai --------//
  106. //------- Begin of function UnitMonster::die -------//
  107. //
  108. void UnitMonster::die()
  109. {
  110. if( !is_visible() )
  111. return;
  112. //--- check if the location where the unit dies already has an item ---//
  113. int xLoc = cur_x_loc();
  114. int yLoc = cur_y_loc();
  115. if( !world.get_loc(xLoc, yLoc)->can_build_site() )
  116. {
  117. int txLoc, tyLoc, foundFlag=0;
  118. for( tyLoc=max(yLoc-1,0) ; tyLoc<=min(yLoc+1,MAX_WORLD_Y_LOC-1) && !foundFlag ; tyLoc++ )
  119. {
  120. for( txLoc=max(xLoc-1,0) ; txLoc<=min(xLoc+1,MAX_WORLD_X_LOC-1) ; txLoc++ )
  121. {
  122. if( world.get_loc(txLoc,tyLoc)->can_build_site() )
  123. {
  124. xLoc = txLoc;
  125. yLoc = tyLoc;
  126. foundFlag = 1;
  127. break;
  128. }
  129. }
  130. }
  131. if( !foundFlag )
  132. return;
  133. }
  134. //--- when a general monster is killed, it leaves gold coins ---//
  135. if( !nation_recno && get_monster_id() != 0) // to skip monster_res[ get_monster_id() ] error in test game 2
  136. {
  137. MonsterInfo* monsterInfo = monster_res[ get_monster_id() ];
  138. if( rank_id == RANK_GENERAL )
  139. {
  140. int goldAmount = 2 * max_hit_points * monsterInfo->level * (100+m.random(30)) / 100;
  141. site_array.add_site( xLoc, yLoc, SITE_GOLD_COIN, goldAmount );
  142. site_array.ai_get_site_object(); // ask AI units to get the gold coins
  143. }
  144. //--- when a king monster is killed, it leaves a scroll of power ---//
  145. else if( rank_id == RANK_KING )
  146. {
  147. king_leave_scroll();
  148. }
  149. }
  150. //---------- add news ----------//
  151. if( rank_id == RANK_KING )
  152. news_array.monster_king_killed( get_monster_id(), next_x_loc(), next_y_loc() );
  153. }
  154. //-------- End of function UnitMonster::die -------//
  155. //------- Begin of function UnitMonster::king_leave_scroll -------//
  156. //
  157. void UnitMonster::king_leave_scroll()
  158. {
  159. #define SCROLL_SCAN_RANGE 10
  160. int xOffset, yOffset;
  161. int xLoc, yLoc;
  162. Location* locPtr;
  163. int curXLoc = next_x_loc(), curYLoc = next_y_loc();
  164. BYTE regionId = world.get_region_id(curXLoc, curYLoc);
  165. short raceCountArray[MAX_RACE];
  166. memset( raceCountArray, 0, sizeof(raceCountArray) );
  167. int i;
  168. for( i=2 ; i<SCROLL_SCAN_RANGE*SCROLL_SCAN_RANGE ; i++ )
  169. {
  170. m.cal_move_around_a_point(i, SCROLL_SCAN_RANGE, SCROLL_SCAN_RANGE, xOffset, yOffset);
  171. xLoc = curXLoc + xOffset;
  172. yLoc = curYLoc + yOffset;
  173. xLoc = max(0, xLoc);
  174. xLoc = min(MAX_WORLD_X_LOC-1, xLoc);
  175. yLoc = max(0, yLoc);
  176. yLoc = min(MAX_WORLD_Y_LOC-1, yLoc);
  177. locPtr = world.get_loc(xLoc, yLoc);
  178. //----- if there is a unit on the location ------//
  179. if( locPtr->has_unit(UNIT_LAND) )
  180. {
  181. Unit* unitPtr = unit_array[locPtr->unit_recno(UNIT_LAND)];
  182. raceCountArray[ unitPtr->race_id-1 ]++;
  183. }
  184. }
  185. //------ find out which race is most populated in the area -----//
  186. int maxRaceCount=0, bestRaceId=0;
  187. for( i=0 ; i<MAX_RACE ; i++ )
  188. {
  189. if( raceCountArray[i] > maxRaceCount )
  190. {
  191. maxRaceCount = raceCountArray[i];
  192. bestRaceId = i+1;
  193. }
  194. }
  195. if( !bestRaceId )
  196. bestRaceId = m.random(MAX_RACE)+1; // if there is no human units nearby (perhaps just using weapons)
  197. //------ locate for space to add the scroll -------//
  198. #define ADD_SITE_RANGE 5
  199. for( i=1 ; i<ADD_SITE_RANGE*ADD_SITE_RANGE ; i++ )
  200. {
  201. m.cal_move_around_a_point(i, ADD_SITE_RANGE, ADD_SITE_RANGE, xOffset, yOffset);
  202. xLoc = curXLoc + xOffset;
  203. yLoc = curYLoc + yOffset;
  204. xLoc = max(0, xLoc);
  205. xLoc = min(MAX_WORLD_X_LOC-1, xLoc);
  206. yLoc = max(0, yLoc);
  207. yLoc = min(MAX_WORLD_Y_LOC-1, yLoc);
  208. locPtr = world.get_loc(xLoc, yLoc);
  209. if( locPtr->can_build_site() && locPtr->region_id==regionId )
  210. {
  211. int scrollGodId = bestRaceId;
  212. site_array.add_site( xLoc, yLoc, SITE_SCROLL, scrollGodId );
  213. site_array.ai_get_site_object(); // ask AI units to get the scroll
  214. break;
  215. }
  216. }
  217. }
  218. //-------- End of function UnitMonster::king_leave_scroll -------//
  219. //--------- Begin of function UnitMonster::random_attack --------//
  220. //
  221. // Randomly pick an object to attack.
  222. //
  223. int UnitMonster::random_attack()
  224. {
  225. #define ATTACK_SCAN_RANGE 100
  226. int xOffset, yOffset;
  227. int xLoc, yLoc;
  228. Location* locPtr;
  229. int curXLoc = next_x_loc(), curYLoc = next_y_loc();
  230. BYTE regionId = world.get_region_id(curXLoc, curYLoc);
  231. int rc;
  232. for( int i=2 ; i<ATTACK_SCAN_RANGE*ATTACK_SCAN_RANGE ; i++ )
  233. {
  234. m.cal_move_around_a_point(i, ATTACK_SCAN_RANGE, ATTACK_SCAN_RANGE, xOffset, yOffset);
  235. xLoc = curXLoc + xOffset;
  236. yLoc = curYLoc + yOffset;
  237. xLoc = max(0, xLoc);
  238. xLoc = min(MAX_WORLD_X_LOC-1, xLoc);
  239. yLoc = max(0, yLoc);
  240. yLoc = min(MAX_WORLD_Y_LOC-1, yLoc);
  241. locPtr = world.get_loc(xLoc, yLoc);
  242. if( locPtr->region_id != regionId )
  243. continue;
  244. rc = 0;
  245. //----- if there is a unit on the location ------//
  246. if( locPtr->has_unit(UNIT_LAND) )
  247. {
  248. int unitRecno = locPtr->unit_recno(UNIT_LAND);
  249. if( unit_array.is_deleted(unitRecno) )
  250. continue;
  251. rc = 1;
  252. }
  253. //----- if there is a firm on the location ------//
  254. if( !rc && locPtr->is_firm() )
  255. {
  256. int firmRecno = locPtr->firm_recno();
  257. if( firm_array.is_deleted(firmRecno) )
  258. continue;
  259. rc = 1;
  260. }
  261. //----- if there is a town on the location ------//
  262. if( !rc && locPtr->is_town() )
  263. {
  264. int townRecno = locPtr->town_recno();
  265. if( town_array.is_deleted(townRecno) )
  266. continue;
  267. rc = 1;
  268. }
  269. //-------------------------------------//
  270. if( rc )
  271. {
  272. group_order_monster(xLoc, yLoc, 1); // 1-the action is attack
  273. return 1;
  274. }
  275. }
  276. return 0;
  277. }
  278. //---------- End of function UnitMonster::random_attack --------//
  279. //--------- Begin of function UnitMonster::assign_to_firm --------//
  280. //
  281. int UnitMonster::assign_to_firm()
  282. {
  283. int i, firmCount=firm_array.size();
  284. Firm* firmPtr;
  285. int curXLoc = next_x_loc(), curYLoc = next_y_loc();
  286. BYTE regionId = world.get_region_id(curXLoc, curYLoc);
  287. int firmRecno = m.random(firm_array.size())+1;
  288. for( i=0 ; i<firmCount ; i++ )
  289. {
  290. if( ++firmRecno > firmCount )
  291. firmRecno = 1;
  292. if( firm_array.is_deleted(firmRecno) )
  293. continue;
  294. firmPtr = firm_array[firmRecno];
  295. if( firmPtr->region_id != regionId )
  296. continue;
  297. if( firmPtr->firm_id == FIRM_MONSTER )
  298. {
  299. if( ((FirmMonster*)firmPtr)->can_assign_monster(sprite_recno) )
  300. {
  301. group_order_monster(firmPtr->loc_x1, firmPtr->loc_y1, 2); // 2-the action is assign
  302. return 1;
  303. }
  304. }
  305. }
  306. return 0;
  307. }
  308. //---------- End of function UnitMonster::assign_to_firm --------//
  309. //--------- Begin of function UnitMonster::group_order_monster --------//
  310. //
  311. // <int> destXLoc, destYLoc - location of the destination.
  312. // <int> actionType - 1-attack, 2-assign.
  313. //
  314. void UnitMonster::group_order_monster(int destXLoc, int destYLoc, int actionType)
  315. {
  316. #define GROUP_ACTION_RANGE 30 // only notify units within this range
  317. #define MAX_MONSTER_GROUP_COUNT 15
  318. int curXLoc = next_x_loc(), curYLoc = next_y_loc();
  319. BYTE regionId = world.get_region_id(curXLoc, curYLoc);
  320. short unitOrderedArray[MAX_MONSTER_GROUP_COUNT];
  321. int unitOrderedCount=0;
  322. int xOffset, yOffset;
  323. int xLoc, yLoc;
  324. Location* locPtr;
  325. Unit* unitPtr;
  326. //----------------------------------------------//
  327. for( int i=1 ; i<GROUP_ACTION_RANGE*GROUP_ACTION_RANGE ; i++ )
  328. {
  329. m.cal_move_around_a_point(i, GROUP_ACTION_RANGE, GROUP_ACTION_RANGE, xOffset, yOffset);
  330. xLoc = curXLoc + xOffset;
  331. yLoc = curYLoc + yOffset;
  332. xLoc = max(0, xLoc);
  333. xLoc = min(MAX_WORLD_X_LOC-1, xLoc);
  334. yLoc = max(0, yLoc);
  335. yLoc = min(MAX_WORLD_Y_LOC-1, yLoc);
  336. locPtr = world.get_loc(xLoc, yLoc);
  337. if( !locPtr->has_unit(UNIT_LAND) )
  338. continue;
  339. //------------------------------//
  340. int unitRecno = locPtr->unit_recno(UNIT_LAND);
  341. if( unit_array.is_deleted(unitRecno) )
  342. continue;
  343. unitPtr = unit_array[unitRecno];
  344. if( unit_res[unitPtr->unit_id]->unit_class != UNIT_CLASS_MONSTER )
  345. continue;
  346. unitOrderedArray[unitOrderedCount] = unitRecno;
  347. if( ++unitOrderedCount >= MAX_MONSTER_GROUP_COUNT )
  348. break;
  349. }
  350. if( unitOrderedCount == 0 )
  351. return;
  352. //---------------------------------------//
  353. if( actionType==1 ) // attack
  354. {
  355. // ##### patch begin Gilbert 5/8 ######//
  356. unit_array.attack( destXLoc, destYLoc, 0, unitOrderedArray, unitOrderedCount, COMMAND_AI, 0 );
  357. // ##### patch end Gilbert 5/8 ######//
  358. }
  359. else
  360. {
  361. unit_array.assign( destXLoc, destYLoc, 0, COMMAND_AI, unitOrderedArray, unitOrderedCount);
  362. }
  363. }
  364. //---------- End of function UnitMonster::group_order_monster --------//