Exemple #1
0
//-------- Begin of function Nation::hire_best_capturer ------//
//
// Hire the best unit you can find in one of the existing inns.
//
// <int>  townRecno 			    - recno of the town to capture
// <int>  raceId				    - race id. of the unit to hire
//
// return: <int> the recno of the unit hired.
//
int Nation::hire_best_capturer(int townRecno, int raceId)
{
	if( !ai_should_hire_unit(30) )		// 30 - importance rating
		return 0;

	FirmInn	*firmInn;
	Firm		*firmPtr;
	InnUnit *innUnit;
	Skill		*innUnitSkill;
	int		i, j, innUnitCount, curRating;
	int		bestRating=0, bestInnRecno=0, bestInnUnitId=0;
	Town* 	townPtr = town_array[townRecno];
	int		destRegionId = world.get_region_id(townPtr->loc_x1, townPtr->loc_y1);

	for(i=0; i<ai_inn_count; i++)
	{
		firmPtr = (FirmInn*) firm_array[ai_inn_array[i]];

		err_when( firmPtr->firm_id != FIRM_INN );

		if( firmPtr->region_id != destRegionId )
			continue;

		firmInn = firmPtr->cast_to_FirmInn();

		innUnitCount=firmInn->inn_unit_count;

		if( !innUnitCount )
			continue;

		innUnit = firmInn->inn_unit_array + innUnitCount - 1;

		//------- check units in the inn ---------//

		for(j=innUnitCount; j>0; j--, innUnit--)
		{
			innUnitSkill = &(innUnit->skill);

			if( innUnitSkill->skill_id==SKILL_LEADING &&
				 unit_res[innUnit->unit_id]->race_id == raceId &&
				 cash >= innUnit->hire_cost )
			{
				//----------------------------------------------//
				// evalute a unit on:
				// -its race, whether it's the same as the nation's race
				// -the inn's distance from the destination
				// -the skill level of the unit.
				//----------------------------------------------//

				curRating = innUnitSkill->skill_level;

				if( curRating > bestRating )
				{
					bestRating = curRating;

					bestInnRecno  = firmInn->firm_recno;
					bestInnUnitId = j;
				}
			}
		}
	}

	if( !bestInnUnitId )
		return 0;

	//----------------------------------------------------//

	firmInn = (FirmInn*) firm_array[bestInnRecno];

	int unitRecno = firmInn->hire(bestInnUnitId);

	if( !unitRecno )
		return 0;

	unit_array[unitRecno]->set_rank(RANK_GENERAL);

	return unitRecno;
}
Exemple #2
0
//--------- Begin of function Nation::hire_unit --------//
//
// <int>		raceId  - the race the selected unit should have
//							 (0 for any races)
// <int> isCivilian - whether the unit to be hired should be a civilian unit
// <int>   hireSpy  - whether hire a spy or a normal military unit
//	<short>	destX	  - the x location the unit will move to
//	<short>	destY	  - the y location the unit will move to
//
// Note: Units hired in inns are military units only.
// 		There are no civilian units in inns.
//
// return: <int> recno of the unit recruited.
//
int Nation::hire_unit(int raceId, int isCivilian, int hireSpy, short destX, short destY)
{
	if( !ai_should_hire_unit(20) )			// 20 - importance rating
		return 0;

	//-------------------------------------------//

	FirmInn	*firmInnPtr;
	Firm		*firmPtr;
	InnUnit *innUnit;
	Skill		*innUnitSkill;
	int		i, j, innUnitCount, curRating, curFirmDist;
	int		bestRating=0, bestInnRecno=0, bestInnUnitId=0;
	int		destRegionId = world.get_region_id(destX, destY);

	for(i=0; i<ai_inn_count; i++)
	{
		firmPtr = firm_array[ai_inn_array[i]];

		if( firmPtr->region_id != destRegionId )
			continue;

		firmInnPtr = firmPtr->cast_to_FirmInn();

		innUnitCount=firmInnPtr->inn_unit_count;

		if( !innUnitCount )
			continue;

		innUnit = firmInnPtr->inn_unit_array + innUnitCount - 1;

		curFirmDist = m.points_distance(firmPtr->center_x, firmPtr->center_y, destX, destY);

		//------- check units in the inn ---------//

		for(j=innUnitCount; j>0; j--, innUnit--)
		{
			innUnitSkill = &(innUnit->skill);

			if( raceId && unit_res[innUnit->unit_id]->race_id != raceId )
				continue;

			if( isCivilian != unit_res[innUnit->unit_id]->is_civilian )
				continue;

			if( hireSpy && innUnit->spy_skill==0 )
				continue;

			if( cash < innUnit->hire_cost )
				continue;

			//----------------------------------------------//
			// evalute a unit on:
			// -its race, whether it's the same as the nation's race
			// -the inn's distance from the destination
			// -the skill level of the unit.
			//----------------------------------------------//

			curRating = (int) innUnit->skill_level()
							- (100-100*curFirmDist/MAX_WORLD_X_LOC);

			if( unit_res[innUnit->unit_id]->race_id == race_id )
				curRating += 50;

			if( curRating > bestRating )
			{
				bestRating = curRating;

				bestInnRecno  = firmInnPtr->firm_recno;
				bestInnUnitId = j;
			}
		}
	}

	//----------------------------------------------------//

	if( bestInnUnitId )
	{
		firmPtr = firm_array[bestInnRecno];
		firmInnPtr = firmPtr->cast_to_FirmInn();

		return firmInnPtr->hire(bestInnUnitId);
	}

	return 0;
}