SLIC-Reference : CreateCity
CTP2 Bureau
The Modding
Knowledgebase
Apolyton CTP2 forums
Apolyton CTP2 archives
Source Code Project SVN

SLIC-Reference : : CreateCity  

Arguments: owner, nearlocation, distance[, savecity] Type: int

Create a city belonging to owner, at approximately distance tiles away from nearlocation

If a city variable is passed in to savecity, it will contain the newly created city after this function returns. Example:
//when the first city is created (i.e. I settle a settler)

city_t my_city;
city_t new_city;

HandleEvent(CreateCity) post
{

    if(IsHumanPlayer(player[0]))
    {
        my_city = city[0];

        //I want to create a city about 5 tiles from my first city,
        //and then save the new city in a variable
        CreateCity(player[0], my_city.location, 5, new_city);

    }

}

tested   working  

Source:

SFN_ERROR Slic_CreateCity::Call(SlicArgList *args)
{
	if (args->Count() < 3 || args->Count() > 4)
		return SFN_ERROR_NUM_ARGS;

	sint32 owner;
	if(!args->GetPlayer(0, owner)) {
		return SFN_ERROR_TYPE_ARGS;
	}

	MapPoint pos;
	if(!args->GetPos(1, pos))
		return SFN_ERROR_TYPE_ARGS;

	sint32 distance;
	if(!args->GetInt(2, distance))
		return SFN_ERROR_TYPE_ARGS;

	sint32 x, y;
	BOOL found = FALSE;
	static DynamicArray legalPoints;
	legalPoints.Clear();

	sint16 origContinent = g_theWorld->GetContinent(pos);
	
	for(x = 0; x < g_theWorld->GetXWidth(); x++) {
		for(y = 0; y < g_theWorld->GetYHeight(); y++) {
			MapPoint chk(x, y);
			if(g_theWorld->GetContinent(chk) == origContinent &&
			   !g_theWorld->IsNextToCity(chk) &&
			   !g_theWorld->IsCity(chk)) {
				legalPoints.Insert(chk);
				found = TRUE;
			}
		}
	}

	if(!found) {
		m_result.m_int = 0;
		return SFN_ERROR_OK;
	}

	sint32 bestDistance = 0x7fffffff;
	sint32 bestDiff = 0x7fffffff;
	sint32 i;
	for(i = 0; i < legalPoints.Num(); i++) {
		sint32 dist = pos.NormalizedDistance(legalPoints[i]);
		if(abs(distance - dist) < bestDiff) {
			bestDiff = abs(distance - dist);
			bestDistance = dist;
		}
	}

	for(i = legalPoints.Num() - 1; i >= 0; i--) {
		sint32 dist = pos.NormalizedDistance(legalPoints[i]);
		if(dist != bestDistance) {
			legalPoints.DelIndex(i);
		}
	}

	MapPoint cpos;
	cpos = legalPoints[g_rand->Next(legalPoints.Num())];
	const UnitRecord *rec;
	for(i = 0; i < g_theUnitDB->NumRecords() - 1; i++) {
		rec = g_theUnitDB->Get(i, g_player[owner]->GetGovernmentType());
		if(!rec->GetHasPopAndCanBuild())
			continue;
		if((g_theWorld->IsLand(cpos) || g_theWorld->IsMountain(cpos)) && 
		   !g_theWorld->IsWater(cpos) && rec->GetMovementTypeLand())
			break;
		if(g_theWorld->IsWater(cpos) && rec->GetMovementTypeSea())
			break;
		if(g_theWorld->IsSpace(cpos) && rec->GetMovementTypeSpace())
			break;
	}
	if(i >= g_theUnitDB->NumRecords() - 1) {
		m_result.m_int = 0;
		return SFN_ERROR_OK;
	}

	Unit city = g_player[owner]->CreateCity(i, cpos,
											CAUSE_NEW_CITY_SCENARIO,
											NULL, -1);
	if(city.m_id == 0) {
		m_result.m_int = 0;
		return SFN_ERROR_OK;
	}

	m_result.m_int = 1;
	if (args->Count() > 3) {
		if(args->m_argValue[3].m_type == SA_TYPE_INT_VAR) {
			SlicSymbolData *sym = args->m_argValue[3].m_symbol;
			if(sym->GetType() == SLIC_SYM_CITY) {
				sym->SetCity(city);
			}
		}
	}
	return SFN_ERROR_OK;
}

Information last updated by BureauBert on 23.11.2010

User Comments:

Using this form you may add your own observations concerning the above item, e.g. additional hints for usage, limitations, oddities etc. Please note this is not the place for asking questions or carrying out discussions about modding - please refer to the Apolyton CTP2 Modification Forum on that purpose.

Subject / Title*:
Text:*
(A maximum of characters left)
Your name:*
Your website (URL):

*) Required fields

Error including libraries: Can't locate config.cfg in @INC (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.28.1 /usr/local/share/perl/5.28.1 /usr/lib/x86_64-linux-gnu/perl5/5.28 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.28 /usr/share/perl/5.28 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /home/www/ctp2.info/cgi-bin/comments.cgi line 4. Make sure they exist, permissions are set properly, and paths are set correctly.
  Administration
This site is currently maintained by: BureauBert (Webmaster)  Maquiladora (Manager)  Legal Notice: Legal Notice Statistics: 82005 unique users since 02.05.2004 (» Site Statistics) Hosted by: WebhostOne
Share this page:  VK Facebook Twitter LiveJournal OK MR Google+ LinkedIn tumblr Pinterest Blogger Digg Evernote