Пример #1
0
//----- Begin of function TownArray::think_new_independent_town -----//
//
// Think about creating new independent towns.
//
void TownArray::think_new_independent_town()
{
	if( m.random(3) != 0 )		// 1/3 chance
		return;

	//---- count the number of independent towns ----//

	Town* townPtr;
	int   independentTownCount=0, allTotalPop=0;

	int i;
	for( i=town_array.size() ; i>0 ; i-- )
	{
		if( town_array.is_deleted(i) )
			continue;

		townPtr = town_array[i];

		allTotalPop += townPtr->population;

		if( townPtr->nation_recno == 0 )
			independentTownCount++;
	}

	if( independentTownCount >= 10 )		// only when the no. of independent town is less than 10
		return;

	//--- if the total population of all nations combined > 1000, then no new independent town will emerge ---//

	if( allTotalPop > 1000 )
		return;

	//--- add 1 to 2 wanderer per month per race ---//

	for( i=0 ; i<MAX_RACE ; i++ )
	{
		race_wander_pop_array[i] += 2+m.random(5);
	}

	//----- check if there are enough wanderers to set up a new town ---//

	int raceId = m.random(MAX_RACE)+1;

	for( i=0 ; i<MAX_RACE ; i++ )
	{
		if( ++raceId > MAX_RACE )
			raceId = 1;

		if( race_wander_pop_array[raceId-1] >= 10 )	// one of the race must have at least 10 people
			break;
	}

	if( i==MAX_RACE )
		return;

	//------- locate for a space to build the town ------//

	int xLoc, yLoc;

	if( !think_town_loc(MAX_WORLD_X_LOC*MAX_WORLD_Y_LOC/4, xLoc, yLoc) )
		return;

	//--------------- create town ---------------//

	int townRecno  = town_array.add_town(0, raceId, xLoc, yLoc);
	int maxTownPop = 20 + m.random(10);
	int addPop, townResistance;
	int loopCount=0;

	townPtr = town_array[townRecno];

	while(1)
	{
		err_when( loopCount++ > 100 );

		addPop = race_wander_pop_array[raceId-1];
		addPop = MIN(maxTownPop-townPtr->population, addPop);

		townResistance = independent_town_resistance();

		townPtr->init_pop( raceId, addPop, townResistance, 0, 1 );		// 0-the add pop do not have jobs, 1-first init

		race_wander_pop_array[raceId-1] -= addPop;

		err_when( race_wander_pop_array[raceId-1] < 0 );

		if( townPtr->population >= maxTownPop )
			break;

		//---- next race to be added to the independent town ----//

		raceId = m.random(MAX_RACE)+1;

		for( i=0 ; i<MAX_RACE ; i++ )
		{
			if( ++raceId > MAX_RACE )
				raceId = 1;

			if( race_wander_pop_array[raceId-1] >= 5 )
				break;
		}

		if( i==MAX_RACE )		// no suitable race
			break;
	}

	//---------- set town layout -----------//

	townPtr->auto_set_layout();

//	if( sys.debug_session )
//		box.msg( "A new independent town has emerged." );
}
Пример #2
0
//-------- Begin of function Battle::create_town --------//
//
// <int> nationRecno = the nation recno of the town
// <int> raceId      = the race id. of the town
//
// <int&> xLoc = for the starting location of the town
// <int&> yLoc = for the starting location of the town
//
// return: <int> townRecno - >0  the recno of the town created
//                           ==0 no town created
//
int Battle::create_town(int nationRecno, int raceId, int& xLoc, int& yLoc)
{
	//------- locate for a space to build the town ------//

	if( !town_array.think_town_loc(MAX_WORLD_X_LOC*MAX_WORLD_Y_LOC, xLoc, yLoc) )
		return 0;

	//--------------- create town ---------------//

	int townRecno = town_array.add_town(nationRecno, raceId, xLoc, yLoc);

	Town* townPtr = town_array[townRecno];

	//--------- no. of mixed races ---------//

	if( nationRecno )
	{
		int initPop;

		if( config.random_start_up )
			initPop = 25 + misc.random(26);		// 25 to 50
		else
			initPop = 40;

		Town* townPtr = town_array[townRecno];

		townPtr->init_pop( raceId, initPop, 100, 0, 1 );		// 100-startup loyalty, last 1-first initialization at the beginning of the game
	}
	else
	{
		int mixedRaceCount;

		if( nationRecno )
			mixedRaceCount = 1;
		else
			mixedRaceCount= misc.random(3)+1;		// 1 to 3 mixed races

		int curPop, totalPop=0, townResistance;

		for( int i=0 ; i<mixedRaceCount ; i++ )
		{
			if(totalPop>=MAX_TOWN_POPULATION)
				break;

			townResistance = town_array.independent_town_resistance();

			if( i==0 )
			{
				curPop = 15/mixedRaceCount + misc.random(15/mixedRaceCount);
				if(curPop>=MAX_TOWN_POPULATION)
					curPop = MAX_TOWN_POPULATION;

				err_when(curPop==0);
				townPtr->init_pop( raceId, curPop, townResistance, 0, 1 ); 	// last 1-first initialization at the beginning of the game
				totalPop += curPop;
			}
			else
			{
				curPop = 10/mixedRaceCount + misc.random(10/mixedRaceCount);
				if(curPop>=MAX_TOWN_POPULATION-totalPop)
					curPop = MAX_TOWN_POPULATION-totalPop;

				err_when(curPop==0);
				townPtr->init_pop( misc.random(MAX_RACE)+1, curPop, townResistance, 0, 1 );
				totalPop += curPop;
			}
		}
	}

	//---------- set town layout -----------//

	townPtr->auto_set_layout();

	return townRecno;
}
Пример #3
0
// ----- begin of function ScenarioEditor::detect_human_view ------//
//
int ScenarioEditor::detect_human_view()
{
	if( power.command_id )
		return 0;

	// detect map area

	if( mouse.double_click( ZOOM_X1, ZOOM_Y1, ZOOM_X2, ZOOM_Y2, 0 ) )	// left click
	{
		int xLoc, yLoc;
		if( world.zoom_matrix->get_detect_location(mouse.click_x(0), mouse.click_y(0),
			&xLoc, &yLoc, NULL) )
		{
			if( unit_or_struct == 0 && !vbrowse_unit_id.none_record )
			{
				// put unit human_unit_id_array[human_unit_id_browse_recno-1]
				// on (xLoc, yLoc)
				int unitId = human_unit_id_array[human_unit_id_browse_recno-1];
				UnitInfo *unitInfo = unit_res[unitId];
				SpriteInfo *spriteInfo = sprite_res[unitInfo->sprite_id];
				if( world.check_unit_space(xLoc, yLoc, 
					xLoc+spriteInfo->loc_width-1, yLoc+spriteInfo->loc_height-1, unitInfo->mobile_type) )
				{
					int unitRecno = unit_array.add_unit( unitId, brush_player_recno, RANK_SOLDIER, 100, xLoc, yLoc );

					if( unitRecno && unitId == UNIT_WAGON )
					{
						err_when( !human_race_filter );
						UnitWagon* unitPtr = (UnitWagon *)unit_array[unitRecno];
						unitPtr->race_id = human_race_filter;
						unitPtr->set_pop( MAX_WAGON_POPULATION );
					}
				}
			}
			else if( unit_or_struct == 1 && !vbrowse_firm_build.none_record )
			{
				err_when( human_firm_group_array[human_firm_group_browse_recno] == 0 );
				if( human_firm_group_array[human_firm_group_browse_recno-1] < 0 )
				{
					// put town of race -human_firm_group_array[human_firm_group_browse_recno]
					// (xLoc, yLoc)

					if( world.can_build_town(xLoc, yLoc, 0) )
					{
						int raceId = -human_firm_group_array[human_firm_group_browse_recno-1];
						int townRecno = town_array.generate_town(brush_player_recno, raceId, xLoc, yLoc);
						if( townRecno )
						{
							Town *townPtr = town_array[townRecno];
							townPtr->construction_completion_percent = 100;
							townPtr->under_construction = 0;

							// init_pop
							if( brush_player_recno )
								townPtr->init_pop( 0, 100, 0, 1 );
							else
								townPtr->init_pop( 0, town_array.independent_town_resistance(), 0, 1 );

							townPtr->auto_set_layout();
						}
					}
				}
				else
				{
					// put firm group human_firm_group_array[human_firm_group_browse_recno-1]
					// on (xLoc, yLoc)

					FirmGroup *firmGroup = firm_res.get_group(human_firm_group_array[human_firm_group_browse_recno-1]);
					if( world.can_build_firm( xLoc, yLoc, firmGroup->firm_id, 0) )
					{
						if (brush_player_recno == 0)
						{
							box.msg( text_editor.str_no_independent() ); // "Cannot add any Independent structures" );
						}
						else
						{
							if( firmGroup->firm_id == FIRM_BASE )		// if it's a seat of power, give the nation a scroll of power first
								god_res[firmGroup->race_id]->enable_know(brush_player_recno);

							firm_array.generate_firm( xLoc, yLoc, brush_player_recno, firmGroup->firm_id, firmGroup->race_code );
						}
					}
				}
			}
		}
		return 1;
	}

	if( mouse.double_click( ZOOM_X1, ZOOM_Y1, ZOOM_X2, ZOOM_Y2, 1) )	// right click to remove
	{
		int xLoc, yLoc;
		char mobileType;
		Location* locPtr = power.test_detect(mouse.click_x(1), mouse.click_y(1), &mobileType, &xLoc, &yLoc );
		if( locPtr )
		{
			if( locPtr->unit_recno(mobileType) )		// remove unit
			{
				int unitRecno = locPtr->unit_recno(mobileType);
				if( !unit_array.is_deleted(unitRecno) )
				{
					unit_array[unitRecno]->hit_points = 0.0f;
				}
			}
			else if( locPtr->is_firm() )			// remove firm
			{
				int firmRecno = locPtr->firm_recno();
				if( !firm_array.is_deleted(firmRecno) )
				{
					firm_array.del_firm( firmRecno );
				}
			}
			else if( locPtr->is_town() )			// remove town
			{
				int townRecno = locPtr->town_recno();
				if( !town_array.is_deleted(townRecno) )
				{
					town_array.del_town(townRecno);
				}
			}
		}
		return 1;
	}

	return 0;
}