//-------- Begin of function SpyArray::catch_spy ------// // // <int> spyPlace - either SPY_TOWN or SPY_FIRM // <int> spyPlacePara - town_recno or firm_recno // int SpyArray::catch_spy(int spyPlace, int spyPlacePara) { int nationRecno, totalPop; if( spyPlace == SPY_TOWN ) { Town* townPtr = town_array[spyPlacePara]; nationRecno = townPtr->nation_recno; totalPop = townPtr->population; } else if( spyPlace == SPY_FIRM ) { Firm* firmPtr = firm_array[spyPlacePara]; nationRecno = firmPtr->nation_recno; if( firmPtr->cast_to_FirmCamp() ) { FirmCamp *firmCamp = firmPtr->cast_to_FirmCamp(); totalPop = firmCamp->soldier_count + (firmCamp->overseer_recno>0); } else if( firmPtr->cast_to_FirmWork() ) { FirmWork *firmWork = firmPtr->cast_to_FirmWork(); totalPop = firmWork->worker_count; } else if( firmPtr->cast_to_FirmTrain() ) { totalPop = firmPtr->cast_to_FirmTrain()->trainee_count; return 0; // don't catch spy } else if( firmPtr->cast_to_FirmInn() ) { totalPop = firmPtr->cast_to_FirmInn()->inn_unit_count; return 0; // don't catch spy } else if( firmPtr->cast_to_FirmMarket() ) { totalPop = 0; return 0; } else if( firmPtr->cast_to_FirmMonsterTrain() ) { totalPop = firmPtr->cast_to_FirmMonsterTrain()->trainee_count; return 0; // don't catch spy } else if( firmPtr->cast_to_FirmMonsterAlchemy() ) { totalPop = 0; return 0; } else if( firmPtr->cast_to_FirmLishorr() ) { totalPop = 0; return 0; } else if( firmPtr->cast_to_FirmMonsterFortress() ) { FirmMonsterFortress *firmMonsterFortress = firmPtr->cast_to_FirmMonsterFortress(); totalPop = firmMonsterFortress->archer_count + firmMonsterFortress->extra_builder_count; } else if( firmPtr->cast_to_FirmAnimal() ) { totalPop = 0; return 0; } else if( firmPtr->cast_to_FirmIncubator() ) { totalPop = 0; return 0; } else if( firmPtr->cast_to_FirmMagic() ) { totalPop = 0; return 0; } else if( firmPtr->cast_to_FirmOffensive() ) { totalPop = 0; return 0; } else if( firmPtr->cast_to_FirmOffensive2() ) { totalPop = 0; return 0; } else { err_here(); totalPop = 0; } } else err_here(); //--- calculate the total of anti-spy skill in this town ----// int enemySpyCount=0, counterSpySkill=0; Spy* spyPtr; int techLevel = 0; if( nationRecno ) techLevel = tech_res[TECH_COUNTER_SPY]->get_nation_tech_level(nationRecno); int i; for( i=size() ; i>0 ; i-- ) { if( is_deleted(i) ) continue; spyPtr = spy_array[i]; if( spyPtr->spy_place == spyPlace && spyPtr->spy_place_para == spyPlacePara ) { if( spyPtr->true_nation_recno == nationRecno ) counterSpySkill += spyPtr->spy_skill + spyPtr->spy_skill * techLevel / 2; else enemySpyCount++; } } //----- if all citizens are enemy spies ----// if( enemySpyCount == totalPop ) return 0; err_when( enemySpyCount > totalPop ); //-------- try to catch enemy spies now ------// for( i=spy_array.size() ; i>0 ; i-- ) { if( spy_array.is_deleted(i) ) continue; spyPtr = spy_array[i]; if( spyPtr->action_mode == SPY_IDLE ) // it is very hard to get caught in sleep mode continue; if( spyPtr->spy_place == spyPlace && spyPtr->spy_place_para == spyPlacePara && spyPtr->true_nation_recno != nationRecno ) // doesn't get caught in sleep mode { int escapeChance = 100 + spyPtr->spy_skill - counterSpySkill; escapeChance = max( spyPtr->spy_skill/10, escapeChance ); if( m.random(escapeChance) == 0 ) { spyPtr->get_killed(); // only catch one spy per calling return 1; } } } return 0; }