OAI_ACT.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 : OAI_ACT.CPP
  21. //Description: AI - action progressing functions
  22. #include <ALL.h>
  23. #include <OBOX.h>
  24. #include <OSYS.h>
  25. #include <OCONFIG.h>
  26. #include <OINFO.h>
  27. #include <OU_MARI.h>
  28. #include <ONATION.h>
  29. //------- Begin of function Nation::process_action --------//
  30. //
  31. // [int] priorityActionRecno - if this is given, this specific action will
  32. // be processed first. Otherwise it will process
  33. // actions in the array in a sequential order.
  34. //
  35. // [int] processActionMode - if this is given, only message of this type
  36. // will be processed and all messages in the queued of this
  37. // type will be processed.
  38. //
  39. // Note: priorityActionRecno and processActionMode couldn't be used at
  40. // the same time.
  41. //
  42. // return: <int> 1 - all messages of the specific action mode are processed or all messages are processed
  43. // or the priority message has been processed.
  44. //
  45. int Nation::process_action(int priorityActionRecno, int processActionMode)
  46. {
  47. err_when( priorityActionRecno && processActionMode );
  48. // #define MAX_PROCESS_ACTION_TIME 0.01 // maximum time given to processing actions (second)
  49. // unsigned expireTime = m.get_time() + (unsigned)(MAX_PROCESS_ACTION_TIME*1000);
  50. int actionRecno, rc, delFlag, doneFlag=0;
  51. int thisSessionProcessCount=0; // actions processed in this call session
  52. ActionNode* actionNode;
  53. int divider = 4-config.ai_aggressiveness; // the more nations there, the less process count
  54. int nationRecno = nation_recno;
  55. int maxSessionProcessCount = 70 / nation_array.nation_count / max(divider,1);
  56. for( actionRecno=1 ; actionRecno<=action_count() &&
  57. (thisSessionProcessCount < maxSessionProcessCount || processActionMode) && !doneFlag ; // if processActionMode has been specific, then all messages in the queue of this type will be processed
  58. actionRecno++ )
  59. {
  60. //----- priority action ------//
  61. if( priorityActionRecno )
  62. {
  63. actionRecno = priorityActionRecno;
  64. doneFlag = 1; // mark it done, so if the function "continue" to the next loop, the function will end
  65. }
  66. actionNode = get_action(actionRecno);
  67. //----- if only process specific action mode -----//
  68. if( processActionMode && actionNode->action_mode != processActionMode )
  69. continue;
  70. //--- if the AI action is about processing diplomatic message ---//
  71. if( actionNode->action_mode == ACTION_AI_PROCESS_TALK_MSG &&
  72. processActionMode != ACTION_AI_PROCESS_TALK_MSG )
  73. {
  74. if( m.random(10) > 0 ) // 1/10 chance of processing the diplomatic messages
  75. continue;
  76. }
  77. //----------------------------------------------//
  78. if( actionNode->processing_instance_count == actionNode->instance_count )
  79. {
  80. //---------------------------------------------//
  81. //
  82. // If this action has been marked processing for over 6 months
  83. // and we still haven't received finishing notifications,
  84. // then there may be some accidents (or bugs) happened, and
  85. // we will need to delete the action.
  86. //
  87. //---------------------------------------------//
  88. if( info.game_date > actionNode->add_date + 30 * 6 )
  89. {
  90. del_action(actionRecno);
  91. actionRecno--; // stay in this array position as the current one has been deleted, the following one replace the current one's position
  92. }
  93. continue;
  94. }
  95. err_when( actionNode->processing_instance_count > actionNode->instance_count );
  96. if( info.game_date < actionNode->next_retry_date && !priorityActionRecno ) // priorityAction bypass retry date checking
  97. continue;
  98. if( actionNode->retry_count==0 ) // the actionNode may still exist even when retry_count==0, waiting for processed_count to reach processing_count
  99. continue;
  100. //-- there is an unprocessing action in this waiting node --//
  101. switch( actionNode->action_mode )
  102. {
  103. case ACTION_AI_BUILD_FIRM:
  104. rc = ai_build_firm(actionNode);
  105. break;
  106. case ACTION_AI_ASSIGN_OVERSEER:
  107. rc = ai_assign_overseer(actionNode);
  108. break;
  109. case ACTION_AI_ASSIGN_CONSTRUCTION_WORKER:
  110. rc = ai_assign_construction_worker(actionNode);
  111. break;
  112. case ACTION_AI_ASSIGN_WORKER:
  113. rc = ai_assign_worker(actionNode);
  114. break;
  115. case ACTION_AI_ASSIGN_SPY:
  116. rc = ai_assign_spy(actionNode);
  117. break;
  118. case ACTION_AI_SCOUT:
  119. rc = ai_scout(actionNode);
  120. break;
  121. case ACTION_AI_SETTLE_TO_OTHER_TOWN:
  122. rc = ai_settle_to_other_town(actionNode);
  123. break;
  124. case ACTION_AI_PROCESS_TALK_MSG:
  125. rc = ai_process_talk_msg(actionNode);
  126. break;
  127. case ACTION_AI_SEA_TRAVEL:
  128. rc = ai_sea_travel(actionNode);
  129. break;
  130. case ACTION_AI_SEA_TRAVEL2:
  131. rc = ai_sea_travel2(actionNode);
  132. break;
  133. case ACTION_AI_SEA_TRAVEL3:
  134. rc = ai_sea_travel3(actionNode);
  135. break;
  136. }
  137. if( nation_array.is_deleted(nationRecno) ) // diplomatic option can result in surrendering
  138. return 0;
  139. thisSessionProcessCount++;
  140. //------ check the return result -------//
  141. delFlag = 0;
  142. if( rc==1 ) // the action has been processed, but not sure whether it is complete or not
  143. {
  144. actionNode->processing_instance_count++;
  145. //---------------------------------------------------//
  146. // for ACTION_DYNAMIC, the action is immediately
  147. // deleted when processing_instance_count == instance_count.
  148. //---------------------------------------------------//
  149. if( actionNode->action_type == ACTION_DYNAMIC )
  150. {
  151. if( actionNode->processing_instance_count > actionNode->instance_count )
  152. delFlag = 1;
  153. }
  154. }
  155. else if( rc==0 ) // action failed, retry
  156. {
  157. actionNode->next_retry_date = info.game_date + 7; // try again one week later
  158. if( --actionNode->retry_count==0 )
  159. delFlag = 1;
  160. err_when( actionNode->retry_count < 0 );
  161. }
  162. else if( rc== -1 ) // action failed, remove immediately if return -1
  163. {
  164. actionNode->retry_count = 0;
  165. delFlag = 1;
  166. }
  167. //-----------------------------------------//
  168. if( delFlag && actionNode->processing_instance_count == actionNode->processed_instance_count ) // if processing_count > processed_count, do not remove this ActionNode, as there are some unit using this actionNode, when they finish or fail the action, processed_count will increase and processing_count will reach processed_count
  169. {
  170. del_action(actionRecno);
  171. actionRecno--; // stay in this array position as the current one has been deleted, the following one replace the current one's position
  172. }
  173. }
  174. return actionRecno > action_count() || doneFlag;
  175. }
  176. //---------- End of function Nation::process_action --------//
  177. //------- Begin of function Nation::process_action_id --------//
  178. //
  179. // Process a specific action.
  180. //
  181. int Nation::process_action_id(int actionId)
  182. {
  183. for( int i=action_count() ; i>0 ; i-- )
  184. {
  185. if( get_action(i)->action_id == actionId )
  186. {
  187. process_action(i);
  188. return 1;
  189. }
  190. }
  191. return 0;
  192. }
  193. //---------- End of function Nation::process_action_id --------//
  194. //------- Begin of function Nation::get_action_based_on_id --------//
  195. //
  196. // Return ActionNode for the given actionId.
  197. //
  198. ActionNode* Nation::get_action_based_on_id(int actionId)
  199. {
  200. for( int i=action_count() ; i>0 ; i-- )
  201. {
  202. if( get_action(i)->action_id == actionId )
  203. {
  204. return get_action(i);
  205. }
  206. }
  207. return 0;
  208. }
  209. //---------- End of function Nation::get_action_based_on_id --------//
  210. //--------- Begin of function Nation::add_action --------//
  211. //
  212. // <short> xLoc, yLoc - location of the action
  213. // <short> refXLoc, refYLoc - reference location (optional, not all action types need this)
  214. // <int> actionMode - action mode
  215. // <int> actionPara - action parameter
  216. // [int] instanceCount - no. of instances of this action should be carried out
  217. // (default: 1)
  218. // [int] unitRecno - recno of the unit responsible for this action
  219. // if not given, an appropriate unit will be found.
  220. // [int] actionPara2 - action para2
  221. // [short*] groupUnitArray - array of unit recno in the group for the action
  222. // the no. of units in the array is stored in instance_count
  223. // (default: NULL)
  224. //
  225. // return: <int> recno of the action added in action_array
  226. // 0 - if the action is not added as it is already in action_array.
  227. //
  228. int Nation::add_action(short xLoc, short yLoc, short refXLoc, short refYLoc,
  229. int actionMode, int actionPara, int instanceCount,
  230. int unitRecno, int actionPara2, short* groupUnitArray)
  231. {
  232. err_when( instanceCount < 1 );
  233. //--- check if the action has been added already or not ---//
  234. if( is_action_exist(xLoc, yLoc, refXLoc, refYLoc, actionMode, actionPara, unitRecno) )
  235. return 0;
  236. //---------- queue the action ----------//
  237. ActionNode actionNode;
  238. memset( &actionNode, 0, sizeof(ActionNode) );
  239. actionNode.action_mode = actionMode; // what kind of action
  240. actionNode.action_para = actionPara; // parameter of the action
  241. actionNode.action_para2 = actionPara2; // parameter of the action
  242. actionNode.action_x_loc = xLoc; // location to act to
  243. actionNode.action_y_loc = yLoc;
  244. actionNode.ref_x_loc = refXLoc; // the refective location of this action make to
  245. actionNode.ref_y_loc = refYLoc;
  246. actionNode.retry_count = STD_ACTION_RETRY_COUNT; // number of term to wait before discarding this action
  247. actionNode.instance_count = instanceCount; // num of this action being processed in the waiting queue
  248. int immediateProcess=0;
  249. if( groupUnitArray )
  250. {
  251. // the no. of units in the array is stored in instance_count
  252. err_when( instanceCount < 1 );
  253. err_when( instanceCount > ActionNode::MAX_ACTION_GROUP_UNIT );
  254. memcpy( actionNode.group_unit_array, groupUnitArray, instanceCount * sizeof(groupUnitArray[0]) );
  255. immediateProcess = 1; // have to execute this command immediately as the unit in unit_array[] may change
  256. actionNode.retry_count = 1; // only try once as the unit in unit_array[] may change
  257. }
  258. if( unitRecno )
  259. {
  260. //-- this may happen when the unit is a spy and has just changed cloak --//
  261. if( !nation_array[unit_array[unitRecno]->true_nation_recno()]->is_ai() &&
  262. !nation_array[unit_array[unitRecno]->nation_recno]->is_ai() )
  263. {
  264. return 0;
  265. }
  266. //-------------------------------------//
  267. actionNode.unit_recno = unitRecno;
  268. if( unit_array[unitRecno]->is_visible() )
  269. {
  270. immediateProcess = 1; // have to execute this command immediately as the unit in unit_array[] may change
  271. actionNode.retry_count = 1; // only try once as the unit in unit_array[] may change
  272. }
  273. else //--- the unit is still being trained ---//
  274. {
  275. actionNode.next_retry_date = info.game_date + TOTAL_TRAIN_DAYS + 1;
  276. }
  277. }
  278. //-------- set action type ---------//
  279. actionNode.action_type = ACTION_FIXED; // default action type
  280. //------- link into action_array --------//
  281. return add_action( &actionNode, immediateProcess );
  282. }
  283. //---------- End of function Nation::add_action --------//
  284. //--------- Begin of function Nation::add_action --------//
  285. //
  286. // <ActionNode*> actionNode - the action node to be added.
  287. // [int] immediateProcess - process this action immediately
  288. //
  289. int Nation::add_action(ActionNode* actionNode, int immediateProcess)
  290. {
  291. //----------- reset some vars -----------//
  292. actionNode->add_date = info.game_date;
  293. actionNode->action_id = ++last_action_id;
  294. actionNode->retry_count = STD_ACTION_RETRY_COUNT;
  295. actionNode->processing_instance_count = 0;
  296. actionNode->processed_instance_count = 0;
  297. //------- link into action_array --------//
  298. action_array.linkin( actionNode );
  299. if( immediateProcess )
  300. process_action( action_array.recno() );
  301. return action_array.recno();
  302. }
  303. //---------- End of function Nation::add_action --------//
  304. //--------- Begin of function Nation::del_action --------//
  305. //
  306. void Nation::del_action(int actionRecno)
  307. {
  308. action_array.linkout(actionRecno);
  309. }
  310. //---------- End of function Nation::del_action --------//
  311. //--------- Begin of function Nation::is_action_exist --------//
  312. //
  313. // <short> actionXLoc, actionYLoc - action_?_loc in ActionNode to match with
  314. // <short> refXLoc, refYLoc - ref_?_loc in ActionNode to match with
  315. // <int> actionMode - action mode
  316. // <int> actionPara - parameter of the action
  317. // [int] unitRecno - unit recno to match with, only useful for actions under processing.
  318. // [int] checkMode - 1-check actionXLoc & actionYLoc only
  319. // 2-check refXLoc & refYLoc only
  320. // 0-check both
  321. // (default: 0)
  322. //
  323. // return: <int> >0 if the action recno of the existing action
  324. // ==0 if not exist
  325. //
  326. int Nation::is_action_exist(short actionXLoc, short actionYLoc, short refXLoc, short refYLoc, int actionMode, int actionPara, int unitRecno, int checkMode)
  327. {
  328. int i;
  329. ActionNode* actionNode;
  330. for( i=action_count() ; i>0 ; i-- )
  331. {
  332. actionNode = get_action(i);
  333. if( actionNode->action_mode == actionMode &&
  334. actionNode->action_para == actionPara )
  335. {
  336. if( unitRecno && unitRecno != actionNode->unit_recno ) // it requests to match the unit recno and it is not matched here
  337. continue;
  338. if( refXLoc>=0 )
  339. {
  340. if( checkMode==0 || checkMode==2 )
  341. {
  342. if( actionNode->ref_x_loc==refXLoc && actionNode->ref_y_loc==refYLoc)
  343. return i;
  344. }
  345. }
  346. else
  347. {
  348. if( checkMode==0 || checkMode==1 )
  349. {
  350. if( actionNode->action_x_loc==actionXLoc && actionNode->action_y_loc==actionYLoc )
  351. return i;
  352. }
  353. }
  354. }
  355. }
  356. return 0;
  357. }
  358. //---------- End of function Nation::is_action_exist --------//
  359. //--------- Begin of function Nation::is_action_exist --------//
  360. //
  361. // Check if the an action of the specific mode and para
  362. // exists in the action_array.
  363. //
  364. // <int> actionMode - action mode
  365. // <int> actionPara - parameter of the action
  366. // [int] regionId - if this parameter is given, only
  367. // action with destination in this
  368. // region will be checked.
  369. //
  370. int Nation::is_action_exist(int actionMode, int actionPara, int regionId)
  371. {
  372. int i;
  373. ActionNode* actionNode;
  374. for( i=action_count() ; i>0 ; i-- )
  375. {
  376. actionNode = get_action(i);
  377. if( actionNode->action_mode == actionMode &&
  378. actionNode->action_para == actionPara )
  379. {
  380. if( !regionId )
  381. return 1;
  382. err_when( actionNode->action_x_loc < 0 ||
  383. actionNode->action_y_loc < 0 ||
  384. actionNode->action_x_loc >= MAX_WORLD_X_LOC ||
  385. actionNode->action_y_loc >= MAX_WORLD_Y_LOC );
  386. if( world.get_region_id(actionNode->action_x_loc,
  387. actionNode->action_y_loc) == regionId )
  388. {
  389. return 1;
  390. }
  391. }
  392. }
  393. return 0;
  394. }
  395. //---------- End of function Nation::is_action_exist --------//
  396. //--------- Begin of function Nation::is_build_action_exist --------//
  397. //
  398. // Return 1 if there is already a firm queued for building with
  399. // a building location that is within the effective range
  400. // of the given position.
  401. //
  402. int Nation::is_build_action_exist(int firmId, int xLoc, int yLoc)
  403. {
  404. int i;
  405. ActionNode* actionNode;
  406. for( i=action_count() ; i>0 ; i-- )
  407. {
  408. actionNode = get_action(i);
  409. if( actionNode->action_mode == ACTION_AI_BUILD_FIRM &&
  410. actionNode->action_para == firmId )
  411. {
  412. if( m.points_distance( actionNode->action_x_loc, actionNode->action_y_loc,
  413. xLoc, yLoc) <= EFFECTIVE_FIRM_TOWN_DISTANCE )
  414. {
  415. return 1;
  416. }
  417. }
  418. }
  419. return 0;
  420. }
  421. //---------- End of function Nation::is_build_action_exist --------//
  422. //--------- Begin of function Nation::action_finished --------//
  423. //
  424. // The action under processing is finished successfully.
  425. //
  426. // <WORD> aiActionId - the id. of the action to be marked finished.
  427. // [short] unitRecno - if this is given, the the unit's all action
  428. // will be stopped.
  429. // [int] actionFailure - whether the action is failed and called by
  430. // action_failure(). (default: 0)
  431. //
  432. void Nation::action_finished(WORD aiActionId, short unitRecno, int actionFailure)
  433. {
  434. //----- locate the ActionNode of this action ------//
  435. int actionRecno;
  436. ActionNode* actionNode;
  437. //----- try to match actions by unitRecno first ----//
  438. for( actionRecno=action_count() ; actionRecno>0 ; actionRecno-- )
  439. {
  440. actionNode = get_action(actionRecno);
  441. if( aiActionId == actionNode->action_id )
  442. break;
  443. }
  444. if( actionRecno==0 ) // not found
  445. {
  446. // if( sys.debug_session && !sys.signal_exit_flag )
  447. // box.msg( "Error: action_finished() - entry not found." );
  448. stop_unit_action(unitRecno);
  449. return;
  450. }
  451. //------------------------------------------------//
  452. //
  453. // In the above condition is true, that means this ship
  454. // unit has called this function once and the current
  455. // calling is a duplicated calling.
  456. //
  457. //------------------------------------------------//
  458. int shouldStop=1;
  459. if( actionNode->action_mode == ACTION_AI_SEA_TRAVEL ) // don't reset the unit's ai_action_id in ACTION_AI_SEA_TRAVEL mode as if we reset it, the ship will take new action and won't wait for the units to go aboard.
  460. {
  461. if( !unit_array.is_deleted(unitRecno) &&
  462. unit_res[ unit_array[unitRecno]->unit_id ]->unit_class == UNIT_CLASS_SHIP )
  463. {
  464. if( actionNode->action_para2 )
  465. {
  466. return;
  467. }
  468. else
  469. {
  470. actionNode->action_para2 = unitRecno;
  471. shouldStop = 0;
  472. }
  473. }
  474. }
  475. //---------------------------------------------//
  476. //
  477. // Only handle ACTION_FIXED, for ACTION_DYNAMIC,
  478. // the action is immediately deleted when
  479. // processing_instance_count == instance_count.
  480. //
  481. //---------------------------------------------//
  482. if( actionNode->action_type != ACTION_FIXED )
  483. {
  484. stop_unit_action(unitRecno);
  485. return;
  486. }
  487. //-------------------------------------------------//
  488. actionNode->processed_instance_count++;
  489. err_when( actionNode->processed_instance_count > actionNode->processing_instance_count );
  490. err_when( actionNode->processed_instance_count > actionNode->instance_count );
  491. //---- if all requested instances are processed ----//
  492. int allDoneFlag=0;
  493. if( actionNode->processed_instance_count >= actionNode->instance_count )
  494. allDoneFlag = 1;
  495. //---- if the action is failed and all the outstanding units are finished, del the action ---//
  496. else if( actionNode->retry_count==0 &&
  497. actionNode->processed_instance_count >= actionNode->processing_instance_count )
  498. {
  499. allDoneFlag = 1;
  500. }
  501. //------- stop the AI actions of the unit -----//
  502. if( shouldStop )
  503. stop_unit_action(unitRecno);
  504. //---- if the action is done, see if there needs to be a following action ----//
  505. if( allDoneFlag )
  506. {
  507. auto_next_action(actionNode);
  508. del_action(actionRecno);
  509. }
  510. }
  511. //---------- End of function Nation::action_finished --------//
  512. //--------- Begin of function Nation::action_failure --------//
  513. //
  514. // It's basically the same as action_finish(). Now there isn't any
  515. // difference.
  516. //
  517. // <WORD> aiActionId - the id. of the action to be marked finished.
  518. // [short] unitRecno - if this is given, the the unit's all action
  519. // will be stopped.
  520. //
  521. void Nation::action_failure(WORD aiActionId, short unitRecno)
  522. {
  523. //-- if the unit is a ship, ignore it as it will be called by stop2() when it stops, but hasn't yet finished its action --//
  524. /*
  525. Unit* unitPtr = unit_array[unitRecno];
  526. if( unit_res[unitPtr->unit_id]->unit_class == UNIT_CLASS_SHIP )
  527. {
  528. int actionMode = get_action_based_on_id(aiActionId)->action_mode;
  529. if( actionMode == ACTION_AI_SEA_TRAVEL ||
  530. actionMode == ACTION_AI_SEA_TRAVEL2 )
  531. {
  532. return;
  533. }
  534. }
  535. */
  536. //------------------------------------------------//
  537. action_finished(aiActionId, unitRecno, 1); // 1 - action failure
  538. }
  539. //---------- End of function Nation::action_failure ---------//
  540. //--------- Begin of function Nation::stop_unit_action --------//
  541. void Nation::stop_unit_action(short unitRecno)
  542. {
  543. if( !unitRecno )
  544. return;
  545. //------- stop the AI actions of the unit -----//
  546. //
  547. // It is possible that this is not an AI unit as
  548. // when a player spy cloaked as an enemy unit,
  549. // the AI will control it.
  550. //
  551. //---------------------------------------------//
  552. if( !unit_array.is_deleted(unitRecno) )
  553. {
  554. Unit* unitPtr = unit_array[unitRecno];
  555. unitPtr->ai_action_id = 0;
  556. //---- if the unit is a ship on the beach and it's mode isn't NO_EXTRA_MOVE, we couldn't call stop2() as that will cause bug ---//
  557. if(unitPtr->action_mode2==ACTION_SHIP_TO_BEACH)
  558. {
  559. UnitMarine *shipPtr = (UnitMarine*) unitPtr;
  560. err_when( unit_res[shipPtr->unit_id]->unit_class != UNIT_CLASS_SHIP );
  561. if( shipPtr->extra_move_in_beach != NO_EXTRA_MOVE )
  562. return;
  563. }
  564. //--------------------------------------------------//
  565. unitPtr->stop2();
  566. unitPtr->reset_action_misc_para();
  567. err_when(unitPtr->in_auto_defense_mode());
  568. err_when(unitPtr->action_x_loc!=-1 || unitPtr->action_y_loc!=-1);
  569. err_when(unitPtr->action_mode!=ACTION_STOP);
  570. }
  571. }
  572. //---------- End of function Nation::stop_unit_action ---------//
  573. //--------- Begin of function Nation::auto_next_action --------//
  574. //
  575. // Automatically create a follow-up action to this action
  576. // if this action needs one.
  577. //
  578. void Nation::auto_next_action(ActionNode* actionNode)
  579. {
  580. int actionRecno=0;
  581. switch( actionNode->action_mode )
  582. {
  583. case ACTION_AI_SEA_TRAVEL:
  584. actionNode->action_mode = ACTION_AI_SEA_TRAVEL2;
  585. actionNode->instance_count = 1; // only move one ship, it was previously set to the no. units to aboard the ship
  586. actionRecno = add_action(actionNode, 1); // 1-immediate process flag
  587. break;
  588. case ACTION_AI_SEA_TRAVEL2:
  589. actionNode->action_mode = ACTION_AI_SEA_TRAVEL3;
  590. actionRecno = add_action(actionNode, 1); // 1-immediate process flag
  591. break;
  592. }
  593. if( actionRecno )
  594. process_action(actionRecno);
  595. }
  596. //---------- End of function Nation::auto_next_action ---------//