From fc125bec37433c6d6fe059cdf240310c56a622cb Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 15 Mar 2012 07:09:36 +0000 Subject: [PATCH] - added command groups and unit selection to save / load game info --- source/glest_game/game/game.cpp | 3 + source/glest_game/gui/display.cpp | 28 +++++++- source/glest_game/gui/display.h | 1 + source/glest_game/gui/gui.cpp | 74 +++++++++++++++++++- source/glest_game/gui/gui.h | 1 + source/glest_game/gui/selection.cpp | 73 +++++++++++++++++-- source/glest_game/gui/selection.h | 2 + source/shared_lib/sources/xml/xml_parser.cpp | 2 +- 8 files changed, 177 insertions(+), 7 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 73fcbd476..7e80015f7 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -886,6 +886,9 @@ void Game::init(bool initForPreviewOnly) { gameCamera.setPos(Vec2f(v.x, v.y)); } } + else { + gui.loadGame(loadGameNode,&world); + } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); diff --git a/source/glest_game/gui/display.cpp b/source/glest_game/gui/display.cpp index 9376b55b9..f031c1109 100644 --- a/source/glest_game/gui/display.cpp +++ b/source/glest_game/gui/display.cpp @@ -153,6 +153,32 @@ void Display::saveGame(XmlNode *rootNode) const { // int upCellSideCount; // int upImageSize; // int maxUpIndex; - } + +void Display::loadGame(const XmlNode *rootNode) { + const XmlNode *displayNode = rootNode->getChild("Display"); + + // string title; + title = displayNode->getAttribute("title")->getValue(); + // string text; + text = displayNode->getAttribute("text")->getValue(); + // string infoText; + infoText = displayNode->getAttribute("infoText")->getValue(); + // const Texture2D *upImages[upCellCount]; + // const Texture2D *downImages[downCellCount]; + // bool downLighted[downCellCount]; + // const CommandType *commandTypes[downCellCount]; + // CommandClass commandClasses[downCellCount]; + // int progressBar; + progressBar = displayNode->getAttribute("progressBar")->getIntValue(); + // int downSelectedPos; + //displayNode->addAttribute("downSelectedPos",intToStr(downSelectedPos), mapTagReplacements); + // Vec4f colors[colorCount]; + // int currentColor; + //currentColor = displayNode->getAttribute("progressBar")->getIntValue(); + // int upCellSideCount; + // int upImageSize; + // int maxUpIndex; +} + }}//end namespace diff --git a/source/glest_game/gui/display.h b/source/glest_game/gui/display.h index ec7ca4f5f..7206ef745 100644 --- a/source/glest_game/gui/display.h +++ b/source/glest_game/gui/display.h @@ -104,6 +104,7 @@ public: int computeUpY(int index) const; void saveGame(XmlNode *rootNode) const; + void loadGame(const XmlNode *rootNode); private: void calculateUpDimensions(int index); diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 90eb251c3..affb168d1 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -1189,7 +1189,9 @@ void Gui::saveGame(XmlNode *rootNode) const { // //display // const UnitType *choosenBuildingType; if(choosenBuildingType != NULL) { + const Faction* thisFaction= world->getThisFaction(); guiNode->addAttribute("choosenBuildingType",choosenBuildingType->getName(), mapTagReplacements); + guiNode->addAttribute("choosenBuildingTypeFactionIndex",intToStr(thisFaction->getIndex()), mapTagReplacements); } // const CommandType *activeCommandType; if(activeCommandType != NULL) { @@ -1231,6 +1233,76 @@ void Gui::saveGame(XmlNode *rootNode) const { // const Object *selectedResourceObject; // // Texture2D* hudTexture; - } + +void Gui::loadGame(const XmlNode *rootNode, World *world) { + const XmlNode *guiNode = rootNode->getChild("Gui"); + + //External objects +// RandomGen random; + random.setLastNumber(guiNode->getAttribute("random")->getIntValue()); +// const Commander *commander; +// const World *world; +// const Game *game; +// GameCamera *gameCamera; +// Console *console; +// +// //Positions +// Vec2i posObjWorld; //world coords + //guiNode->addAttribute("posObjWorld",posObjWorld.getString(), mapTagReplacements); + posObjWorld = Vec2i::strToVec2(guiNode->getAttribute("posObjWorld")->getValue()); +// bool validPosObjWorld; + validPosObjWorld = guiNode->getAttribute("validPosObjWorld")->getIntValue(); +// //display +// const UnitType *choosenBuildingType; +// if(choosenBuildingType != NULL) { +// guiNode->addAttribute("choosenBuildingType",choosenBuildingType->getName(), mapTagReplacements); +// } + if(guiNode->hasAttribute("choosenBuildingType") == true) { + string unitType = guiNode->getAttribute("choosenBuildingType")->getValue(); + int factionIndex = guiNode->getAttribute("choosenBuildingTypeFactionIndex")->getIntValue(); + choosenBuildingType = world->getFaction(factionIndex)->getType()->getUnitType(unitType); + } +// const CommandType *activeCommandType; + //if(activeCommandType != NULL) { + // guiNode->addAttribute("activeCommandType",activeCommandType->getName(), mapTagReplacements); + //} + +// CommandClass activeCommandClass; + //guiNode->addAttribute("activeCommandClass",intToStr(activeCommandClass), mapTagReplacements); +// int activePos; + activePos = guiNode->getAttribute("activePos")->getIntValue(); +// int lastPosDisplay; + lastPosDisplay = guiNode->getAttribute("lastPosDisplay")->getIntValue(); +// //composite +// Display display; + display.loadGame(guiNode); +// Mouse3d mouse3d; +// Selection selection; + selection.loadGame(guiNode,world); +// SelectionQuad selectionQuad; +// int lastQuadCalcFrame; + lastQuadCalcFrame = guiNode->getAttribute("lastQuadCalcFrame")->getIntValue(); +// int selectionCalculationFrameSkip; + selectionCalculationFrameSkip = guiNode->getAttribute("selectionCalculationFrameSkip")->getIntValue(); +// int minQuadSize; + minQuadSize = guiNode->getAttribute("minQuadSize")->getIntValue(); +// Chrono lastGroupRecallTime; + //guiNode->addAttribute("lastGroupRecallTime",intToStr(lastGroupRecallTime.getMillis()), mapTagReplacements); +// int lastGroupRecall; + lastGroupRecall = guiNode->getAttribute("lastGroupRecall")->getIntValue(); +// //states +// bool selectingBuilding; + //selectingBuilding = guiNode->getAttribute("selectingBuilding")->getIntValue(); +// bool selectingPos; + //guiNode->addAttribute("selectingPos",intToStr(selectingPos), mapTagReplacements); +// bool selectingMeetingPoint; + //guiNode->addAttribute("selectingMeetingPoint",intToStr(selectingMeetingPoint), mapTagReplacements); +// CardinalDir selectedBuildingFacing; + //guiNode->addAttribute("selectedBuildingFacing",intToStr(selectedBuildingFacing), mapTagReplacements); +// const Object *selectedResourceObject; +// +// Texture2D* hudTexture; +} + }}//end namespace diff --git a/source/glest_game/gui/gui.h b/source/glest_game/gui/gui.h index 004ae3c5d..3a1b4d32c 100644 --- a/source/glest_game/gui/gui.h +++ b/source/glest_game/gui/gui.h @@ -200,6 +200,7 @@ public: void onSelectionChanged(); void saveGame(XmlNode *rootNode) const; + void loadGame(const XmlNode *rootNode, World *world); private: diff --git a/source/glest_game/gui/selection.cpp b/source/glest_game/gui/selection.cpp index 5723227bb..f2767c9a3 100644 --- a/source/glest_game/gui/selection.cpp +++ b/source/glest_game/gui/selection.cpp @@ -16,6 +16,7 @@ #include "unit_type.h" #include "gui.h" #include "config.h" +#include "world.h" #include "leak_dumper.h" using namespace std; @@ -229,20 +230,84 @@ void Selection::saveGame(XmlNode *rootNode) const { Unit *unit = selectedUnits[i]; XmlNode *selectedUnitsNode = selectionNode->addChild("selectedUnits"); - selectedUnitsNode->addAttribute("id",intToStr(unit->getId()), mapTagReplacements); + selectedUnitsNode->addAttribute("unitId",intToStr(unit->getId()), mapTagReplacements); } // UnitContainer groups[maxGroups]; for(unsigned int x = 0; x < maxGroups; ++x) { XmlNode *groupsNode = selectionNode->addChild("groups"); - for(unsigned int i = 0; i < selectedUnits.size(); i++) { - Unit *unit = selectedUnits[i]; + for(unsigned int i = 0; i < groups[x].size(); ++i) { + Unit *unit = groups[x][i]; XmlNode *selectedUnitsNode = groupsNode->addChild("selectedUnits"); - selectedUnitsNode->addAttribute("id",intToStr(unit->getId()), mapTagReplacements); + selectedUnitsNode->addAttribute("unitId",intToStr(unit->getId()), mapTagReplacements); } } // Gui *gui; } +void Selection::loadGame(const XmlNode *rootNode, World *world) { + const XmlNode *selectionNode = rootNode->getChild("Selection"); + +// vector fowPixmap1NodeList = minimapNode->getChildList("fowPixmap1"); +// for(unsigned int i = 0; i < fowPixmap1NodeList.size(); ++i) { +// XmlNode *fowPixmap1Node = fowPixmap1NodeList[i]; +// +// int pixelIndex = fowPixmap1Node->getAttribute("index")->getIntValue(); +// fowPixmap1->getPixels()[pixelIndex] = fowPixmap1Node->getAttribute("pixel")->getIntValue(); +// } + + // int factionIndex; + factionIndex = selectionNode->getAttribute("factionIndex")->getIntValue(); + // int teamIndex; + teamIndex = selectionNode->getAttribute("teamIndex")->getIntValue(); + // UnitContainer selectedUnits; +// for(unsigned int i = 0; i < selectedUnits.size(); i++) { +// Unit *unit = selectedUnits[i]; +// +// XmlNode *selectedUnitsNode = selectionNode->addChild("selectedUnits"); +// selectedUnitsNode->addAttribute("id",intToStr(unit->getId()), mapTagReplacements); +// } + vector selectedUnitsNodeList = selectionNode->getChildList("selectedUnits"); + for(unsigned int i = 0; i < selectedUnitsNodeList.size(); ++i) { + XmlNode *selectedUnitsNode = selectedUnitsNodeList[i]; + + int unitId = selectedUnitsNode->getAttribute("unitId")->getIntValue(); + Unit *unit = world->findUnitById(unitId); + //assert(unit != NULL); + //printf("#1 Unit [%s], group: %d\n",unit->getType()->getName().c_str(),i); + selectedUnits.push_back(unit); + } + + // UnitContainer groups[maxGroups]; +// for(unsigned int x = 0; x < maxGroups; ++x) { +// XmlNode *groupsNode = selectionNode->addChild("groups"); +// for(unsigned int i = 0; i < groups[x].size(); ++i) { +// Unit *unit = groups[x][i]; +// +// XmlNode *selectedUnitsNode = groupsNode->addChild("selectedUnits"); +// selectedUnitsNode->addAttribute("unitId",intToStr(unit->getId()), mapTagReplacements); +// } +// } + vector groupsNodeList = selectionNode->getChildList("groups"); + for(unsigned int i = 0; i < groupsNodeList.size(); ++i) { + XmlNode *groupsNode = groupsNodeList[i]; + + vector selectedGroupsUnitsNodeList = groupsNode->getChildList("selectedUnits"); + for(unsigned int j = 0; j < selectedGroupsUnitsNodeList.size(); ++j) { + XmlNode *selectedGroupsUnitsNode = selectedGroupsUnitsNodeList[j]; + + int unitId = selectedGroupsUnitsNode->getAttribute("unitId")->getIntValue(); + Unit *unit = world->findUnitById(unitId); + //assert(unit != NULL); + //printf("Unit #2 [%s], group: %d\n",unit->getType()->getName().c_str(),i); + groups[i].push_back(unit); + } + } + + // Gui *gui; + +} + + }}//end namespace diff --git a/source/glest_game/gui/selection.h b/source/glest_game/gui/selection.h index 4d867e76f..cc42d7f6d 100644 --- a/source/glest_game/gui/selection.h +++ b/source/glest_game/gui/selection.h @@ -21,6 +21,7 @@ using std::vector; namespace Glest{ namespace Game{ class Gui; +class World; // ===================================================== // class Selection @@ -81,6 +82,7 @@ public: virtual void unitEvent(UnitObserver::Event event, const Unit *unit); virtual void saveGame(XmlNode *rootNode) const; + void loadGame(const XmlNode *rootNode, World *world); }; }}//end namespace diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index 80c738a3f..0321e5f8c 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -367,7 +367,7 @@ void XmlIoRapid::save(const string &path, const XmlNode *node){ //xmlFile << xml_no_indent; // xmlFile << xml_as_string << '\0'; - xmlFile << doc << '\0'; + xmlFile << doc; #if defined(WIN32) && !defined(__MINGW32__) if(fp) {