Source Code Snippets


#1

Not sure how interesting or helpful this is, but I think I might as well share some stuff. This is the code that shows how voters get assigned at the games start. Feel free to ask questions, or suggest improvements or spot bugs :smiley: (I can’t answer everything due to time being megabusy though). The code has been simplified to make it easier to read, by removing some debugging and error checking stuff

Heres the bit that basically creates the voters:

for(int n = 0; n < TotalVoters; n++) { SIM_Voter* pvoter = new SIM_Voter(); AddDummyConnections(pvoter); } std::list<SIM_VoterType*>::iterator it; for(it = Types.begin(); it != Types.end(); ++it) { SIM_VoterType* ptype = *it; sprintf_s(output,MAXSTRING,"Adding membership links (%s)",ptype->GetGUIName()); GUI_GetProcessingScreen()->Message(output,0.01f); GetGame()->DoUrgentWork(); ptype->AddMostSuitable(); }

the add most suitable bit does this:

[code] int quota = (int)((float)SIM_VoterManager::TotalVoters * GetPercentageNeuron()->GetValue());
if(quota <= 0)
{
return;
}
//build a list of suitabilities

std::vector<VoterTypeSuitability> tempvector;
std::list<SIM_Voter*>::iterator vit = SIM_Voter::GlobalVoters.begin();
std::list<SIM_Voter*>::iterator vitend = SIM_Voter::GlobalVoters.end();
for(; vit != vitend; ++vit)
{
	SIM_Voter* pvoter = *vit;
	VoterTypeSuitability suit;
	suit.pvoter = pvoter;
	suit.suitability = GRandom::RandUnitFloat() + pvoter->GetSuitabilityScore(this);
	tempvector.push_back(suit);
}

//sort the list once
sort(tempvector.begin(),tempvector.end(),CompareSuitabilities);

//take the highest ones and add them	
float join_level = GETGLOBAL("VOTER_GROUP_MEMBERSHIP_THRESHHOLD");
std::vector<VoterTypeSuitability>::iterator it = tempvector.begin();
std::vector<VoterTypeSuitability>::iterator endit = tempvector.end();
for(; it != endit; ++it)
{
	VoterTypeSuitability* psuit = &(*it);
	SIM_Voter* pvoter = psuit->pvoter;
	if(strcmp(Name,"_All_") == 0)
	{
		AddVoter(pvoter,1.0f);
	}
	else
	{
		AddVoter(pvoter,GRandom::RandReal(join_level,1.0f));
	}
	if(Members.size() >= quota)
	{
		tempvector.clear();
		return;
	}
}

//clean up memory
tempvector.clear();

[/code]

probably no help to most players of the game, but if anyone knows some C++ or other coding it might be of interest.