Changed unit rotation logic to attach to existing ccBuild command (placing rotateAmount into the targetId field which was always unused for this commandtype). Commented out the old network code that was used to do this for now.

This commit is contained in:
Mark Vejvoda
2010-03-22 04:47:16 +00:00
parent 344c4778d1
commit d713349187
7 changed files with 106 additions and 20 deletions

View File

@@ -41,7 +41,7 @@ void Commander::init(World *world){
} }
CommandResult Commander::tryGiveCommand(const Unit* unit, const CommandType *commandType, const Vec2i &pos, const UnitType* unitType) const{ CommandResult Commander::tryGiveCommand(const Unit* unit, const CommandType *commandType, const Vec2i &pos, const UnitType* unitType) const{
NetworkCommand networkCommand(nctGiveCommand, unit->getId(), commandType->getId(), pos, unitType->getId()); NetworkCommand networkCommand(this->world,nctGiveCommand, unit->getId(), commandType->getId(), pos, unitType->getId());
return pushNetworkCommand(&networkCommand); return pushNetworkCommand(&networkCommand);
} }
@@ -61,7 +61,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, CommandClass
int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId();
int unitId= selection->getUnit(i)->getId(); int unitId= selection->getUnit(i)->getId();
Vec2i currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos); Vec2i currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos);
NetworkCommand networkCommand(nctGiveCommand, unitId, ct->getId(), currPos, -1, targetId); NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, ct->getId(), currPos, -1, targetId);
//every unit is ordered to a different pos //every unit is ordered to a different pos
CommandResult result= pushNetworkCommand(&networkCommand); CommandResult result= pushNetworkCommand(&networkCommand);
@@ -90,7 +90,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Comman
int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId();
int unitId= selection->getUnit(i)->getId(); int unitId= selection->getUnit(i)->getId();
Vec2i currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos); Vec2i currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos);
NetworkCommand networkCommand(nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId); NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId);
//every unit is ordered to a different position //every unit is ordered to a different position
CommandResult result= pushNetworkCommand(&networkCommand); CommandResult result= pushNetworkCommand(&networkCommand);
@@ -125,7 +125,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Vec2i
if(commandType!=NULL){ if(commandType!=NULL){
int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId();
int unitId= selection->getUnit(i)->getId(); int unitId= selection->getUnit(i)->getId();
NetworkCommand networkCommand(nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId); NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId);
CommandResult result= pushNetworkCommand(&networkCommand); CommandResult result= pushNetworkCommand(&networkCommand);
results.push_back(result); results.push_back(result);
@@ -144,7 +144,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Vec2i
CommandResult Commander::tryCancelCommand(const Selection *selection) const{ CommandResult Commander::tryCancelCommand(const Selection *selection) const{
for(int i=0; i<selection->getCount(); ++i){ for(int i=0; i<selection->getCount(); ++i){
NetworkCommand command(nctCancelCommand, selection->getUnit(i)->getId()); NetworkCommand command(this->world,nctCancelCommand, selection->getUnit(i)->getId());
pushNetworkCommand(&command); pushNetworkCommand(&command);
} }
@@ -152,7 +152,7 @@ CommandResult Commander::tryCancelCommand(const Selection *selection) const{
} }
void Commander::trySetMeetingPoint(const Unit* unit, const Vec2i &pos)const{ void Commander::trySetMeetingPoint(const Unit* unit, const Vec2i &pos)const{
NetworkCommand command(nctSetMeetingPoint, unit->getId(), -1, pos); NetworkCommand command(this->world,nctSetMeetingPoint, unit->getId(), -1, pos);
pushNetworkCommand(&command); pushNetworkCommand(&command);
} }
@@ -245,6 +245,7 @@ void Commander::updateNetwork(){
} }
} }
/*
void Commander::giveNetworkCommandSpecial(const NetworkCommand* networkCommand) const { void Commander::giveNetworkCommandSpecial(const NetworkCommand* networkCommand) const {
switch(networkCommand->getNetworkCommandType()) { switch(networkCommand->getNetworkCommandType()) {
case nctNetworkCommand: { case nctNetworkCommand: {
@@ -282,13 +283,18 @@ void Commander::giveNetworkCommandSpecial(const NetworkCommand* networkCommand)
assert(false); assert(false);
} }
} }
*/
void Commander::giveNetworkCommand(const NetworkCommand* networkCommand) const { void Commander::giveNetworkCommand(NetworkCommand* networkCommand) const {
networkCommand->preprocessNetworkCommand(this->world);
/*
if(networkCommand->getNetworkCommandType() == nctNetworkCommand) { if(networkCommand->getNetworkCommandType() == nctNetworkCommand) {
giveNetworkCommandSpecial(networkCommand); giveNetworkCommandSpecial(networkCommand);
} }
else { else
*/
{
Unit* unit= world->findUnitById(networkCommand->getUnitId()); Unit* unit= world->findUnitById(networkCommand->getUnitId());
//exec ute command, if unit is still alive //exec ute command, if unit is still alive

View File

@@ -54,13 +54,13 @@ public:
CommandResult tryCancelCommand(const Selection *selection) const; CommandResult tryCancelCommand(const Selection *selection) const;
void trySetMeetingPoint(const Unit* unit, const Vec2i &pos) const; void trySetMeetingPoint(const Unit* unit, const Vec2i &pos) const;
CommandResult pushNetworkCommand(const NetworkCommand* networkCommand) const; CommandResult pushNetworkCommand(const NetworkCommand* networkCommand) const;
void giveNetworkCommandSpecial(const NetworkCommand* networkCommand) const; //void giveNetworkCommandSpecial(const NetworkCommand* networkCommand) const;
private: private:
Vec2i computeRefPos(const Selection *selection) const; Vec2i computeRefPos(const Selection *selection) const;
Vec2i computeDestPos(const Vec2i &refUnitPos, const Vec2i &unitPos, const Vec2i &commandPos) const; Vec2i computeDestPos(const Vec2i &refUnitPos, const Vec2i &unitPos, const Vec2i &commandPos) const;
CommandResult computeResult(const CommandResultContainer &results) const; CommandResult computeResult(const CommandResultContainer &results) const;
void giveNetworkCommand(const NetworkCommand* networkCommand) const; void giveNetworkCommand(NetworkCommand* networkCommand) const;
Command* buildCommand(const NetworkCommand* networkCommand) const; Command* buildCommand(const NetworkCommand* networkCommand) const;
}; };

View File

@@ -363,6 +363,7 @@ void Gui::hotKey(char key){
unitTypeBuildRotation[unitKey] = unitTypeRotation; unitTypeBuildRotation[unitKey] = unitTypeRotation;
//!!! //!!!
/*
//if(allowRotateUnits == true && unitRotation > 0) { //if(allowRotateUnits == true && unitRotation > 0) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] before sending nctNetworkCommand RotateUnit unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),factionIndex,unitTypeRotation); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] before sending nctNetworkCommand RotateUnit unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),factionIndex,unitTypeRotation);
@@ -375,6 +376,7 @@ void Gui::hotKey(char key){
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] after sending nctNetworkCommand RotateUnit [%d] result = %d\n",__FILE__,__FUNCTION__,builtUnit->getId(),result); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] after sending nctNetworkCommand RotateUnit [%d] result = %d\n",__FILE__,__FUNCTION__,builtUnit->getId(),result);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] after sending nctNetworkCommand RotateUnit unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),factionIndex,unitTypeRotation); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] after sending nctNetworkCommand RotateUnit unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),factionIndex,unitTypeRotation);
//} //}
*/
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] unitType->getId() = %d NEW unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),unitTypeRotation); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] unitType->getId() = %d NEW unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),unitTypeRotation);
} }

View File

@@ -137,7 +137,7 @@ public:
//access functions //access functions
void requestCommand(const NetworkCommand *networkCommand, bool insertAtStart=false); void requestCommand(const NetworkCommand *networkCommand, bool insertAtStart=false);
int getPendingCommandCount() const {return pendingCommands.size();} int getPendingCommandCount() const {return pendingCommands.size();}
const NetworkCommand* getPendingCommand(int i) const {return &pendingCommands[i];} NetworkCommand* getPendingCommand(int i) {return &pendingCommands[i];}
void clearPendingCommands() {pendingCommands.clear();} void clearPendingCommands() {pendingCommands.clear();}
bool getQuit() const {return quit;} bool getQuit() const {return quit;}
}; };

View File

@@ -10,16 +10,24 @@
// ============================================================== // ==============================================================
#include "network_types.h" #include "network_types.h"
#include "util.h"
#include "unit.h"
#include "world.h"
#include "unit_type.h"
#include "game.h"
#include "gui.h"
#include "leak_dumper.h" #include "leak_dumper.h"
using namespace Shared::Util;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{
// ===================================================== // =====================================================
// class NetworkCommand // class NetworkCommand
// ===================================================== // =====================================================
NetworkCommand::NetworkCommand(int networkCommandType, int unitId, int commandTypeId, const Vec2i &pos, int unitTypeId, int targetId){ NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId, int commandTypeId, const Vec2i &pos, int unitTypeId, int targetId){
this->networkCommandType= networkCommandType; this->networkCommandType= networkCommandType;
this->unitId= unitId; this->unitId= unitId;
this->commandTypeId= commandTypeId; this->commandTypeId= commandTypeId;
@@ -27,8 +35,72 @@ NetworkCommand::NetworkCommand(int networkCommandType, int unitId, int commandTy
this->positionY= pos.y; this->positionY= pos.y;
this->unitTypeId= unitTypeId; this->unitTypeId= unitTypeId;
this->targetId= targetId; this->targetId= targetId;
if(this->networkCommandType == nctGiveCommand) {
const Unit *unit= world->findUnitById(this->unitId);
//validate unit
if(unit != NULL) {
const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), this->unitTypeId);
const CommandType *ct = unit->getType()->findCommandTypeById(this->commandTypeId);
if(ct != NULL && ct->getClass() == ccBuild) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str());
Game *game = world->getGame();
Gui *gui = game->getGui();
int factionIndex = world->getThisFactionIndex();
char unitKey[50]="";
sprintf(unitKey,"%d_%d",this->unitTypeId,factionIndex);
float unitTypeRotation = gui->getUnitTypeBuildRotation(unitKey);
if(unitTypeRotation >= 0) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] attaching RotateUnit to ccBuild command for unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,this->unitTypeId,factionIndex,unitTypeRotation);
this->targetId = unitTypeRotation;
}
}
}
}
} }
void NetworkCommand::preprocessNetworkCommand(World *world) {
if(networkCommandType == nctGiveCommand) {
const Unit *unit= world->findUnitById(unitId);
//validate unit
if(unit != NULL) {
const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), unitTypeId);
const CommandType *ct = unit->getType()->findCommandTypeById(commandTypeId);
if(ct != NULL && ct->getClass() == ccBuild && targetId >= 0) {
Game *game = world->getGame();
Gui *gui = game->getGui();
int factionIndex = unit->getFactionIndex();
char unitKey[50]="";
sprintf(unitKey,"%d_%d",unitTypeId,factionIndex);
gui->setUnitTypeBuildRotation(unitKey,targetId);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] %s, unitKey = [%s] targetId = %d\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str(),unitKey,targetId);
targetId = Unit::invalidId;
}
}
else {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] (unit == NULL) %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str());
}
}
}
string NetworkCommand::toString() const {
char szBuf[1024]="";
sprintf(szBuf,"networkCommandType = %d\nunitId = %d\ncommandTypeId = %d\npositionX = %d\nthis->positionY = %d\nunitTypeId = %d\ntargetId = %d",
networkCommandType,unitId,commandTypeId,positionX,this->positionY,unitTypeId,targetId);
string result = szBuf;
return result;
}
/*
NetworkCommand::NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2) { NetworkCommand::NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2) {
this->networkCommandType= networkCommandType; this->networkCommandType= networkCommandType;
this->unitId= unitId; this->unitId= unitId;
@@ -38,5 +110,6 @@ NetworkCommand::NetworkCommand(int networkCommandType, NetworkCommandSubType ncs
this->unitTypeId= value1; this->unitTypeId= value1;
this->targetId= value2; this->targetId= value2;
} }
*/
}}//end namespace }}//end namespace

View File

@@ -25,6 +25,8 @@ using Shared::Graphics::Vec2i;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{
class World;
// ===================================================== // =====================================================
// class NetworkString // class NetworkString
// ===================================================== // =====================================================
@@ -47,13 +49,13 @@ public:
enum NetworkCommandType { enum NetworkCommandType {
nctGiveCommand, nctGiveCommand,
nctCancelCommand, nctCancelCommand,
nctSetMeetingPoint, nctSetMeetingPoint
nctNetworkCommand //nctNetworkCommand
}; };
enum NetworkCommandSubType { //enum NetworkCommandSubType {
ncstRotateUnit // ncstRotateUnit
}; //};
#pragma pack(push, 2) #pragma pack(push, 2)
class NetworkCommand{ class NetworkCommand{
@@ -68,8 +70,8 @@ private:
public: public:
NetworkCommand(){}; NetworkCommand(){};
NetworkCommand(int networkCommandType, int unitId, int commandTypeId= -1, const Vec2i &pos= Vec2i(0), int unitTypeId= -1, int targetId= -1); NetworkCommand(World *world, int networkCommandType, int unitId, int commandTypeId= -1, const Vec2i &pos= Vec2i(0), int unitTypeId= -1, int targetId= -1);
NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2=-1); //NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2=-1);
NetworkCommandType getNetworkCommandType() const {return static_cast<NetworkCommandType>(networkCommandType);} NetworkCommandType getNetworkCommandType() const {return static_cast<NetworkCommandType>(networkCommandType);}
int getUnitId() const {return unitId;} int getUnitId() const {return unitId;}
@@ -77,6 +79,9 @@ public:
Vec2i getPosition() const {return Vec2i(positionX, positionY);} Vec2i getPosition() const {return Vec2i(positionX, positionY);}
int getUnitTypeId() const {return unitTypeId;} int getUnitTypeId() const {return unitTypeId;}
int getTargetId() const {return targetId;} int getTargetId() const {return targetId;}
void preprocessNetworkCommand(World *world);
string toString() const;
}; };
#pragma pack(pop) #pragma pack(pop)

View File

@@ -301,7 +301,7 @@ void UnitUpdater::updateBuild(Unit *unit){
sprintf(unitKey,"%d_%d",builtUnitType->getId(),unit->getFaction()->getIndex()); sprintf(unitKey,"%d_%d",builtUnitType->getId(),unit->getFaction()->getIndex());
unitRotation = gui->getUnitTypeBuildRotation(unitKey); unitRotation = gui->getUnitTypeBuildRotation(unitKey);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] builtUnitType->getId() = %d unitRotation = %f\n",__FILE__,__FUNCTION__,__LINE__,builtUnitType->getId(),unitRotation); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitKey = [%s] unitRotation = %f\n",__FILE__,__FUNCTION__,__LINE__,unitKey,unitRotation);
} }
Unit *builtUnit= new Unit(world->getNextUnitId(), command->getPos(), builtUnitType, unit->getFaction(), world->getMap(),unitRotation); Unit *builtUnit= new Unit(world->getNextUnitId(), command->getPos(), builtUnitType, unit->getFaction(), world->getMap(),unitRotation);
builtUnit->create(); builtUnit->create();