//------- Begin of function Town::think_ai_migrate_to_town --------// // // Think about the town to migrate to. // int Town::think_ai_migrate_to_town() { //------ think about which town to migrate to ------// Nation* nationPtr = nation_array[nation_recno]; int curRating, bestRating=0, bestTownRecno=0; short *aiTownPtr = nationPtr->ai_town_array; int majorityRace = majority_race(); Town *townPtr; for(int i=0; i<nationPtr->ai_town_count; i++, aiTownPtr++) { if( town_recno == *aiTownPtr ) continue; townPtr = town_array[*aiTownPtr]; err_when( townPtr->nation_recno != nation_recno ); if( !townPtr->is_base_town ) // only migrate to base towns continue; if( townPtr->region_id != region_id ) continue; if( population > MAX_TOWN_POPULATION-townPtr->population ) // if the town does not have enough space for the migration continue; //--------- compare the ratings ---------// curRating = 1000 * townPtr->race_pop_array[majorityRace-1] / townPtr->population; // *1000 so that this will have a much bigger weight than the distance rating curRating += world.distance_rating( center_x, center_y, townPtr->center_x, townPtr->center_y ); if( curRating > bestRating ) { //--- if there is a considerable population of this race, then must migrate to a town with the same race ---// if( race_pop_array[majorityRace-1] >= 6 ) { if( townPtr->majority_race() != majorityRace ) // must be commented out otherwise low population town will never be optimized continue; } bestRating = curRating; bestTownRecno = townPtr->town_recno; } } return bestTownRecno; }
//------- Begin of function Town::think_move_between_town -------// // void Town::think_move_between_town() { //------ move people between linked towns ------// int ourMajorityRace = majority_race(); int raceId, rc, loopCount; Town* townPtr; for( int i=0 ; i<linked_town_count ; i++ ) { townPtr = town_array[ linked_town_array[i] ]; if( townPtr->nation_recno != nation_recno ) continue; loopCount=0; //--- migrate people to the new town ---// while(1) { err_when( ++loopCount > 100 ); rc = 0; raceId = townPtr->majority_race(); // get the linked town's major race if( ourMajorityRace != raceId ) // if our major race is not this race, then move the person to the target town { rc = 1; } else //-- if this town's major race is the same as the target town --// { if( population - townPtr->population > 10 ) // if this town's population is larger than the target town by 10 people, then move rc = 1; } if( rc ) { if( !migrate_to(townPtr->town_recno, COMMAND_AI, raceId) ) break; } else break; } } }
//-------- Begin of function Town::think_spying_town --------// // // Think about planting spies into independent towns and enemy towns. // int Town::think_spying_town() { int majorityRace = majority_race(); //---- don't recruit spy if we are low in cash or losing money ----// Nation* ownNation = nation_array[nation_recno]; if( ownNation->total_population < 30-ownNation->pref_spy/10 ) // don't use spies if the population is too low, we need to use have people to grow population return 0; if( ownNation->total_spy_count > ownNation->total_population * (10+ownNation->pref_spy/5) / 100 ) // 10% to 30% return 0; if( !ownNation->ai_should_spend(ownNation->pref_spy/2) ) // 0 to 50 return 0; //--- pick minority units first (also for increasing town harmony) ---// for( int raceId=1 ; raceId<=MAX_RACE ; raceId++ ) { if( race_pop_array[raceId-1]==0 || raceId==majorityRace ) continue; if( !can_recruit(raceId) ) continue; if( think_spying_town_assign_to(raceId) ) return 1; } //---- then think about assign spies of majority race ----// if( ownNation->pref_spy > 50 ) // only if pref_spy is > 50, it will consider using spies of majority race { if( can_recruit(majorityRace) ) { if( think_spying_town_assign_to(majorityRace) ) return 1; } } return 0; }
//------- Begin of function Firm::think_hire_inn_unit -------// // int Firm::think_hire_inn_unit() { if( !nation_array[nation_recno]->ai_should_hire_unit(30) ) // 30 - importance rating return 0; //---- one firm only hire one foreign race worker ----// int i, foreignRaceCount=0; int majorityRace = majority_race(); if( majorityRace ) { for( i=0 ; i<worker_count ; i++ ) { if( worker_array[i].race_id != majorityRace ) foreignRaceCount++; } } //-------- try to get skilled workers from inns --------// Nation* nationPtr = nation_array[nation_recno]; FirmInn* firmInn, *bestInn=NULL; int curRating, bestRating=0, bestInnUnitId=0; int prefTownHarmony = nationPtr->pref_town_harmony; for( i=0 ; i<nationPtr->ai_inn_count ; i++ ) { firmInn = (FirmInn*) firm_array[ nationPtr->ai_inn_array[i] ]; if( firmInn->region_id != region_id ) continue; InnUnit* innUnit = firmInn->inn_unit_array; for( int j=0 ; j<firmInn->inn_unit_count ; j++, innUnit++ ) { if( innUnit->skill.skill_id != firm_skill_id ) continue; //-------------------------------------------// // Rating of a unit to be hired is based on: // // -distance between the inn and this firm. // -whether the unit is racially homogenous to the majority of the firm workers // //-------------------------------------------// curRating = world.distance_rating( center_x, center_y, firmInn->center_x, firmInn->center_y ); curRating += innUnit->skill.skill_level; if( majorityRace == unit_res[innUnit->unit_id]->race_id ) { curRating += prefTownHarmony; } else { //----------------------------------------------------// // Don't pick this unit if it isn't racially homogenous // to the villagers, and its pref_town_harmony is higher // than its skill level. (This means if its skill level // is low, its chance of being selected is lower. //----------------------------------------------------// if( majorityRace ) { if( foreignRaceCount>0 || prefTownHarmony > innUnit->skill.skill_level-50 ) continue; } } if( curRating > bestRating ) { bestRating = curRating; bestInn = firmInn; bestInnUnitId = j+1; } } } //-----------------------------------------// if( bestInn ) { int unitRecno = bestInn->hire(bestInnUnitId); if( unitRecno ) { unit_array[unitRecno]->assign(loc_x1, loc_y1); return 1; } } return 0; }