From b2101a018dac99fe59a78b82b8388d18502e46ad Mon Sep 17 00:00:00 2001 From: johns <> Date: Sun, 25 Feb 2001 04:01:38 +0000 Subject: [PATCH] Better logging of commands, missing network command functions added. --- src/network/commands.cpp | 189 ++++++++++++++++++++++++++------------- 1 file changed, 126 insertions(+), 63 deletions(-) diff --git a/src/network/commands.cpp b/src/network/commands.cpp index 71dd81e71..0115eae14 100644 --- a/src/network/commands.cpp +++ b/src/network/commands.cpp @@ -10,7 +10,7 @@ // /**@name commands.c - Global command handler - network support. */ // -// (c) Copyright 2000 by Lutz Sammer +// (c) Copyright 2000,2001 by Lutz Sammer and Andreas Arens. // // $Id$ @@ -70,10 +70,15 @@ local void CommandLog(const char* name,const Unit* unit,int flag, return; } + // + // Create and write header of log file. + // if( !logf ) { time_t now; + char buf[256]; - logf=fopen("command.log","wb"); + sprintf(buf,"log_of_freecraft_%d.log",ThisPlayer->Player); + logf=fopen(buf,"wb"); if( !logf ) { return; } @@ -83,18 +88,35 @@ local void CommandLog(const char* name,const Unit* unit,int flag, fprintf(logf,";;;\tDate: %s",ctime(&now)); fprintf(logf,";;;\tMap: %s\n\n",TheMap.Description); } - fprintf(logf,"(log %d 'U%Zd '%s '%s", - FrameCounter,UnitNumber(unit),name, + + // + // Frame, unit, (type-ident only for better readable. + // + fprintf(logf,"(log %d 'U%Zd '%s '%s '%s", + FrameCounter,UnitNumber(unit),unit->Type->Ident,name, flag ? "flush" : "append"); + + // + // Coordinates given. + // if( x!=-1 || y!=-1 ) { fprintf(logf," (%d %d)",x,y); } + // + // Destination given. + // if( dest ) { fprintf(logf," 'U%Zd",UnitNumber(dest)); } + // + // Value given. + // if( value ) { fprintf(logf," '%s",value); } + // + // Number given. + // if( num!=-1 ) { fprintf(logf," %d",num); } @@ -118,8 +140,8 @@ local void CommandLog(const char* name,const Unit* unit,int flag, */ global void SendCommandStopUnit(Unit* unit) { - CommandLog("stop",unit,1,-1,-1,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("stop",unit,1,-1,-1,NoUnitP,NULL,-1); CommandStopUnit(unit); } else { NetworkSendCommand(MessageCommandStop,unit,0,0,NoUnitP,0,1); @@ -134,8 +156,8 @@ global void SendCommandStopUnit(Unit* unit) */ global void SendCommandStandGround(Unit* unit,int flush) { - CommandLog("stand-ground",unit,flush,-1,-1,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("stand-ground",unit,flush,-1,-1,NoUnitP,NULL,-1); CommandStandGround(unit,flush); } else { NetworkSendCommand(MessageCommandStand,unit,0,0,NoUnitP,0,flush); @@ -152,8 +174,8 @@ global void SendCommandStandGround(Unit* unit,int flush) */ global void SendCommandFollow(Unit* unit,Unit* dest,int flush) { - CommandLog("follow",unit,flush,-1,-1,dest,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("follow",unit,flush,-1,-1,dest,NULL,-1); CommandFollow(unit,dest,flush); } else { NetworkSendCommand(MessageCommandFollow,unit,0,0,dest,0,flush); @@ -170,8 +192,8 @@ global void SendCommandFollow(Unit* unit,Unit* dest,int flush) */ global void SendCommandMove(Unit* unit,int x,int y,int flush) { - CommandLog("move",unit,flush,x,y,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("move",unit,flush,x,y,NoUnitP,NULL,-1); CommandMove(unit,x,y,flush); } else { NetworkSendCommand(MessageCommandMove,unit,x,y,NoUnitP,0,flush); @@ -188,8 +210,8 @@ global void SendCommandMove(Unit* unit,int x,int y,int flush) */ global void SendCommandRepair(Unit* unit,int x,int y,Unit* dest,int flush) { - CommandLog("repair",unit,flush,x,y,dest,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("repair",unit,flush,x,y,dest,NULL,-1); CommandRepair(unit,x,y,dest,flush); } else { NetworkSendCommand(MessageCommandRepair,unit,x,y,dest,0,flush); @@ -207,8 +229,8 @@ global void SendCommandRepair(Unit* unit,int x,int y,Unit* dest,int flush) */ global void SendCommandAttack(Unit* unit,int x,int y,Unit* attack,int flush) { - CommandLog("attack",unit,flush,x,y,attack,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("attack",unit,flush,x,y,attack,NULL,-1); CommandAttack(unit,x,y,attack,flush); } else { NetworkSendCommand(MessageCommandAttack,unit,x,y,attack,0,flush); @@ -225,8 +247,8 @@ global void SendCommandAttack(Unit* unit,int x,int y,Unit* attack,int flush) */ global void SendCommandAttackGround(Unit* unit,int x,int y,int flush) { - CommandLog("attack-ground",unit,flush,x,y,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("attack-ground",unit,flush,x,y,NoUnitP,NULL,-1); CommandAttackGround(unit,x,y,flush); } else { NetworkSendCommand(MessageCommandGround,unit,x,y,NoUnitP,0,flush); @@ -243,8 +265,8 @@ global void SendCommandAttackGround(Unit* unit,int x,int y,int flush) */ global void SendCommandPatrol(Unit* unit,int x,int y,int flush) { - CommandLog("patrol",unit,flush,x,y,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("patrol",unit,flush,x,y,NoUnitP,NULL,-1); CommandPatrolUnit(unit,x,y,flush); } else { NetworkSendCommand(MessageCommandPatrol,unit,x,y,NoUnitP,0,flush); @@ -260,8 +282,8 @@ global void SendCommandPatrol(Unit* unit,int x,int y,int flush) */ global void SendCommandBoard(Unit* unit,int x,int y,Unit* dest,int flush) { - CommandLog("board",unit,flush,x,y,dest,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("board",unit,flush,x,y,dest,NULL,-1); CommandBoard(unit,dest,flush); } else { NetworkSendCommand(MessageCommandBoard,unit,x,y,dest,0,flush); @@ -279,8 +301,8 @@ global void SendCommandBoard(Unit* unit,int x,int y,Unit* dest,int flush) */ global void SendCommandUnload(Unit* unit,int x,int y,Unit* what,int flush) { - CommandLog("unload",unit,flush,x,y,what,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("unload",unit,flush,x,y,what,NULL,-1); CommandUnload(unit,x,y,what,flush); } else { NetworkSendCommand(MessageCommandUnload,unit,x,y,what,0,flush); @@ -299,8 +321,8 @@ global void SendCommandUnload(Unit* unit,int x,int y,Unit* what,int flush) global void SendCommandBuildBuilding(Unit* unit,int x,int y ,UnitType* what,int flush) { - CommandLog("build",unit,flush,x,y,NULL,what->Ident,-1); if( NetworkFildes==-1 ) { + CommandLog("build",unit,flush,x,y,NULL,what->Ident,-1); CommandBuildBuilding(unit,x,y,what,flush); } else { NetworkSendCommand(MessageCommandBuild,unit,x,y,NoUnitP,what,flush); @@ -315,8 +337,8 @@ global void SendCommandBuildBuilding(Unit* unit,int x,int y global void SendCommandCancelBuilding(Unit* unit,Unit* worker) { // FIXME: currently unit and worker are same? - CommandLog("cancel-build",unit,1,-1,-1,worker,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("cancel-build",unit,1,-1,-1,worker,NULL,-1); CommandCancelBuilding(unit,worker); } else { NetworkSendCommand(MessageCommandCancelBuild,unit,0,0,worker,0,1); @@ -333,8 +355,8 @@ global void SendCommandCancelBuilding(Unit* unit,Unit* worker) */ global void SendCommandHarvest(Unit* unit,int x,int y,int flush) { - CommandLog("harvest",unit,flush,x,y,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("harvest",unit,flush,x,y,NoUnitP,NULL,-1); CommandHarvest(unit,x,y,flush); } else { NetworkSendCommand(MessageCommandHarvest,unit,x,y,NoUnitP,0,flush); @@ -350,8 +372,8 @@ global void SendCommandHarvest(Unit* unit,int x,int y,int flush) */ global void SendCommandMineGold(Unit* unit,Unit* dest,int flush) { - CommandLog("mine",unit,flush,-1,-1,dest,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("mine",unit,flush,-1,-1,dest,NULL,-1); CommandMineGold(unit,dest,flush); } else { NetworkSendCommand(MessageCommandMine,unit,0,0,dest,0,flush); @@ -367,8 +389,8 @@ global void SendCommandMineGold(Unit* unit,Unit* dest,int flush) */ global void SendCommandHaulOil(Unit* unit,Unit* dest,int flush) { - CommandLog("haul",unit,flush,-1,-1,dest,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("haul",unit,flush,-1,-1,dest,NULL,-1); CommandHaulOil(unit,dest,flush); } else { NetworkSendCommand(MessageCommandHaul,unit,0,0,dest,0,flush); @@ -383,8 +405,8 @@ global void SendCommandHaulOil(Unit* unit,Unit* dest,int flush) */ global void SendCommandReturnGoods(Unit* unit,int flush) { - CommandLog("return",unit,flush,-1,-1,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("return",unit,flush,-1,-1,NoUnitP,NULL,-1); CommandReturnGoods(unit,flush); } else { NetworkSendCommand(MessageCommandReturn,unit,0,0,NoUnitP,0,flush); @@ -400,8 +422,8 @@ global void SendCommandReturnGoods(Unit* unit,int flush) */ global void SendCommandTrainUnit(Unit* unit,UnitType* what,int flush) { - CommandLog("train",unit,flush,-1,-1,NULL,what->Ident,-1); if( NetworkFildes==-1 ) { + CommandLog("train",unit,flush,-1,-1,NULL,what->Ident,-1); CommandTrainUnit(unit,what,flush); } else { NetworkSendCommand(MessageCommandTrain,unit,0,0,NoUnitP,what,flush); @@ -416,8 +438,8 @@ global void SendCommandTrainUnit(Unit* unit,UnitType* what,int flush) */ global void SendCommandCancelTraining(Unit* unit,int slot) { - CommandLog("cancel-train",unit,1,-1,-1,NoUnitP,NULL,slot); if( NetworkFildes==-1 ) { + CommandLog("cancel-train",unit,1,-1,-1,NoUnitP,NULL,slot); CommandCancelTraining(unit,slot); } else { NetworkSendCommand(MessageCommandCancelTrain,unit,slot,0,NoUnitP,0,1); @@ -433,8 +455,8 @@ global void SendCommandCancelTraining(Unit* unit,int slot) */ global void SendCommandUpgradeTo(Unit* unit,UnitType* what,int flush) { - CommandLog("upgrade-to",unit,flush,-1,-1,NULL,what->Ident,-1); if( NetworkFildes==-1 ) { + CommandLog("upgrade-to",unit,flush,-1,-1,NULL,what->Ident,-1); CommandUpgradeTo(unit,what,flush); } else { NetworkSendCommand(MessageCommandUpgrade,unit,0,0,NoUnitP,what,flush); @@ -448,8 +470,8 @@ global void SendCommandUpgradeTo(Unit* unit,UnitType* what,int flush) */ global void SendCommandCancelUpgradeTo(Unit* unit) { - CommandLog("cancel-upgrade-to",unit,1,-1,-1,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("cancel-upgrade-to",unit,1,-1,-1,NoUnitP,NULL,-1); CommandCancelUpgradeTo(unit); } else { NetworkSendCommand(MessageCommandCancelUpgrade,unit @@ -466,12 +488,12 @@ global void SendCommandCancelUpgradeTo(Unit* unit) */ global void SendCommandResearch(Unit* unit,Upgrade* what,int flush) { - CommandLog("research",unit,flush,-1,-1,NULL,what->Ident,-1); if( NetworkFildes==-1 ) { + CommandLog("research",unit,flush,-1,-1,NULL,what->Ident,-1); CommandResearch(unit,what,flush); } else { NetworkSendCommand(MessageCommandResearch,unit - ,what-Upgrades,0,NoUnitP,0,flush); + ,0,0,NoUnitP,(void*)(what-Upgrades),flush); } } @@ -482,8 +504,8 @@ global void SendCommandResearch(Unit* unit,Upgrade* what,int flush) */ global void SendCommandCancelResearch(Unit* unit) { - CommandLog("cancel-research",unit,1,-1,-1,NoUnitP,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("cancel-research",unit,1,-1,-1,NoUnitP,NULL,-1); CommandCancelResearch(unit); } else { NetworkSendCommand(MessageCommandCancelResearch,unit @@ -502,8 +524,8 @@ global void SendCommandCancelResearch(Unit* unit) */ global void SendCommandDemolish(Unit* unit,int x,int y,Unit* attack,int flush) { - CommandLog("demolish",unit,flush,x,y,attack,NULL,-1); if( NetworkFildes==-1 ) { + CommandLog("demolish",unit,flush,x,y,attack,NULL,-1); CommandDemolish(unit,x,y,attack,flush); } else { NetworkSendCommand(MessageCommandDemolish,unit,x,y,attack,0,flush); @@ -520,10 +542,11 @@ global void SendCommandDemolish(Unit* unit,int x,int y,Unit* attack,int flush) ** @param spellid Spell type id. ** @param flush Flag flush all pending commands. */ -global void SendCommandSpellCast(Unit* unit,int x,int y,Unit* dest,int spellid,int flush) +global void SendCommandSpellCast(Unit* unit,int x,int y,Unit* dest,int spellid + ,int flush) { - CommandLog("spell-cast",unit,flush,x,y,dest,NULL,spellid); if( NetworkFildes==-1 ) { + CommandLog("spell-cast",unit,flush,x,y,dest,NULL,spellid); CommandSpellCast(unit,x,y,dest,spellid,flush); } else { NetworkSendCommand(MessageCommandSpellCast+spellid @@ -533,6 +556,9 @@ global void SendCommandSpellCast(Unit* unit,int x,int y,Unit* dest,int spellid,i //@} +//---------------------------------------------------------------------------- +// Parse the message, from the network. +//---------------------------------------------------------------------------- /**@name parse */ //@{ @@ -540,21 +566,21 @@ global void SendCommandSpellCast(Unit* unit,int x,int y,Unit* dest,int spellid,i /** ** Parse a command (from network). ** -** @param Type Network message type -** @param Unum Unit number (slot) that receive the command. +** @param msgnr Network message type +** @param unum Unit number (slot) that receive the command. ** @param x optional X map position. ** @param y optional y map position. -** @param Dest optional destination unit. +** @param dstnr optional destination unit. */ -global void ParseCommand(unsigned short Type,UnitRef Unum, - unsigned short x,unsigned short y,UnitRef Dest) +global void ParseCommand(unsigned short msgnr,UnitRef unum, + unsigned short x,unsigned short y,UnitRef dstnr) { Unit *unit, *dest; int id, status; - DebugLevel3Fn(" %d frame %d\n", Type, FrameCounter); + DebugLevel3Fn(" %d frame %d\n", msgnr, FrameCounter); - unit=UnitSlots[Unum]; + unit=UnitSlots[unum]; DebugCheck( !unit ); // // Check if unit is already killed? @@ -564,116 +590,153 @@ global void ParseCommand(unsigned short Type,UnitRef Unum, return; } - status=(Type&0x80)>>7; + status=(msgnr&0x80)>>7; // Note: destroyed destination unit is handled by the action routines. - switch( Type&0x7F ) { + switch( msgnr&0x7F ) { case MessageSync: return; case MessageQuit: return; + case MessageCommandStop: + CommandLog("stop",unit,1,-1,-1,NoUnitP,NULL,-1); CommandStopUnit(unit); break; case MessageCommandStand: + CommandLog("stand-ground",unit,status,-1,-1,NoUnitP,NULL,-1); CommandStandGround(unit,status); break; + case MessageCommandFollow: + dest=NoUnitP; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; + DebugCheck( !dest ); + } + CommandLog("follow",unit,status,-1,-1,dest,NULL,-1); + CommandFollow(unit,dest,status); + break; case MessageCommandMove: + CommandLog("move",unit,status,x,y,NoUnitP,NULL,-1); CommandMove(unit,x,y,status); break; case MessageCommandAttack: dest=NoUnitP; - DebugLevel3Fn(" %x\n",Dest); - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } + CommandLog("attack",unit,status,x,y,dest,NULL,-1); CommandAttack(unit,x,y,dest,status); break; case MessageCommandGround: + CommandLog("attack-ground",unit,status,x,y,NoUnitP,NULL,-1); CommandAttackGround(unit,x,y,status); break; case MessageCommandPatrol: + CommandLog("patrol",unit,status,x,y,NoUnitP,NULL,-1); CommandPatrolUnit(unit,x,y,status); break; case MessageCommandBoard: dest=NoUnitP; - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } + CommandLog("board",unit,status,x,y,dest,NULL,-1); CommandBoard(unit,dest,status); break; case MessageCommandUnload: dest=NoUnitP; - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } + CommandLog("unload",unit,status,x,y,dest,NULL,-1); CommandUnload(unit,x,y,dest,status); break; case MessageCommandBuild: - CommandBuildBuilding(unit,x,y - ,UnitTypes+Dest,status); + CommandLog("build",unit,status,x,y,NULL,UnitTypes[dstnr].Ident,-1); + CommandBuildBuilding(unit,x,y,UnitTypes+dstnr,status); break; case MessageCommandCancelBuild: // dest is the worker building the unit... dest=NoUnitP; - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } + CommandLog("cancel-build",unit,1,-1,-1,dest,NULL,-1); CommandCancelBuilding(unit,dest); break; case MessageCommandHarvest: + CommandLog("harvest",unit,status,x,y,NoUnitP,NULL,-1); CommandHarvest(unit,x,y,status); break; case MessageCommandMine: dest=NoUnitP; - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } + CommandLog("mine",unit,status,-1,-1,dest,NULL,-1); CommandMineGold(unit,dest,status); break; case MessageCommandHaul: dest=NoUnitP; - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } + CommandLog("haul",unit,status,-1,-1,dest,NULL,-1); CommandHaulOil(unit,dest,status); break; case MessageCommandReturn: + CommandLog("return",unit,status,-1,-1,NoUnitP,NULL,-1); CommandReturnGoods(unit,status); break; case MessageCommandTrain: - CommandTrainUnit(unit,UnitTypes+Dest,status); + CommandLog("train",unit,status,-1,-1,NULL + ,UnitTypes[dstnr].Ident,-1); + CommandTrainUnit(unit,UnitTypes+dstnr,status); break; case MessageCommandCancelTrain: - // FIXME: cancel slot? - CommandCancelTraining(unit,0); + CommandLog("cancel-train",unit,1,-1,-1,NoUnitP,NULL,dstnr); + CommandCancelTraining(unit,dstnr); break; case MessageCommandUpgrade: - CommandUpgradeTo(unit,UnitTypes+Dest,status); + CommandLog("upgrade-to",unit,status,-1,-1,NULL + ,UnitTypes[dstnr].Ident,-1); + CommandUpgradeTo(unit,UnitTypes+dstnr,status); + break; + case MessageCommandCancelUpgrade: + CommandLog("cancel-upgrade-to",unit,1,-1,-1,NoUnitP,NULL,-1); + CommandCancelUpgradeTo(unit); break; case MessageCommandResearch: + CommandLog("research",unit,status,-1,-1,NULL + ,Upgrades[dstnr].Ident,-1); CommandResearch(unit,Upgrades+x,status); break; + case MessageCommandCancelResearch: + CommandLog("cancel-research",unit,1,-1,-1,NoUnitP,NULL,-1); + CommandCancelResearch(unit); + break; case MessageCommandDemolish: dest=NoUnitP; - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } + CommandLog("demolish",unit,status,x,y,dest,NULL,-1); CommandDemolish(unit,x,y,dest,status); break; default: - id = (Type&0x7f) - MessageCommandSpellCast; + id = (msgnr&0x7f) - MessageCommandSpellCast; dest=NoUnitP; - if( Dest!=(unsigned short)0xFFFF ) { - dest=UnitSlots[Dest]; + if( dstnr!=(unsigned short)0xFFFF ) { + dest=UnitSlots[dstnr]; DebugCheck( !dest ); } CommandSpellCast(unit,x,y,dest,id,status);