2010-03-25 13:15:10 +01:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
//
|
2011-12-14 08:40:48 +01:00
|
|
|
// Copyright (C) 2001-2008 Martiño Figueroa
|
2010-03-25 13:15:10 +01:00
|
|
|
//
|
2011-01-02 01:39:13 +01:00
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
2010-03-25 13:15:10 +01:00
|
|
|
// License, or (at your option) any later version
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
#include "command.h"
|
|
|
|
|
|
|
|
#include "command_type.h"
|
2010-05-01 11:27:08 +02:00
|
|
|
#include "util.h"
|
2010-05-18 05:53:57 +02:00
|
|
|
#include "conversion.h"
|
2010-05-29 12:34:56 +02:00
|
|
|
#include "unit_type.h"
|
2012-03-13 16:21:25 +01:00
|
|
|
#include "faction.h"
|
|
|
|
#include "world.h"
|
2010-05-18 05:53:57 +02:00
|
|
|
#include "leak_dumper.h"
|
2010-05-01 11:27:08 +02:00
|
|
|
|
|
|
|
using namespace Shared::Util;
|
2010-03-25 13:15:10 +01:00
|
|
|
|
|
|
|
namespace Glest{ namespace Game{
|
|
|
|
|
|
|
|
// =====================================================
|
|
|
|
// class Command
|
|
|
|
// =====================================================
|
2012-10-20 09:15:13 +02:00
|
|
|
Command::Command() : unitRef() {
|
2012-03-13 16:21:25 +01:00
|
|
|
this->commandType= NULL;
|
|
|
|
unitType= NULL;
|
|
|
|
stateType = cst_None;
|
|
|
|
stateValue = -1;
|
|
|
|
unitCommandGroupId = -1;
|
|
|
|
}
|
2010-03-25 13:15:10 +01:00
|
|
|
|
2012-10-20 09:15:13 +02:00
|
|
|
Command::Command(const CommandType *ct, const Vec2i &pos) :unitRef() {
|
2011-01-17 17:31:25 +01:00
|
|
|
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] ct = [%p]\n",__FILE__,__FUNCTION__,__LINE__,ct);
|
2010-05-01 11:27:08 +02:00
|
|
|
|
2011-01-02 01:39:13 +01:00
|
|
|
this->commandType= ct;
|
2010-03-25 13:15:10 +01:00
|
|
|
this->pos= pos;
|
2011-03-25 21:51:14 +01:00
|
|
|
this->originalPos = this->pos;
|
2010-03-25 13:15:10 +01:00
|
|
|
unitType= NULL;
|
2010-08-30 22:45:12 +02:00
|
|
|
stateType = cst_None;
|
|
|
|
stateValue = -1;
|
2011-07-05 06:37:35 +02:00
|
|
|
unitCommandGroupId = -1;
|
2011-01-02 01:39:13 +01:00
|
|
|
}
|
2010-03-25 13:15:10 +01:00
|
|
|
|
2011-01-02 01:39:13 +01:00
|
|
|
Command::Command(const CommandType *ct, Unit* unit) {
|
|
|
|
this->commandType= ct;
|
2010-03-25 13:15:10 +01:00
|
|
|
this->pos= Vec2i(0);
|
2011-03-25 21:51:14 +01:00
|
|
|
this->originalPos = this->pos;
|
2010-03-25 13:15:10 +01:00
|
|
|
this->unitRef= unit;
|
|
|
|
unitType= NULL;
|
2010-08-30 22:45:12 +02:00
|
|
|
if(unit!=NULL) {
|
2012-04-05 00:57:32 +02:00
|
|
|
//unit->resetHighlight(); is in gui now
|
2010-03-25 13:15:10 +01:00
|
|
|
pos= unit->getCellPos();
|
|
|
|
}
|
2010-08-30 22:45:12 +02:00
|
|
|
stateType = cst_None;
|
|
|
|
stateValue = -1;
|
2011-07-05 06:37:35 +02:00
|
|
|
unitCommandGroupId = -1;
|
2011-01-02 01:39:13 +01:00
|
|
|
}
|
2010-03-25 13:15:10 +01:00
|
|
|
|
2012-10-20 09:15:13 +02:00
|
|
|
Command::Command(const CommandType *ct, const Vec2i &pos, const UnitType *unitType, CardinalDir facing) : unitRef() {
|
2011-01-02 01:39:13 +01:00
|
|
|
this->commandType= ct;
|
2010-03-25 13:15:10 +01:00
|
|
|
this->pos= pos;
|
2011-03-25 21:51:14 +01:00
|
|
|
this->originalPos = this->pos;
|
2010-03-25 13:15:10 +01:00
|
|
|
this->unitType= unitType;
|
|
|
|
this->facing = facing;
|
2010-08-30 22:45:12 +02:00
|
|
|
stateType = cst_None;
|
|
|
|
stateValue = -1;
|
2011-07-05 06:37:35 +02:00
|
|
|
unitCommandGroupId = -1;
|
2010-05-29 12:34:56 +02:00
|
|
|
|
2011-01-17 17:31:25 +01:00
|
|
|
//if(this->unitType != NULL) {
|
|
|
|
// SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitType = [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->unitType->toString().c_str());
|
|
|
|
//}
|
2010-03-25 13:15:10 +01:00
|
|
|
}
|
|
|
|
|
2010-12-23 11:44:11 +01:00
|
|
|
int Command::getPriority(){
|
|
|
|
if(this->commandType->commandTypeClass==ccAttack && getUnit()==NULL){
|
|
|
|
return 5; // attacks to the ground have low priority
|
|
|
|
}
|
|
|
|
return this->commandType->getTypePriority();
|
|
|
|
}
|
2010-03-25 13:15:10 +01:00
|
|
|
// =============== set ===============
|
|
|
|
|
2011-03-25 21:51:14 +01:00
|
|
|
void Command::setCommandType(const CommandType *commandType) {
|
2010-03-25 13:15:10 +01:00
|
|
|
this->commandType= commandType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::setPos(const Vec2i &pos){
|
|
|
|
this->pos= pos;
|
|
|
|
}
|
|
|
|
|
2011-03-25 21:51:14 +01:00
|
|
|
void Command::setOriginalPos(const Vec2i &pos) {
|
|
|
|
this->originalPos= pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::setPosToOriginalPos() {
|
|
|
|
this->pos= this->originalPos;
|
|
|
|
}
|
|
|
|
|
2010-03-25 13:15:10 +01:00
|
|
|
void Command::setUnit(Unit *unit){
|
|
|
|
this->unitRef= unit;
|
|
|
|
}
|
|
|
|
|
2013-09-21 22:50:58 +02:00
|
|
|
std::string Command::toString(bool translatedValue) const {
|
2010-06-02 10:03:56 +02:00
|
|
|
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__);
|
|
|
|
|
2010-05-29 18:55:55 +02:00
|
|
|
std::string result = "";
|
2010-06-10 16:58:06 +02:00
|
|
|
if(commandType != NULL) {
|
2013-09-21 22:50:58 +02:00
|
|
|
result = "commandType id = " + intToStr(commandType->getId()) + ", desc = " + commandType->toString(translatedValue);
|
2010-06-10 16:58:06 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = "commandType = NULL";
|
|
|
|
}
|
2010-06-02 10:03:56 +02:00
|
|
|
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__);
|
|
|
|
|
2011-03-25 21:51:14 +01:00
|
|
|
result += ", pos = " + pos.getString() + ", originalPos = " + originalPos.getString() + ", facing = " + intToStr(facing.asInt());
|
|
|
|
|
2010-06-02 10:03:56 +02:00
|
|
|
//if(unitRef.getUnit() != NULL) {
|
|
|
|
if(unitRef.getUnitId() >= 0) {
|
|
|
|
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__);
|
|
|
|
|
2010-06-02 22:06:49 +02:00
|
|
|
result += ", unitRef.getUnit() id = " + intToStr(unitRef.getUnitId());
|
2010-12-01 01:05:33 +01:00
|
|
|
|
|
|
|
// The code below causes a STACK OVERFLOW!
|
|
|
|
//if(unitRef.getUnit() != NULL) {
|
|
|
|
// result += ", unitRef.getUnit() = " + unitRef.getUnit()->toString();
|
|
|
|
//}
|
2010-05-31 08:11:31 +02:00
|
|
|
}
|
|
|
|
|
2010-06-02 10:03:56 +02:00
|
|
|
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__);
|
|
|
|
|
2010-05-29 18:55:55 +02:00
|
|
|
if(unitType != NULL) {
|
2010-08-07 05:26:38 +02:00
|
|
|
result += ", unitTypeId = " + intToStr(unitType->getId());
|
2013-09-21 22:50:58 +02:00
|
|
|
result += ", unitTypeDesc = " + unitType->getReqDesc(translatedValue);
|
2010-05-29 18:55:55 +02:00
|
|
|
}
|
2010-06-02 10:03:56 +02:00
|
|
|
|
|
|
|
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__);
|
|
|
|
|
2010-10-28 20:31:12 +02:00
|
|
|
result += ", stateType = " + intToStr(stateType) + ", stateValue = " + intToStr(stateValue);
|
2010-08-30 22:45:12 +02:00
|
|
|
|
2011-07-05 06:37:35 +02:00
|
|
|
|
|
|
|
result += ", unitCommandGroupId = " + intToStr(unitCommandGroupId);
|
|
|
|
|
2010-05-18 05:53:57 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-23 19:16:34 +02:00
|
|
|
Checksum Command::getCRC() {
|
|
|
|
Checksum crcForCmd;
|
|
|
|
|
|
|
|
crcForCmd.addInt(commandType->getId());
|
|
|
|
crcForCmd.addInt(originalPos.x);
|
|
|
|
crcForCmd.addInt(originalPos.y);
|
|
|
|
crcForCmd.addInt(pos.x);
|
|
|
|
crcForCmd.addInt(pos.y);
|
|
|
|
crcForCmd.addInt(unitRef.getUnitId());
|
|
|
|
crcForCmd.addInt(facing);
|
|
|
|
if(unitType != NULL) {
|
|
|
|
crcForCmd.addInt(unitType->getId());
|
|
|
|
}
|
|
|
|
crcForCmd.addInt(stateType);
|
|
|
|
crcForCmd.addInt(stateValue);
|
|
|
|
crcForCmd.addInt(unitCommandGroupId);
|
|
|
|
|
|
|
|
return crcForCmd;
|
|
|
|
}
|
|
|
|
|
2012-03-13 16:21:25 +01:00
|
|
|
void Command::saveGame(XmlNode *rootNode, Faction *faction) {
|
2012-03-10 04:27:25 +01:00
|
|
|
std::map<string,string> mapTagReplacements;
|
|
|
|
XmlNode *commandNode = rootNode->addChild("Command");
|
|
|
|
|
|
|
|
// const CommandType *commandType;
|
|
|
|
if(commandType != NULL) {
|
2012-03-13 16:21:25 +01:00
|
|
|
commandNode->addAttribute("commandType",intToStr(commandType->getId()), mapTagReplacements);
|
2012-03-10 04:27:25 +01:00
|
|
|
}
|
|
|
|
// Vec2i originalPos;
|
|
|
|
commandNode->addAttribute("originalPos",originalPos.getString(), mapTagReplacements);
|
|
|
|
// Vec2i pos;
|
|
|
|
commandNode->addAttribute("pos",pos.getString(), mapTagReplacements);
|
|
|
|
// UnitReference unitRef; //target unit, used to move and attack optionally
|
|
|
|
unitRef.saveGame(commandNode);
|
|
|
|
// CardinalDir facing; // facing, for build command
|
|
|
|
commandNode->addAttribute("facing",intToStr(facing), mapTagReplacements);
|
|
|
|
// const UnitType *unitType; //used for build
|
|
|
|
if(unitType != NULL) {
|
2012-03-13 16:21:25 +01:00
|
|
|
commandNode->addAttribute("unitTypeId",intToStr(unitType->getId()), mapTagReplacements);
|
|
|
|
commandNode->addAttribute("unitTypeFactionIndex",intToStr(faction->getIndex()), mapTagReplacements);
|
2012-03-10 04:27:25 +01:00
|
|
|
}
|
|
|
|
// CommandStateType stateType;
|
|
|
|
commandNode->addAttribute("stateType",intToStr(stateType), mapTagReplacements);
|
|
|
|
// int stateValue;
|
|
|
|
commandNode->addAttribute("stateValue",intToStr(stateValue), mapTagReplacements);
|
|
|
|
// int unitCommandGroupId;
|
|
|
|
commandNode->addAttribute("unitCommandGroupId",intToStr(unitCommandGroupId), mapTagReplacements);
|
|
|
|
}
|
2012-03-13 16:21:25 +01:00
|
|
|
|
|
|
|
Command * Command::loadGame(const XmlNode *rootNode,const UnitType *ut,World *world) {
|
|
|
|
Command *result = new Command();
|
|
|
|
const XmlNode *commandNode = rootNode;
|
|
|
|
|
|
|
|
//description = commandNode->getAttribute("description")->getValue();
|
|
|
|
|
|
|
|
// const CommandType *commandType;
|
|
|
|
if(commandNode->hasAttribute("commandType") == true) {
|
|
|
|
int cmdTypeId = commandNode->getAttribute("commandType")->getIntValue();
|
|
|
|
result->commandType = ut->findCommandTypeById(cmdTypeId);
|
|
|
|
}
|
|
|
|
// Vec2i originalPos;
|
|
|
|
result->originalPos = Vec2i::strToVec2(commandNode->getAttribute("originalPos")->getValue());
|
|
|
|
// Vec2i pos;
|
|
|
|
result->pos = Vec2i::strToVec2(commandNode->getAttribute("pos")->getValue());
|
|
|
|
// UnitReference unitRef; //target unit, used to move and attack optionally
|
|
|
|
result->unitRef.loadGame(commandNode,world);
|
|
|
|
// CardinalDir facing; // facing, for build command
|
|
|
|
result->facing = static_cast<CardinalDir>(commandNode->getAttribute("facing")->getIntValue());
|
|
|
|
// const UnitType *unitType; //used for build
|
|
|
|
if(commandNode->hasAttribute("unitTypeId") == true) {
|
|
|
|
//result->unitType = ut;
|
|
|
|
int unitTypeId = commandNode->getAttribute("unitTypeId")->getIntValue();
|
|
|
|
int unitTypeFactionIndex = commandNode->getAttribute("unitTypeFactionIndex")->getIntValue();
|
|
|
|
Faction *faction = world->getFaction(unitTypeFactionIndex);
|
|
|
|
result->unitType = world->findUnitTypeById(faction->getType(),unitTypeId);
|
|
|
|
}
|
|
|
|
// CommandStateType stateType;
|
|
|
|
result->stateType = static_cast<CommandStateType>(commandNode->getAttribute("stateType")->getIntValue());
|
|
|
|
// int stateValue;
|
|
|
|
result->stateValue = commandNode->getAttribute("stateValue")->getIntValue();
|
|
|
|
// int unitCommandGroupId;
|
|
|
|
result->unitCommandGroupId = commandNode->getAttribute("unitCommandGroupId")->getIntValue();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-03-25 13:15:10 +01:00
|
|
|
}}//end namespace
|