From 23deb957a74c608374002ec1436bee54e86ea0c3 Mon Sep 17 00:00:00 2001 From: Mike Hoffert Date: Sat, 19 Jul 2014 15:37:59 -0600 Subject: [PATCH] First, basic looting implementation Only absolute values are obtained for now, no loss. --- source/glest_game/types/unit_type.cpp | 26 ++++++++++++++++++ source/glest_game/types/unit_type.h | 35 ++++++++++++++++++++++++ source/glest_game/world/unit_updater.cpp | 7 +++++ 3 files changed, 68 insertions(+) diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index c8e0be10..6f3fb13d 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -592,6 +592,32 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree, } sortedItems.clear(); + // Lootable resources + if(parametersNode->hasChild("resources-death")) { + const XmlNode *deathResourcesNode= parametersNode->getChild("resources-death"); + + for(int i=0; i < deathResourcesNode->getChildCount(); ++i){ + const XmlNode *resourceNode= deathResourcesNode->getChild("resource", i); + + // TODO: Add rest of attributes and make appropriate ones optional + string name= resourceNode->getAttribute("name")->getRestrictedValue(); + int amountValue= resourceNode->getAttribute("amount-value")->getIntValue(); + + LootableResource resource; + resource.setResourceType(techTree->getResourceType(name)); + resource.setAmountValue(amountValue); + + lootableResources.push_back(resource); + + // TODO: Add checks for duplicate resources + } + } + + // TODO: For debug purposes only -- remove + for(int i = 0; i < lootableResources.size(); i++) { + printf("Lootable resource %s has amount %d\n", lootableResources[i].getResourceType()->getName().c_str(), lootableResources[i].getAmountValue()); + } + //image const XmlNode *imageNode= parametersNode->getChild("image"); image= Renderer::getInstance().newTexture2D(rsGame); diff --git a/source/glest_game/types/unit_type.h b/source/glest_game/types/unit_type.h index d623eec0..1635802f 100644 --- a/source/glest_game/types/unit_type.h +++ b/source/glest_game/types/unit_type.h @@ -63,6 +63,37 @@ public: static const Level * loadGame(const XmlNode *rootNode, const UnitType *ut); }; +// =============================== +// class LootResource +// +/// Stores information about a lootable resource. Lootable resources are stolen by the attacker on death. +// =============================== + +class LootableResource { +private: + const ResourceType *type; + int amountValue; + double amountPercentage; + int lossValue; + double lossPercentage; + +public: + const ResourceType* getResourceType() {return type;} + void setResourceType(const ResourceType *type) {this->type=type;} + + int getAmountValue() {return amountValue;} + void setAmountValue(int amountValue) {this->amountValue=amountValue;} + + double getAmountPercentage() {return amountPercentage;} + void setAmountPercentage(double amountPercentage) {this->amountPercentage=amountPercentage;} + + int getLossValue() {return lossValue;} + void setLossValue(int lossValue) {this->lossValue=lossValue;} + + double getLossPercentage() {return lossPercentage;} + void setLossPercentage(double lossPercentage) {this->lossPercentage=lossPercentage;} +}; + // =============================== // class UnitType // @@ -104,6 +135,7 @@ private: typedef vector CommandTypes; typedef vector StoredResources; typedef vector Levels; + typedef vector LootableResources; private: //basic @@ -151,6 +183,7 @@ private: CommandTypes commandTypes; StoredResources storedResources; Levels levels; + LootableResources lootableResources; //meeting point bool meetingPoint; @@ -220,6 +253,8 @@ public: int getHeight() const {return height;} int getStoredResourceCount() const {return (int)storedResources.size();} inline const Resource *getStoredResource(int i) const {return &storedResources[i];} + int getLootableResourceCount() const {return lootableResources.size();} + inline const LootableResource getLootableResource(int i) const {return lootableResources.at(i);} bool getCellMapCell(int x, int y, CardinalDir facing) const; inline bool getMeetingPoint() const {return meetingPoint;} inline bool getCountUnitDeathInStats() const {return countUnitDeathInStats;} diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index de9d6560..4ee4a4b6 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -2573,6 +2573,13 @@ void UnitUpdater::damage(Unit *attacker, const AttackSkillType* ast, Unit *attac attacker->incKills(attacked->getTeam()); } + // TODO: Add looting here + int lootableResourceCount = attacked->getType()->getLootableResourceCount(); + for(int i = 0; i < lootableResourceCount; i++) { + LootableResource resource = attacked->getType()->getLootableResource(i); + attacker->getFaction()->incResourceAmount(resource.getResourceType(), resource.getAmountValue()); + } + switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: break;