Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Representations/RoboCup/GameControlData.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file GameControlData.cpp
    00003 *
    00004 * Implementation of class GameControlData.
    00005 *
    00006 * @author Martin Loetzsch
    00007 */
    00008
    00009 #include "GameControlData.h"
    00010 #include "Platform/SystemCall.h"
    00011 #include "Tools/Debugging/Debugging.h"
    00012
    00013
    00014 GameControlData::GameControlData()
    00015 : timeStamp(0)
    00016 , switchOff(false)
    00017 {
    00018   strncpy(data.header,STRUCT_HEADER,4);
    00019   data.version = STRUCT_VERSION;
    00020   data.state = (int)initial;
    00021   data.firstHalf = 0;
    00022   data.kickOffTeam = 0;
    00023   data.secsRemaining = 10 * 60;
    00024   data.dropInTeam = TEAM_RED;
    00025   data.dropInTime = 0;
    00026
    00027   for (int team=0; team < 2; team++)
    00028   {
    00029     data.teams[team].teamNumber = team;
    00030     data.teams[team].teamColour = team;
    00031     data.teams[team].score = 0;
    00032     for (int player=0; player < 4; player++)
    00033     {
    00034       data.teams[team].players[player].penalty = (int)notPenalized;
    00035     }
    00036   }
    00037 }
    00038
    00039 unsigned int GameControlData::getOwnTeamNumber(Player player) const
    00040 {
    00041   if (data.teams[0].teamNumber == (uint32)player.getGameControllerTeamNumber()) return 0;
    00042   return 1;
    00043 }
    00044
    00045 unsigned int GameControlData::getOpponentTeamNumber(Player player) const
    00046 {
    00047   if (data.teams[0].teamNumber == (uint32)player.getGameControllerTeamNumber()) return 1;
    00048   return 0;
    00049 }
    00050 const TeamInfo& GameControlData::getOwnTeam(Player player) const
    00051 {
    00052   return data.teams[getOwnTeamNumber(player)];
    00053 }
    00054
    00055 TeamInfo& GameControlData::getOwnTeam(Player player)
    00056 {
    00057   return data.teams[getOwnTeamNumber(player)];
    00058 }
    00059
    00060 const TeamInfo& GameControlData::getOpponentTeam(Player player) const
    00061 {
    00062   return data.teams[getOpponentTeamNumber(player)];
    00063 }
    00064
    00065 TeamInfo& GameControlData::getOpponentTeam(Player player)
    00066 {
    00067   return data.teams[getOpponentTeamNumber(player)];
    00068 }
    00069
    00070 GameControlData::KickoffTeam GameControlData::getKickoffTeam(Player player) const
    00071 {
    00072    if (getOwnTeamNumber(player) == data.kickOffTeam)
    00073    {
    00074      return ownTeam;
    00075    }
    00076    else
    00077    {
    00078      return opponentTeam;
    00079    }
    00080 }
    00081
    00082 GameControlData::Penalties GameControlData::getPenalty(Player player) const
    00083 {
    00084    return (Penalties)(getRobot(player).penalty);
    00085 }
    00086
    00087
    00088 const RobotInfo& GameControlData::getRobot(Player player) const
    00089 {
    00090   return (getOwnTeam(player).players[(int)player.getPlayerNumber()]);
    00091 }
    00092
    00093 RobotInfo& GameControlData::getRobot(Player player)
    00094 {
    00095   return (getOwnTeam(player).players[(int)player.getPlayerNumber()]);
    00096 }
    00097
    00098 const TeamInfo& GameControlData::getTeamByNumber(int number) const
    00099 {
    00100   if (data.teams[0].teamNumber == (uint32)number) return data.teams[0];
    00101   return data.teams[1];
    00102 }
    00103
    00104 TeamInfo& GameControlData::getTeamByNumber(int number)
    00105 {
    00106   if (data.teams[0].teamNumber == (uint32)number) return data.teams[0];
    00107   return data.teams[1];
    00108 }
    00109
    00110 const TeamInfo& GameControlData::getTeamByColor(int color) const
    00111 {
    00112   if (data.teams[0].teamColour == (uint32)color) return data.teams[0];
    00113   return data.teams[1];
    00114 }
    00115
    00116 TeamInfo& GameControlData::getTeamByColor(int color)
    00117 {
    00118   if (data.teams[0].teamColour == (uint32)color) return data.teams[0];
    00119   return data.teams[1];
    00120 }
    00121
    00122 bool GameControlData::operator == (const GameControlData& other)
    00123 {
    00124   return memcmp(&data,&(other.data),sizeof(data)) == 0; // ist das so gewollt ?
    00125 }
    00126
    00127 const char* GameControlData::getPenaltyName(Penalties penalty)
    00128   {
    00129     switch (penalty)
    00130     {
    00131     case notPenalized: return "no penalty";
    00132     case illegalDefender: return "illegal defender";
    00133     case illegalDefense: return "illegal defense";
    00134     case obstruction: return "obstruction";
    00135     case goaliePushing: return "goalie pushing";
    00136     case playerPushing: return "field player pushing";
    00137     case ballHolding: return "ball holding";
    00138     case requestForPickup: return "request for pick-up";
    00139     default: return "unknown penalty";
    00140     }
    00141   }
    00142
    00143 const char* GameControlData::getStateName(State state)
    00144 {
    00145     switch (state)
    00146     {
    00147     case initial: return "initial";
    00148     case ready: return "ready";
    00149     case set: return "set";
    00150     case playing: return "playing";
    00151     case finished: return "finished";
    00152     default: return "unknown state";
    00153     }
    00154  }
    00155
    00156
    00157 const char* GameControlData::getKickoffTeamName(KickoffTeam kickoffTeam)
    00158 {
    00159   switch(kickoffTeam)
    00160   {
    00161       case ownTeam: return "own team";
    00162       case opponentTeam: return "opponent team";
    00163       default: return "unknown kickoff";
    00164   }
    00165 }
    00166
    00167 In& operator>>(In& stream,GameControlData& gameControlData)
    00168 {
    00169   if (gameControlData.switchOff)
    00170   {
    00171     stream.skip(sizeof(RoboCupGameControlData));
    00172     return stream;
    00173   }
    00174   RoboCupGameControlData data;
    00175   stream.read(&data,sizeof(RoboCupGameControlData));
    00176
    00177   /* Copy GameControlData only if the Robot is in one of the two teams and has the right header-string*/
    00178   if (  ((data.teams[0].teamNumber == (uint32)getPlayer().getGameControllerTeamNumber())  ||
    00179          (data.teams[1].teamNumber == (uint32)getPlayer().getGameControllerTeamNumber())) &&
    00180          (data.version == STRUCT_VERSION) &&
    00181          (strncmp(data.header,STRUCT_HEADER,4) == 0))
    00182   {
    00183     gameControlData.data = data;
    00184     gameControlData.timeStamp = SystemCall::getCurrentSystemTime();
    00185   }
    00186
    00187   return stream;
    00188 }
    00189
    00190 Out& operator<<(Out& stream, const GameControlData& gameControlData)
    00191 {
    00192   stream.write(&gameControlData.data,sizeof(RoboCupGameControlData));
    00193   return stream;
    00194 }