OUNITS.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 : OUNITS.CPP
  21. //Description : Object Unit(Ship) functions
  22. //Owner : Alex
  23. #include <OU_MARI.h>
  24. #include <OTERRAIN.h>
  25. #include <ONATION.h>
  26. #ifdef NO_DEBUG_UNIT
  27. #undef err_when
  28. #undef err_here
  29. #undef err_if
  30. #undef err_else
  31. #undef err_now
  32. #define err_when(cond)
  33. #define err_here()
  34. #define err_if(cond)
  35. #define err_else
  36. #define err_now(msg)
  37. #undef DEBUG
  38. #endif
  39. //--------- Begin of function Unit::assign_to_ship ---------//
  40. // assigns units to ship
  41. //
  42. // <int> destX, destY - the location for the ship to load the unit
  43. // <short> shipRecno - the recno of the ship
  44. // <int> miscNo - used to cal the offset location (default: 0)
  45. //
  46. void Unit::assign_to_ship(int destX, int destY, short shipRecno, int miscNo)
  47. {
  48. err_when(destX<0 || destX>=MAX_WORLD_X_LOC || destY<0 || destY>=MAX_WORLD_Y_LOC || shipRecno<=0);
  49. err_when(sprite_info->loc_width>1 || sprite_info->loc_height>1);
  50. //----------------------------------------------------------------//
  51. // return if the unit is dead
  52. //----------------------------------------------------------------//
  53. if(hit_points<=0 || action_mode==ACTION_DIE || cur_action==SPRITE_DIE)
  54. return;
  55. if(unit_array.is_deleted(shipRecno))
  56. return;
  57. #ifdef DEBUG
  58. int debugShipXLoc = unit_array[shipRecno]->next_x_loc();
  59. int debugShipYLoc = unit_array[shipRecno]->next_y_loc();
  60. err_when(terrain_res[world.get_loc(debugShipXLoc, debugShipYLoc)->terrain_id]->average_type!=TERRAIN_OCEAN);
  61. int debugCheckXLoc, debugCheckYLoc;
  62. #endif
  63. //----------------------------------------------------------------//
  64. // action_mode2: checking for equal action or idle action
  65. //----------------------------------------------------------------//
  66. if(action_mode2==ACTION_ASSIGN_TO_SHIP && action_para2==shipRecno && action_x_loc2==destX && action_y_loc2==destY)
  67. {
  68. if(cur_action!=SPRITE_IDLE)
  69. return;
  70. }
  71. else
  72. {
  73. //----------------------------------------------------------------//
  74. // action_mode2: store new order
  75. //----------------------------------------------------------------//
  76. action_mode2 = ACTION_ASSIGN_TO_SHIP;
  77. action_para2 = shipRecno;
  78. action_x_loc2 = destX;
  79. action_y_loc2 = destY;
  80. }
  81. //----- order the sprite to stop as soon as possible -----//
  82. stop(); // new order
  83. Unit *shipPtr = unit_array[shipRecno];
  84. int shipXLoc = shipPtr->next_x_loc();
  85. int shipYLoc = shipPtr->next_y_loc();
  86. int resultXLoc, resultYLoc;
  87. int xShift, yShift;
  88. if(!miscNo)
  89. {
  90. //-------- find a suitable location since no offset location is given ---------//
  91. if(abs(shipXLoc-action_x_loc2)<=1 && abs(shipYLoc-action_y_loc2)<=1)
  92. {
  93. int checkXLoc, checkYLoc;
  94. Location *locPtr = world.get_loc(next_x_loc(), next_y_loc());
  95. UCHAR regionId = locPtr->region_id;
  96. for(int i=2; i<=9; i++)
  97. {
  98. m.cal_move_around_a_point(i, 3, 3, xShift, yShift);
  99. checkXLoc = shipXLoc+xShift;
  100. checkYLoc = shipYLoc+yShift;
  101. if(checkXLoc<0 || checkXLoc>=MAX_WORLD_X_LOC || checkYLoc<0 || checkYLoc>=MAX_WORLD_Y_LOC)
  102. continue;
  103. locPtr = world.get_loc(checkXLoc, checkYLoc);
  104. if(locPtr->region_id!=regionId)
  105. continue;
  106. resultXLoc = checkXLoc;
  107. resultYLoc = checkYLoc;
  108. #ifdef DEBUG
  109. debugCheckXLoc = checkXLoc;
  110. debugCheckYLoc = checkYLoc;
  111. #endif
  112. break;
  113. }
  114. }
  115. else
  116. {
  117. resultXLoc = action_x_loc2;
  118. resultYLoc = action_y_loc2;
  119. }
  120. err_when(resultXLoc<0 || resultXLoc>=MAX_WORLD_X_LOC || resultYLoc<0 || resultYLoc>=MAX_WORLD_Y_LOC);
  121. }
  122. else
  123. {
  124. //------------ offset location is given, move there directly ----------//
  125. m.cal_move_around_a_point(miscNo, MAX_WORLD_X_LOC, MAX_WORLD_Y_LOC, xShift, yShift);
  126. resultXLoc = destX+xShift;
  127. resultYLoc = destY+yShift;
  128. err_when(resultXLoc<0 || resultXLoc>=MAX_WORLD_X_LOC || resultYLoc<0 || resultYLoc>=MAX_WORLD_Y_LOC);
  129. }
  130. //--------- start searching ----------//
  131. int curXLoc = next_x_loc();
  132. int curYLoc = next_y_loc();
  133. if((curXLoc!=destX || curYLoc!=destY) && (abs(shipXLoc-curXLoc)>1 || abs(shipYLoc-curYLoc)>1))
  134. search(resultXLoc, resultYLoc, 1);
  135. //-------- set action parameters ----------//
  136. action_mode = ACTION_ASSIGN_TO_SHIP;
  137. action_para = shipRecno;
  138. action_x_loc = destX;
  139. action_y_loc = destY;
  140. }
  141. //----------- End of function Unit::assign_to_ship -----------//
  142. //--------- Begin of function Unit::process_assign_to_ship ---------//
  143. // process unit action of assigning units to ship
  144. //
  145. void Unit::process_assign_to_ship()
  146. {
  147. err_when(sprite_info->loc_width>1 || sprite_info->loc_height>1);
  148. //---------------------------------------------------------------------------//
  149. // clear unit's action if situation is changed
  150. //---------------------------------------------------------------------------//
  151. UnitMarine *shipPtr;
  152. if(unit_array.is_deleted(action_para2))
  153. {
  154. stop2();
  155. return; // stop the unit as the ship is deleted
  156. }
  157. else
  158. {
  159. shipPtr = (UnitMarine*) unit_array[action_para2];
  160. if(shipPtr->nation_recno != nation_recno)
  161. {
  162. stop2();
  163. return; // stop the unit as the ship's nation_recno != the unit's nation_recno
  164. }
  165. }
  166. if(shipPtr->action_mode2!=ACTION_SHIP_TO_BEACH)
  167. {
  168. stop2(); // the ship has changed its action
  169. return;
  170. }
  171. int curXLoc = next_x_loc();
  172. int curYLoc = next_y_loc();
  173. int shipXLoc = shipPtr->next_x_loc();
  174. int shipYLoc = shipPtr->next_y_loc();
  175. if(shipPtr->cur_x==shipPtr->next_x && shipPtr->cur_y==shipPtr->next_y &&
  176. abs(shipXLoc-curXLoc)<=1 && abs(shipYLoc-curYLoc)<=1)
  177. {
  178. //----------- assign the unit now -----------//
  179. if(abs(cur_x-next_x)<sprite_info->speed && abs(cur_y-next_y)<sprite_info->speed)
  180. {
  181. if(ai_action_id)
  182. nation_array[nation_recno]->action_finished(ai_action_id, sprite_recno);
  183. stop2();
  184. set_dir(curXLoc, curYLoc, shipXLoc, shipYLoc);
  185. shipPtr->load_unit(sprite_recno);
  186. return;
  187. }
  188. }
  189. else if(cur_action==SPRITE_IDLE)
  190. set_dir(curXLoc, curYLoc, shipPtr->move_to_x_loc, shipPtr->move_to_y_loc);
  191. //---------------------------------------------------------------------------//
  192. // update location to embark
  193. //---------------------------------------------------------------------------//
  194. int shipActionXLoc = shipPtr->action_x_loc2;
  195. int shipActionYLoc = shipPtr->action_y_loc2;
  196. if(abs(shipActionXLoc-action_x_loc2)>1 || abs(shipActionYLoc-action_y_loc2)>1)
  197. {
  198. if(shipActionXLoc!=action_x_loc2 || shipActionYLoc!=action_y_loc2)
  199. {
  200. Location *unitLocPtr = world.get_loc(curXLoc, curYLoc);
  201. Location *shipActionLocPtr = world.get_loc(shipActionXLoc, shipActionYLoc);
  202. if(unitLocPtr->region_id != shipActionLocPtr->region_id)
  203. {
  204. stop2();
  205. return;
  206. }
  207. assign_to_ship(shipActionXLoc, shipActionYLoc, action_para2);
  208. return;
  209. }
  210. }
  211. }
  212. //----------- End of function Unit::process_assign_to_ship -----------//
  213. //--------- Begin of function Unit::ship_to_beach ---------//
  214. // <int> destX, destY - the location in land planned for LAND_UNIT to embark
  215. // <int&> finalDestX, finalDestY - reference to final location used to embark
  216. //
  217. // This function is only for ship
  218. // move the ship to the coast
  219. //
  220. //
  221. void Unit::ship_to_beach(int destX, int destY, int& finalDestX, int& finalDestY)
  222. {
  223. err_when(unit_res[unit_id]->unit_class!=UNIT_CLASS_SHIP);
  224. err_when(destX<0 || destX>=MAX_WORLD_X_LOC || destY<0 || destY>=MAX_WORLD_Y_LOC);
  225. err_when(terrain_res[world.get_loc(destX, destY)->terrain_id]->average_type == TERRAIN_OCEAN);
  226. //----------------------------------------------------------------//
  227. // return if the unit is dead
  228. //----------------------------------------------------------------//
  229. if(hit_points<=0 || action_mode==ACTION_DIE || cur_action==SPRITE_DIE)
  230. return;
  231. //----------------------------------------------------------------//
  232. // change to move_to if the ship cannot carry units
  233. //----------------------------------------------------------------//
  234. if(unit_res[unit_id]->carry_unit_capacity<=0)
  235. {
  236. move_to(destX, destY, 1);
  237. finalDestX = finalDestY = -1;
  238. return;
  239. }
  240. //----------------------------------------------------------------//
  241. // calculate new destination
  242. //----------------------------------------------------------------//
  243. #ifdef DEBUG
  244. UnitMarine *debugShipPtr = (UnitMarine*) this; //****** for testing
  245. #endif
  246. int curXLoc = next_x_loc();
  247. int curYLoc = next_y_loc();
  248. int resultXLoc, resultYLoc;
  249. stop();
  250. err_when(cur_action==SPRITE_MOVE && cur_x==move_to_x_loc*ZOOM_LOC_WIDTH && cur_y==move_to_y_loc*ZOOM_LOC_HEIGHT);
  251. if(abs(destX-curXLoc)>1 || abs(destY-curYLoc)>1)
  252. {
  253. //-----------------------------------------------------------------------------//
  254. // get a suitable location in the territory as a reference location
  255. //-----------------------------------------------------------------------------//
  256. Location *locPtr = world.get_loc(destX, destY);
  257. UCHAR regionId = locPtr->region_id;
  258. int xStep = curXLoc-destX;
  259. int yStep = curYLoc-destY;
  260. int absXStep = abs(xStep);
  261. int absYStep = abs(yStep);
  262. int count = (absXStep>=absYStep) ? absXStep : absYStep;
  263. int x, y;
  264. long int sameTerr = 0;
  265. for(long int i=1; i<=count; i++)
  266. {
  267. x = destX + int((i*xStep)/count);
  268. y = destY + int((i*yStep)/count);
  269. locPtr = world.get_loc(x, y);
  270. if(locPtr->region_id==regionId)
  271. {
  272. if(locPtr->walkable())
  273. sameTerr = i;
  274. }
  275. }
  276. if(sameTerr)
  277. {
  278. resultXLoc = destX + int((sameTerr*xStep)/count);
  279. resultYLoc = destY + int((sameTerr*yStep)/count);
  280. }
  281. else
  282. {
  283. resultXLoc = destX;
  284. resultYLoc = destY;
  285. }
  286. //------------------------------------------------------------------------------//
  287. // find the path from the ship location in the ocean to the reference location
  288. // in the territory
  289. //------------------------------------------------------------------------------//
  290. if(!ship_to_beach_path_edit(resultXLoc, resultYLoc, regionId))
  291. {
  292. finalDestX = finalDestY = -1;
  293. return; // calling move_to() instead
  294. }
  295. }
  296. else
  297. {
  298. resultXLoc = destX;
  299. resultYLoc = destY;
  300. }
  301. err_when(cur_action==SPRITE_MOVE && cur_x==next_x && cur_y==next_y &&
  302. cur_x==move_to_x_loc*ZOOM_LOC_WIDTH && cur_y==move_to_y_loc*ZOOM_LOC_HEIGHT);
  303. err_when(result_node_array==NULL && result_path_dist);
  304. action_mode = action_mode2 = ACTION_SHIP_TO_BEACH;
  305. action_para = action_para2 = 0;
  306. finalDestX = action_x_loc = action_x_loc2 = resultXLoc;
  307. finalDestY = action_y_loc = action_y_loc2 = resultYLoc;
  308. }
  309. //----------- End of function Unit::ship_to_beach -----------//
  310. //--------- Begin of function Unit::ship_to_beach_path_edit ---------//
  311. // find a path to the beach
  312. //
  313. // <int&> resultXLoc - reference to return final x location the ship move to
  314. // <int&> resultYLoc - reference to return final y location the ship move to
  315. // <UCHAR> regionId - region id of the destination location
  316. //
  317. // return 1 if normal execution
  318. // return 0 if calling move_to() instead
  319. //
  320. int Unit::ship_to_beach_path_edit(int& resultXLoc, int& resultYLoc, UCHAR regionId)
  321. {
  322. int curXLoc = next_x_loc();
  323. int curYLoc = next_y_loc();
  324. if(abs(curXLoc-resultXLoc)<=1 && abs(curYLoc-resultYLoc)<=1)
  325. return 1;
  326. //--------------- find a path to land area -------------------//
  327. UnitMarine *shipPtr = (UnitMarine*) this;
  328. int result = search(resultXLoc, resultYLoc, 1, SEARCH_MODE_TO_LAND_FOR_SHIP, regionId);
  329. if(!result)
  330. return 1;
  331. //----------- update cur location --------//
  332. curXLoc = next_x_loc();
  333. curYLoc = next_y_loc();
  334. err_when(shipPtr->extra_move_in_beach!=NO_EXTRA_MOVE);
  335. //------------------------------------------------------------------------------//
  336. // edit the result path to get a location for embarking
  337. //------------------------------------------------------------------------------//
  338. err_when(result_node_array==NULL && result_node_count);
  339. if(result_node_array && result_node_count)
  340. {
  341. err_when(result_node_count<2);
  342. ResultNode *curNodePtr = result_node_array;
  343. ResultNode *nextNodePtr = curNodePtr + 1;
  344. int moveScale = move_step_magn();
  345. int nodeCount = result_node_count;
  346. Location *locPtr;
  347. int i, j, found, pathDist;
  348. int preXLoc, preYLoc;
  349. int checkXLoc = curXLoc;
  350. int checkYLoc = curYLoc;
  351. int hasMoveStep = 0;
  352. if(checkXLoc!=curNodePtr->node_x || checkYLoc!=curNodePtr->node_y)
  353. {
  354. err_when(abs(checkXLoc-curNodePtr->node_x)>moveScale || abs(checkYLoc-curNodePtr->node_y)>moveScale);
  355. hasMoveStep += moveScale;
  356. checkXLoc = curNodePtr->node_x;
  357. checkYLoc = curNodePtr->node_y;
  358. }
  359. //-----------------------------------------------------------------//
  360. // find the pair of points that one is in ocean and one in land
  361. //-----------------------------------------------------------------//
  362. err_when(terrain_res[world.get_loc(curNodePtr->node_x, curNodePtr->node_y)->terrain_id]->average_type!=TERRAIN_OCEAN);
  363. int vecX, vecY, xMagn, yMagn, magn;
  364. #ifdef DEBUG
  365. int debugLoop1=0, debugLoop2=0;
  366. #endif
  367. for(pathDist=0, found=0, i=1; i<nodeCount; ++i, curNodePtr++, nextNodePtr++)
  368. {
  369. #ifdef DEBUG
  370. err_when(++debugLoop1>10000);
  371. #endif
  372. vecX = nextNodePtr->node_x - curNodePtr->node_x;
  373. vecY = nextNodePtr->node_y - curNodePtr->node_y;
  374. magn = ((xMagn=abs(vecX)) > (yMagn=abs(vecY))) ? xMagn : yMagn;
  375. if(xMagn) { vecX /= xMagn; vecX *= moveScale; }
  376. if(yMagn) { vecY /= yMagn; vecY *= moveScale; }
  377. err_when(abs(vecX)>moveScale && abs(vecY)>moveScale);
  378. //------------- check each location bewteen editNode1 and editNode2 -------------//
  379. for(j=0; j<magn; j+=moveScale)
  380. {
  381. #ifdef DEBUG
  382. err_when(++debugLoop2>10000);
  383. #endif
  384. preXLoc = checkXLoc;
  385. preYLoc = checkYLoc;
  386. checkXLoc += vecX;
  387. checkYLoc += vecY;
  388. locPtr = world.get_loc(checkXLoc, checkYLoc);
  389. if(terrain_res[locPtr->terrain_id]->average_type!=TERRAIN_OCEAN) // found
  390. {
  391. found++;
  392. break;
  393. }
  394. }
  395. if(found)
  396. {
  397. //------------ a soln is found ---------------//
  398. if(!j) // end node should be curNodePtr pointed at
  399. {
  400. pathDist -= hasMoveStep;
  401. result_node_count = i;
  402. result_path_dist = pathDist;
  403. }
  404. else
  405. {
  406. nextNodePtr->node_x = checkXLoc;
  407. nextNodePtr->node_y = checkYLoc;
  408. if(i==1) // first editing
  409. {
  410. ResultNode *firstNodePtr = result_node_array;
  411. if(cur_x==firstNodePtr->node_x*ZOOM_LOC_WIDTH && cur_y==firstNodePtr->node_y*ZOOM_LOC_HEIGHT)
  412. {
  413. go_x = checkXLoc * ZOOM_LOC_WIDTH;
  414. go_y = checkYLoc * ZOOM_LOC_HEIGHT;
  415. }
  416. }
  417. pathDist += (j+moveScale);
  418. pathDist -= hasMoveStep;
  419. result_node_count = i+1;
  420. result_path_dist = pathDist;
  421. }
  422. move_to_x_loc = preXLoc;
  423. move_to_y_loc = preYLoc;
  424. locPtr = world.get_loc((preXLoc+checkXLoc)/2, (preYLoc+checkYLoc)/2);
  425. if(terrain_res[locPtr->terrain_id]->average_type!=TERRAIN_OCEAN)
  426. {
  427. resultXLoc = (preXLoc+checkXLoc)/2;
  428. resultYLoc = (preYLoc+checkYLoc)/2;
  429. }
  430. else
  431. {
  432. resultXLoc = checkXLoc;
  433. resultYLoc = checkYLoc;
  434. }
  435. break;
  436. }
  437. else
  438. pathDist += magn;
  439. }
  440. if(!found)
  441. {
  442. ResultNode *endNodePtr = result_node_array + result_node_count - 1;
  443. if(abs(endNodePtr->node_x-resultXLoc)>1 || abs(endNodePtr->node_y-resultYLoc)>1)
  444. {
  445. move_to(resultXLoc, resultYLoc, -1);
  446. return 0;
  447. }
  448. }
  449. }
  450. else
  451. {
  452. //------------- scan for the surrounding for a land location -----------//
  453. int xShift, yShift, checkXLoc, checkYLoc;
  454. Location *locPtr;
  455. for(int i=2; i<=9; ++i)
  456. {
  457. m.cal_move_around_a_point(i, 3, 3, xShift, yShift);
  458. checkXLoc = curXLoc + xShift;
  459. checkYLoc = curYLoc + yShift;
  460. if(checkXLoc<0 || checkXLoc>=MAX_WORLD_X_LOC || checkYLoc<0 || checkYLoc>=MAX_WORLD_Y_LOC)
  461. continue;
  462. locPtr = world.get_loc(checkXLoc, checkYLoc);
  463. if(locPtr->region_id!=regionId)
  464. continue;
  465. if(terrain_res[locPtr->terrain_id]->average_type!=TERRAIN_OCEAN && locPtr->can_move(UNIT_LAND))
  466. {
  467. resultXLoc = checkXLoc;
  468. resultYLoc = checkYLoc;
  469. return 1;
  470. }
  471. }
  472. return 0;
  473. }
  474. return 1;
  475. }
  476. //----------- End of function Unit::ship_to_beach_path_edit -----------//
  477. //--------- Begin of function Unit::process_ship_to_beach ---------//
  478. // process unit action SHIP_TO_BEACH
  479. //
  480. void Unit::process_ship_to_beach()
  481. {
  482. //----- action_mode never clear, in_beach to skip idle checking
  483. if(cur_action==SPRITE_IDLE)
  484. {
  485. int shipXLoc = next_x_loc();
  486. int shipYLoc = next_y_loc();
  487. if(shipXLoc==move_to_x_loc && shipYLoc==move_to_y_loc)
  488. {
  489. if(abs(move_to_x_loc-action_x_loc2)<=2 && abs(move_to_y_loc-action_y_loc2)<=2)
  490. {
  491. UnitMarine *shipPtr = (UnitMarine*) this;
  492. //------------------------------------------------------------------------------//
  493. // determine whether extra_move is required
  494. //------------------------------------------------------------------------------//
  495. switch(shipPtr->extra_move_in_beach)
  496. {
  497. case NO_EXTRA_MOVE:
  498. if(abs(shipXLoc-action_x_loc2)>1 || abs(shipYLoc-action_y_loc2)>1)
  499. {
  500. err_when(abs(shipXLoc-action_x_loc2)>2 || abs(shipYLoc-action_y_loc2)>2);
  501. err_when(result_node_array);
  502. shipPtr->extra_move();
  503. }
  504. else
  505. {
  506. //shipPtr->in_beach = 1;
  507. shipPtr->extra_move_in_beach = NO_EXTRA_MOVE;
  508. //#### trevor 23/10 #####//
  509. if(ai_action_id)
  510. nation_array[nation_recno]->action_finished(ai_action_id, sprite_recno);
  511. //#### trevor 23/10 #####//
  512. }
  513. break;
  514. case EXTRA_MOVING_IN:
  515. case EXTRA_MOVING_OUT:
  516. //err_when(cur_action!=SPRITE_SHIP_EXTRA_MOVE);
  517. break;
  518. //#### trevor 23/10 #####//
  519. case EXTRA_MOVE_FINISH:
  520. if(ai_action_id)
  521. nation_array[nation_recno]->action_finished(ai_action_id, sprite_recno);
  522. break;
  523. //#### trevor 23/10 #####//
  524. default: err_here();
  525. break;
  526. }
  527. }
  528. }
  529. else
  530. reset_action_para();
  531. }
  532. else if(cur_action==SPRITE_TURN && is_dir_correct())
  533. set_move();
  534. }
  535. //----------- End of function Unit::process_ship_to_beach -----------//
  536. //--------- Begin of function Unit::ship_leave_beach ---------//
  537. void Unit::ship_leave_beach(int shipOldXLoc, int shipOldYLoc)
  538. {
  539. err_when(cur_x!=next_x || cur_y!=next_y);
  540. UnitMarine *shipPtr = (UnitMarine*) this;
  541. err_when(!shipPtr->in_beach && shipPtr->extra_move_in_beach==EXTRA_MOVE_FINISH);
  542. //--------------------------------------------------------------------------------//
  543. // scan for location to leave the beach
  544. //--------------------------------------------------------------------------------//
  545. int curXLoc = next_x_loc();
  546. int curYLoc = next_y_loc();
  547. int xShift, yShift, checkXLoc, checkYLoc, found=0;
  548. Location *locPtr;
  549. //------------- find a location to leave the beach ------------//
  550. for(int i=2; i<=9; i++)
  551. {
  552. m.cal_move_around_a_point(i, 3, 3, xShift, yShift);
  553. checkXLoc = curXLoc + xShift;
  554. checkYLoc = curYLoc + yShift;
  555. if(checkXLoc<0 || checkXLoc>=MAX_WORLD_X_LOC || checkYLoc<0 || checkYLoc>=MAX_WORLD_Y_LOC)
  556. continue;
  557. if(checkXLoc%2 || checkYLoc%2)
  558. continue;
  559. locPtr = world.get_loc(checkXLoc, checkYLoc);
  560. if(terrain_res[locPtr->terrain_id]->average_type==TERRAIN_OCEAN &&
  561. locPtr->can_move(mobile_type))
  562. {
  563. found++;
  564. break;
  565. }
  566. }
  567. if(!found)
  568. return; // no suitable location, wait until finding suitable location
  569. //---------------- leave now --------------------//
  570. set_dir(shipOldXLoc, shipOldYLoc, checkXLoc, checkYLoc);
  571. set_ship_extra_move();
  572. go_x = checkXLoc*ZOOM_LOC_WIDTH;
  573. go_y = checkYLoc*ZOOM_LOC_HEIGHT;
  574. err_when(cur_x==go_x && cur_y==go_y);
  575. }
  576. //----------- End of function Unit::ship_leave_beach -----------//