Skip to content

Commit

Permalink
randomize deployment type for missionSites
Browse files Browse the repository at this point in the history
also refactor mission site code into a function
  • Loading branch information
Warboy1982 committed Apr 27, 2015
1 parent 8f9f926 commit 66326fe
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 29 deletions.
3 changes: 2 additions & 1 deletion bin/data/Ruleset/Xcom1Ruleset/globe.rul
Expand Up @@ -86,4 +86,5 @@ globe:
terrain:
- name: POLAR
- id: -1
deployment: STR_TERROR_MISSION
deployments:
STR_TERROR_MISSION: 100
2 changes: 1 addition & 1 deletion src/Ruleset/RuleGlobe.cpp
Expand Up @@ -225,7 +225,7 @@ std::vector<std::string> RuleGlobe::getTerrains(const std::string &deployment) c
std::vector<std::string> terrains;
for (std::map<int, Texture*>::const_iterator i = _textures.begin(); i != _textures.end(); ++i)
{
if (i->second->getDeployment() == deployment)
if ((deployment == "" && i->second->getDeployments().empty()) || i->second->getDeployments().find(deployment) != i->second->getDeployments().end())
{
for (std::vector<TerrainCriteria>::const_iterator j = i->second->getTerrain()->begin(); j != i->second->getTerrain()->end(); ++j)
{
Expand Down
57 changes: 53 additions & 4 deletions src/Ruleset/Texture.cpp
Expand Up @@ -45,7 +45,7 @@ Texture::~Texture()
void Texture::load(const YAML::Node &node)
{
_id = node["id"].as<int>(_id);
_deployment = node["deployment"].as<std::string>(_deployment);
_deployments = node["deployments"].as<std::map<std::string, int> >(_deployments);
_terrain = node["terrain"].as< std::vector<TerrainCriteria> >(_terrain);
}

Expand All @@ -62,7 +62,8 @@ std::vector<TerrainCriteria> *Texture::getTerrain()
/**
* Calculates a random terrain for a mission target based
* on the texture's available terrain criteria.
* @target Pointer to the mission target.
* @param target Pointer to the mission target.
* @return the name of the picked terrain.
*/
std::string Texture::getRandomTerrain(Target *target) const
{
Expand All @@ -89,9 +90,57 @@ std::string Texture::getRandomTerrain(Target *target) const
return "";
}

std::string Texture::getDeployment() const
/**
* Returns the list of deployments associated
* with this texture.
* @return List of deployments.
*/
const std::map<std::string, int> &Texture::getDeployments()
{
return _deployment;
return _deployments;
}

/**
* Calculates a random deployment for a mission target based
* on the texture's available deployments.
* @return the name of the picked deployment.
*/
std::string Texture::getRandomDeployment() const
{
if (_deployments.empty())
{
return "";
}

std::map<std::string, int>::const_iterator i = _deployments.begin();

if (_deployments.size() == 1)
{
return i->first;
}
int totalWeight = 0;

for (; i != _deployments.end(); ++i)
{
totalWeight += i->second;
}

if (totalWeight >= 1)
{
int pick = RNG::generate(1, totalWeight);
for (i = _deployments.begin(); i != _deployments.end(); ++i)
{
if (pick <= i->second)
{
return i->first;
}
else
{
pick -= i->second;
}
}
}

return "";
}
}
6 changes: 4 additions & 2 deletions src/Ruleset/Texture.h
Expand Up @@ -46,7 +46,7 @@ class Texture
{
private:
int _id;
std::string _deployment;
std::map<std::string, int> _deployments;
std::vector<TerrainCriteria> _terrain;
public:
/// Creates a new texture with mission data.
Expand All @@ -60,7 +60,9 @@ class Texture
/// Gets a random texture terrain for a given target.
std::string getRandomTerrain(Target *target) const;
/// Gets the alien deployment for this texture.
std::string getDeployment() const;
const std::map<std::string, int> &getDeployments();
/// Gets a random deployment.
std::string getRandomDeployment() const;
};

}
Expand Down
60 changes: 39 additions & 21 deletions src/Savegame/AlienMission.cpp
Expand Up @@ -407,29 +407,22 @@ void AlienMission::ufoReachedWaypoint(Ufo &ufo, Game &engine, const Globe &globe
ufo.setStatus(Ufo::DESTROYED);

MissionArea area = regionRules.getMissionPoint(trajectory.getZone(curWaypoint), &ufo);
Texture *texture = rules.getGlobe()->getTexture(area.texture);
AlienDeployment *deployment = rules.getDeployment(texture->getDeployment());

MissionSite *missionSite = new MissionSite(&_rule, deployment);
missionSite->setLongitude(ufo.getLongitude());
missionSite->setLatitude(ufo.getLatitude());
missionSite->setId(game.getId(deployment->getMarkerName()));
missionSite->setSecondsRemaining(RNG::generate(deployment->getDurationMin(), deployment->getDurationMax()) * 3600);
missionSite->setAlienRace(_race);
missionSite->setTexture(area.texture);
missionSite->setCity(area.name);
game.getMissionSites()->push_back(missionSite);
for (std::vector<Target*>::iterator t = ufo.getFollowers()->begin(); t != ufo.getFollowers()->end();)
MissionSite *missionSite = spawnMissionSite(game, rules, area);
if (missionSite)
{
Craft* c = dynamic_cast<Craft*>(*t);
if (c && c->getNumSoldiers() != 0)
game.getMissionSites()->push_back(missionSite);
for (std::vector<Target*>::iterator t = ufo.getFollowers()->begin(); t != ufo.getFollowers()->end();)
{
c->setDestination(missionSite);
t = ufo.getFollowers()->begin();
}
else
{
++t;
Craft* c = dynamic_cast<Craft*>(*t);
if (c && c->getNumSoldiers() != 0)
{
c->setDestination(missionSite);
t = ufo.getFollowers()->begin();
}
else
{
++t;
}
}
}
}
Expand Down Expand Up @@ -689,4 +682,29 @@ std::pair<double, double> AlienMission::getLandPoint(const Globe &globe, const R

}

/**
* Attempt to spawn a Mission Site at a given location.
* @param game reference to the saved game.
* @param rules reference to the game rules.
* @param area the point on the globe at which to spawn this site.
* @return a pointer to the mission site.
*/
MissionSite *AlienMission::spawnMissionSite(SavedGame &game, const Ruleset &rules, const MissionArea &area)
{
Texture *texture = rules.getGlobe()->getTexture(area.texture);
AlienDeployment *deployment = rules.getDeployment(texture->getRandomDeployment());
if (deployment)
{
MissionSite *missionSite = new MissionSite(&_rule, deployment);
missionSite->setLongitude(area.lonMin);
missionSite->setLatitude(area.latMin);
missionSite->setId(game.getId(deployment->getMarkerName()));
missionSite->setSecondsRemaining(RNG::generate(deployment->getDurationMin(), deployment->getDurationMax()) * 3600);
missionSite->setAlienRace(_race);
missionSite->setTexture(area.texture);
missionSite->setCity(area.name);
return missionSite;
}
return 0;
}
}
5 changes: 5 additions & 0 deletions src/Savegame/AlienMission.h
Expand Up @@ -35,6 +35,8 @@ class RuleRegion;
struct MissionWave;
class UfoTrajectory;
class AlienBase;
class MissionSite;
struct MissionArea;

/**
* Represents an ongoing alien mission.
Expand Down Expand Up @@ -116,6 +118,9 @@ class AlienMission
std::pair<double, double> getWaypoint(const UfoTrajectory &trajectory, const size_t nextWaypoint, const Globe &globe, const RuleRegion &region);
/// Get a random landing point inside the given region zone.
std::pair<double, double> getLandPoint(const Globe &globe, const RuleRegion &region, size_t zone);
/// Spawns a MissionSite at a specific location.
MissionSite *spawnMissionSite(SavedGame &game, const Ruleset &rules, const MissionArea &area);

};

}
Expand Down

0 comments on commit 66326fe

Please sign in to comment.