From 66326fe6df68dfcb4a4d4388c60ae60689e6090d Mon Sep 17 00:00:00 2001 From: Warboy1982 Date: Sun, 26 Apr 2015 00:03:42 +1000 Subject: [PATCH] randomize deployment type for missionSites also refactor mission site code into a function --- bin/data/Ruleset/Xcom1Ruleset/globe.rul | 3 +- src/Ruleset/RuleGlobe.cpp | 2 +- src/Ruleset/Texture.cpp | 57 +++++++++++++++++++++-- src/Ruleset/Texture.h | 6 ++- src/Savegame/AlienMission.cpp | 60 ++++++++++++++++--------- src/Savegame/AlienMission.h | 5 +++ 6 files changed, 104 insertions(+), 29 deletions(-) diff --git a/bin/data/Ruleset/Xcom1Ruleset/globe.rul b/bin/data/Ruleset/Xcom1Ruleset/globe.rul index 5e71044fe7..d7c2fbe687 100644 --- a/bin/data/Ruleset/Xcom1Ruleset/globe.rul +++ b/bin/data/Ruleset/Xcom1Ruleset/globe.rul @@ -86,4 +86,5 @@ globe: terrain: - name: POLAR - id: -1 - deployment: STR_TERROR_MISSION \ No newline at end of file + deployments: + STR_TERROR_MISSION: 100 \ No newline at end of file diff --git a/src/Ruleset/RuleGlobe.cpp b/src/Ruleset/RuleGlobe.cpp index 1c82ecc422..a8d4273ba5 100644 --- a/src/Ruleset/RuleGlobe.cpp +++ b/src/Ruleset/RuleGlobe.cpp @@ -225,7 +225,7 @@ std::vector RuleGlobe::getTerrains(const std::string &deployment) c std::vector terrains; for (std::map::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::const_iterator j = i->second->getTerrain()->begin(); j != i->second->getTerrain()->end(); ++j) { diff --git a/src/Ruleset/Texture.cpp b/src/Ruleset/Texture.cpp index 51b228e08d..80d6cc3f75 100644 --- a/src/Ruleset/Texture.cpp +++ b/src/Ruleset/Texture.cpp @@ -45,7 +45,7 @@ Texture::~Texture() void Texture::load(const YAML::Node &node) { _id = node["id"].as(_id); - _deployment = node["deployment"].as(_deployment); + _deployments = node["deployments"].as >(_deployments); _terrain = node["terrain"].as< std::vector >(_terrain); } @@ -62,7 +62,8 @@ std::vector *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 { @@ -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 &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::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 ""; +} } diff --git a/src/Ruleset/Texture.h b/src/Ruleset/Texture.h index 5e983db4c3..9a8e1037a8 100644 --- a/src/Ruleset/Texture.h +++ b/src/Ruleset/Texture.h @@ -46,7 +46,7 @@ class Texture { private: int _id; - std::string _deployment; + std::map _deployments; std::vector _terrain; public: /// Creates a new texture with mission data. @@ -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 &getDeployments(); + /// Gets a random deployment. + std::string getRandomDeployment() const; }; } diff --git a/src/Savegame/AlienMission.cpp b/src/Savegame/AlienMission.cpp index bd23103721..966d4143ce 100644 --- a/src/Savegame/AlienMission.cpp +++ b/src/Savegame/AlienMission.cpp @@ -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::iterator t = ufo.getFollowers()->begin(); t != ufo.getFollowers()->end();) + MissionSite *missionSite = spawnMissionSite(game, rules, area); + if (missionSite) { - Craft* c = dynamic_cast(*t); - if (c && c->getNumSoldiers() != 0) + game.getMissionSites()->push_back(missionSite); + for (std::vector::iterator t = ufo.getFollowers()->begin(); t != ufo.getFollowers()->end();) { - c->setDestination(missionSite); - t = ufo.getFollowers()->begin(); - } - else - { - ++t; + Craft* c = dynamic_cast(*t); + if (c && c->getNumSoldiers() != 0) + { + c->setDestination(missionSite); + t = ufo.getFollowers()->begin(); + } + else + { + ++t; + } } } } @@ -689,4 +682,29 @@ std::pair 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; +} } diff --git a/src/Savegame/AlienMission.h b/src/Savegame/AlienMission.h index f5e98ecb5c..f1f94013c0 100644 --- a/src/Savegame/AlienMission.h +++ b/src/Savegame/AlienMission.h @@ -35,6 +35,8 @@ class RuleRegion; struct MissionWave; class UfoTrajectory; class AlienBase; +class MissionSite; +struct MissionArea; /** * Represents an ongoing alien mission. @@ -116,6 +118,9 @@ class AlienMission std::pair getWaypoint(const UfoTrajectory &trajectory, const size_t nextWaypoint, const Globe &globe, const RuleRegion ®ion); /// Get a random landing point inside the given region zone. std::pair getLandPoint(const Globe &globe, const RuleRegion ®ion, size_t zone); + /// Spawns a MissionSite at a specific location. + MissionSite *spawnMissionSite(SavedGame &game, const Ruleset &rules, const MissionArea &area); + }; }