//--------- Begin of function Unit::think_independent_unit --------// // // Think about the action of an independent unit. It first tries to // settle to a town. If not successful, it will disband itself. // void Unit::think_independent_unit() { if( !is_all_stop() ) return; if( unit_mode ) // don't let them think a new action if they have something to do currently return; //------ if this unit is a hero -------// if( hero_id ) { think_independent_hero(); return; } //--- don't process if it's a spy and the notify cloak flag is on ---// if( spy_recno ) { //---------------------------------------------// // // If notify_cloaked_nation_flag is 0, the AI // won't control the unit. // // If notify_cloaked_nation_flag is 1, the AI // will control the unit. But not immediately, // it will do it once 5 days so the player can // have a chance to select the unit and set its // notify_cloaked_nation_flag back to 0 if the // player wants. // //---------------------------------------------// if( spy_array[spy_recno]->notify_cloaked_nation_flag==0 ) return; if( info.game_date%5 != sprite_recno%5 ) return; } //-------- if this is a rebel ----------// if( unit_mode == UNIT_MODE_REBEL ) { Rebel* rebelPtr = rebel_array[unit_mode_para]; //--- if the group this rebel belongs to already has a rebel town, assign to it now ---// if( rebelPtr->town_recno ) { if( !town_array.is_deleted(rebelPtr->town_recno) ) { Town* townPtr = town_array[rebelPtr->town_recno]; err_when( townPtr->rebel_recno != rebelPtr->rebel_recno ); assign(townPtr->loc_x1, townPtr->loc_y1); } return; // don't do anything if the town has been destroyed, Rebel::next_day() will take care of it. } } //---- look for towns to assign to -----// Town *townPtr, *bestTown=NULL; int regionId = world.get_region_id( next_x_loc(), next_y_loc() ); int curRating, bestRating=0; int curXLoc = next_x_loc(), curYLoc = next_y_loc(); for( int i=town_array.size() ; i>0 ; i-- ) { if( town_array.is_deleted(i) ) continue; townPtr = town_array[i]; if( townPtr->nation_recno || townPtr->population >= MAX_TOWN_POPULATION || townPtr->region_id != regionId || townPtr->race_id != race_id ) { continue; } curRating = world.distance_rating(curXLoc, curYLoc, townPtr->center_x, townPtr->center_y ); if( curRating > bestRating ) { bestRating = curRating; bestTown = townPtr; } } if( bestTown ) { err_when( unit_mode==UNIT_MODE_REBEL && rebel_array[unit_mode_para]->town_recno && rebel_array[unit_mode_para]->town_recno != bestTown->town_recno ); assign(bestTown->loc_x1, bestTown->loc_y1); } else resign(COMMAND_AI); }
//--------- Begin of function Unit::process_ai --------// // // [int] forceExecute - whether force execute all AI functions // without checking day interavals. // (default: 0) // void Unit::process_ai() { if( unit_mode ) // let functions in OUN_MODE.CPP process units in specific modes return; err_when( !nation_recno ); //------ the behavior_mode of AI units is aggressive ------// behavior_mode = UNIT_AGGRESSIVE; //------- handle Seek Path failures ------// if( ai_handle_seek_path_fail() ) return; //--- if it's a spy from other nation, don't control it ---// if( spy_recno && true_nation_recno() != nation_recno ) { //--- a random chance of the AI catching the spy and resign it ---// if( is_visible() && misc.random(365 * FRAMES_PER_DAY)==0 ) // if the unit stay outside for one year, it will get caught { resign(COMMAND_AI); return; } if( !spy_array[spy_recno]->notify_cloaked_nation_flag ) // if notify_cloaked_nation_flag is 1, the nation will take it as its own spies return; } //----- think about rewarding this unit -----// if( race_id && rank_id != RANK_KING && info.game_date%5 == sprite_recno%5 ) { think_reward(); } //-----------------------------------------// if( !is_visible() ) return; //----- think about using items -------// if( think_use_item() ) return; //---- King flees under attack or surrounded by enemy ---// if( race_id && rank_id==RANK_KING ) { if( think_king_flee() ) return; } //---- General flees under attack or surrounded by enemy ---// if( race_id && rank_id==RANK_GENERAL && info.game_date%7 == sprite_recno%7 ) { if( think_general_flee() ) return; } //---- stop attacking a town with zero resistance ----// if( cur_order.mode==UNIT_ATTACK && nation_recno) { if( !base_obj_array.is_deleted(cur_order.para) ) { BaseObj* baseObj = base_obj_array[cur_order.para]; if( baseObj->cast_to_Town() && baseObj->cast_to_Town()->nation_recno==0 && baseObj->cast_to_Town()->resistance(nation_recno) < 1 ) { stop_order(); return; } } } //------ if the unit is not stop right now ------// if( !is_all_stop() ) { think_stop_chase(); return; } //-----------------------------------------// if( mobile_type==UNIT_LAND ) { if( ai_escape_fire() ) return; } //---------- if this is your spy --------// if( spy_recno && true_nation_recno()==nation_recno ) think_spy_action(); //----- if this unit is on a attack mission ----// if( is_all_stop() && in_ai_attack_mission ) { if( ai_attack_next_target() ) return; } //------ if this unit is from a camp --------// if( home_camp_firm_recno ) { FirmCamp* firmCamp; FirmMonsterFortress* firmMonsterFortress; int rc = 0; if( firm_array.is_deleted(home_camp_firm_recno) ) { rc = 0; } if( (firmCamp = firm_array[home_camp_firm_recno]->cast_to_FirmCamp()) ) { if( rank_id == RANK_SOLDIER ) rc = firmCamp->soldier_count < MAX_SOLDIER; else rc = !firmCamp->overseer_recno; } else if( (firmMonsterFortress = firm_array[home_camp_firm_recno]->cast_to_FirmMonsterFortress()) ) { rc = (unit_id == firmMonsterFortress->support_unit_id && !firmMonsterFortress->is_extra_builder_full()) || !firmMonsterFortress->is_soldier_full(); } else { err_here(); } if( rc ) { if( return_camp() ) return; } home_camp_firm_recno = 0; // the camp is already occupied by somebody } //------- if the unit is idle ------------// if( is_all_stop() ) { if( race_id && rank_id==RANK_KING ) { if( is_human() ) think_king_action(); else think_normal_monster_action(); } else if( race_id && rank_id==RANK_GENERAL ) { think_general_action(); } else { int unitClass = unit_res[unit_id]->unit_class; //------ if this unit is a weapon ------// // ######### begin Gilbert 24/3 ########// // BUGHERE : monster weapon was UNIT_CLASS_MONSTER but now UNIT_CLASS_MONS_WEAPON if( unitClass == UNIT_CLASS_WEAPON || unitClass == UNIT_CLASS_MONS_WEAPON ) // ######### end Gilbert 24/3 ########// { if( info.game_date%15 == sprite_recno%15 ) // don't call too often as the action may fail and it takes a while to call the function each time { think_weapon_action(); //-- ships AI are called in UnitMarine --// } } //------ if this unit is a monster ------// else if( unitClass == UNIT_CLASS_MONSTER || unitClass == UNIT_CLASS_INSECT || unitClass == UNIT_CLASS_ANIMAL ) { //--- if previous attempts for new action failed, don't call think_normal_human_action() so frequently then ---// if( ai_no_suitable_action ) { if( info.game_date%15 != sprite_recno%15 ) // don't call too often as the action may fail and it takes a while to call the function each time return; } if( !think_normal_monster_action() ) { ai_no_suitable_action = true; // set this flag so think_normal_human_action() won't be called continously ai_move_to_nearby_firm(FIRM_LAIR); } } //------ if this unit is a human ------// else if( unitClass == UNIT_CLASS_HUMAN ) { //--- if previous attempts for new action failed, don't call think_normal_human_action() so frequently then ---// if( ai_no_suitable_action ) { if( info.game_date%15 != sprite_recno%15 ) // don't call too often as the action may fail and it takes a while to call the function each time return; } if( !think_normal_human_action() ) { ai_no_suitable_action = true; // set this flag so think_normal_human_action() won't be called continously // if( !leader_unit_recno ) // only when the unit is not led by a commander // resign(COMMAND_AI); // else ai_move_to_nearby_town(); } } //---- if this unit is a wagon -----// else if( unitClass == UNIT_CLASS_WAGON ) { think_assign_human_to_town(); } } } }