Пример #1
0
//----- Begin of function Nation::ai_has_enough_food -----//
//
// return whether this nation has enough food or not.
//
int Nation::ai_has_enough_food()
{
	return food > 2000 + 2000 * pref_food_reserve / 100
			 || yearly_food_change() > 0;
}
Пример #2
0
//----- Begin of function Nation::think_request_buy_food -----//
//
int Nation::think_request_buy_food()
{
	//------ first see if we need to buy food ------//

	int yearFoodChange = yearly_food_change();
	int neededFoodLevel;

	if( yearFoodChange > 0 )
	{
		if( food > 0 )
			return 0;
		else
			neededFoodLevel = (int) -food;		// if the food is negative
	}
	else
	{
		neededFoodLevel = -yearFoodChange * (100+pref_food_reserve) / 50;

		if( food > neededFoodLevel )		// one to three times (based on pref_food_reserve) of the food needed in a year,
			return 0;
	}

	//----- think about which nation to buy food from -----//

	Nation  *nationPtr, *bestNation=NULL;
	int	  curRating, bestRating=0;
	int	  relationStatus;

	int i;
	for( i=1 ; i<=nation_array.size() ; i++ )
	{
		if( nation_array.is_deleted(i) || i==nation_recno )
			continue;

		nationPtr = nation_array[i];

		if( nationPtr->food < 500 )		// if the nation is short of food itself. The minimum request purchase qty is 500
			continue;

		relationStatus = get_relation_status(i);

		if( relationStatus == RELATION_HOSTILE || !get_relation(i)->has_contact )
			continue;

		if( nationPtr->yearly_food_change() < 0 &&
			 nationPtr->food < 1500 )
		{
			continue;
		}

		if( !should_diplomacy_retry(TALK_REQUEST_BUY_FOOD, i) )
			continue;

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

		curRating = relationStatus*20 +
						(int)nationPtr->food / 100 +
						(int)nationPtr->yearly_food_change() / 10;

		if( curRating > bestRating )
		{
			bestRating = curRating;
			bestNation = nationPtr;
		}
	}

	if( !bestNation )
		return 0;

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

	static short buyQtyArray[] = { 500, 1000, 2000, 4000 };

	int buyQty=0, buyPrice;

	for( i=3 ; i>=0 ; i-- )
	{
		if( bestNation->food/2 > buyQtyArray[i] )
		{
			buyQty = buyQtyArray[i];
			break;
		}
	}

	if( buyQty == 0 )
		return 0;

	//------- set the offering price ------//

	if( food < neededFoodLevel/4 )		// if we need the food badly
	{
		buyPrice = 30;
	}
	else if( food < neededFoodLevel/3 )
	{
		buyPrice = 20;
	}
	else
	{
		if( bestNation->food > bestNation->all_population() * UNIT_FOOD_YEAR_CONSUMPTION * 5 &&		// if the nation has plenty of food
			 bestNation->cash < bestNation->fixed_expense_365days() / 2 )										// if the nation runs short of cash
		{
			buyPrice = 5;
		}
		else
			buyPrice = 10;
	}

	talk_res.ai_send_talk_msg(bestNation->nation_recno, nation_recno, TALK_REQUEST_BUY_FOOD, buyQty, buyPrice);
	return 1;
}
Пример #3
0
//----- Begin of function Nation::consider_sell_food -----//
//
// talkMsg->talk_para1 - qty of food wanted to buy.
// talkMsg->talk_para2 - buying price offered for 10 food.
//
int Nation::consider_sell_food(TalkMsg* talkMsg)
{
	int relationStatus = get_relation_status(talkMsg->from_nation_recno);

	if( relationStatus == RELATION_HOSTILE )
		return 0;

	//--- if after selling the food, the remaining is not enough for its own consumption for ? years ---//

	float newFood = food-talkMsg->talk_para1;
	float yearConsumption = (float) yearly_food_consumption();
	int offeredAmount = talkMsg->talk_para2;
	int relationLevel = get_relation(talkMsg->from_nation_recno)->ai_relation_level;

	if( newFood < 1000 + 1000 * pref_food_reserve / 100 )
		return 0;

	if( relationLevel >= 50 )
		offeredAmount += 5;				// increase the chance of selling food

	else if( relationLevel < 30 )		// decrease the chance of selling food
		offeredAmount -=5 ;

	//---- if we run short of cash, we tend to accept the offer ---//

	float fixedExpense = fixed_expense_365days();

	if( cash < fixedExpense )
		offeredAmount += (int) (20 * (fixedExpense-cash) / fixedExpense);

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

	float reserveYears = (float) (100+pref_food_reserve) / 100;			// 1 to 2 years

	if( yearly_food_change() > 0 &&
		 newFood > yearConsumption * reserveYears )
	{
		if( offeredAmount >= 10 )		// offered >= $10
		{
			return 1;
		}
		else         	// < $10, only if we have plenty of reserve
		{
			if( newFood > yearConsumption * reserveYears * 2 )
				return 1;
		}
	}
	else
	{
		if( offeredAmount >= 20 )
		{
			if( yearly_food_change() > 0 &&
				 newFood > yearConsumption * reserveYears / 2 )
			{
				return 1;
			}
		}

		if( offeredAmount >= 30 )
		{
			return yearly_food_change() > 0 ||
					 newFood > yearConsumption * reserveYears;
		}
	}

	return 0;
}