GameStateInterface.cpp

00001
00002 /***************************************************************************
00003  *  GameStateInterface.cpp - Fawkes BlackBoard Interface - GameStateInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008  Tim Niemueller
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <interfaces/GameStateInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class GameStateInterface <interfaces/GameStateInterface.h>
00034  * GameStateInterface Fawkes BlackBoard Interface.
00035  * 
00036       This interface provides access to the current game state. It is closely related to
00037       the WorldInfo network protocol.
00038       @see WorldInfoTransceiver
00039     
00040  * @ingroup FawkesInterfaces
00041  */
00042
00043 
00044 /** GS_FROZEN constant */
00045 const unsigned int GameStateInterface::GS_FROZEN = 0;
00046 /** GS_PLAY constant */
00047 const unsigned int GameStateInterface::GS_PLAY = 1;
00048 /** GS_KICK_OFF constant */
00049 const unsigned int GameStateInterface::GS_KICK_OFF = 2;
00050 /** GS_DROP_BALL constant */
00051 const unsigned int GameStateInterface::GS_DROP_BALL = 3;
00052 /** GS_PENALTY constant */
00053 const unsigned int GameStateInterface::GS_PENALTY = 4;
00054 /** GS_CORNER_KICK constant */
00055 const unsigned int GameStateInterface::GS_CORNER_KICK = 5;
00056 /** GS_THROW_IN constant */
00057 const unsigned int GameStateInterface::GS_THROW_IN = 6;
00058 /** GS_FREE_KICK constant */
00059 const unsigned int GameStateInterface::GS_FREE_KICK = 7;
00060 /** GS_GOAL_KICK constant */
00061 const unsigned int GameStateInterface::GS_GOAL_KICK = 8;
00062 /** GS_HALF_TIME constant */
00063 const unsigned int GameStateInterface::GS_HALF_TIME = 9;
00064 /** GS_SPL_INITIAL constant */
00065 const unsigned int GameStateInterface::GS_SPL_INITIAL = 0;
00066 /** GS_SPL_READY constant */
00067 const unsigned int GameStateInterface::GS_SPL_READY = 1;
00068 /** GS_SPL_SET constant */
00069 const unsigned int GameStateInterface::GS_SPL_SET = 2;
00070 /** GS_SPL_PLAY constant */
00071 const unsigned int GameStateInterface::GS_SPL_PLAY = 3;
00072 /** GS_SPL_FINISHED constant */
00073 const unsigned int GameStateInterface::GS_SPL_FINISHED = 4;
00074 
00075 /** Constructor */
00076 GameStateInterface::GameStateInterface() : Interface()
00077 {
00078   data_size = sizeof(GameStateInterface_data_t);
00079   data_ptr  = malloc(data_size);
00080   data      = (GameStateInterface_data_t *)data_ptr;
00081   memset(data_ptr, 0, data_size);
00082   add_fieldinfo(IFT_UINT, "game_state", 1, &data->game_state);
00083   add_fieldinfo(IFT_BOOL, "kickoff", 1, &data->kickoff);
00084   add_fieldinfo(IFT_UINT, "score_cyan", 1, &data->score_cyan);
00085   add_fieldinfo(IFT_UINT, "score_magenta", 1, &data->score_magenta);
00086   add_messageinfo("SetTeamColorMessage");
00087   add_messageinfo("SetKickoffMessage");
00088   add_messageinfo("SetStateTeamMessage");
00089   unsigned char tmp_hash[] = {0x15, 0x3, 0x49, 0xf9, 0x8c, 0x4b, 0x6d, 0x2, 0xac, 0x6a, 0xab, 0xb6, 0xde, 0x8b, 0x31, 0x92};
00090   set_hash(tmp_hash);
00091 }
00092 
00093 /** Destructor */
00094 GameStateInterface::~GameStateInterface()
00095 {
00096   free(data_ptr);
00097 }
00098 /* Methods */
00099 /** Get game_state value.
00100  * Current game state
00101  * @return game_state value
00102  */
00103 unsigned int
00104 GameStateInterface::game_state() const
00105 {
00106   return data->game_state;
00107 }
00108 
00109 /** Get maximum length of game_state value.
00110  * @return length of game_state value, can be length of the array or number of 
00111  * maximum number of characters for a string
00112  */
00113 size_t
00114 GameStateInterface::maxlenof_game_state() const
00115 {
00116   return 1;
00117 }
00118 
00119 /** Set game_state value.
00120  * Current game state
00121  * @param new_game_state new game_state value
00122  */
00123 void
00124 GameStateInterface::set_game_state(const unsigned int new_game_state)
00125 {
00126   data->game_state = new_game_state;
00127 }
00128 
00129 /** Get state_team value.
00130  * Team referred to by game state
00131  * @return state_team value
00132  */
00133 GameStateInterface::if_gamestate_team_t
00134 GameStateInterface::state_team() const
00135 {
00136   return data->state_team;
00137 }
00138 
00139 /** Get maximum length of state_team value.
00140  * @return length of state_team value, can be length of the array or number of 
00141  * maximum number of characters for a string
00142  */
00143 size_t
00144 GameStateInterface::maxlenof_state_team() const
00145 {
00146   return 1;
00147 }
00148 
00149 /** Set state_team value.
00150  * Team referred to by game state
00151  * @param new_state_team new state_team value
00152  */
00153 void
00154 GameStateInterface::set_state_team(const if_gamestate_team_t new_state_team)
00155 {
00156   data->state_team = new_state_team;
00157 }
00158 
00159 /** Get our_team value.
00160  * Our team color
00161  * @return our_team value
00162  */
00163 GameStateInterface::if_gamestate_team_t
00164 GameStateInterface::our_team() const
00165 {
00166   return data->our_team;
00167 }
00168 
00169 /** Get maximum length of our_team value.
00170  * @return length of our_team value, can be length of the array or number of 
00171  * maximum number of characters for a string
00172  */
00173 size_t
00174 GameStateInterface::maxlenof_our_team() const
00175 {
00176   return 1;
00177 }
00178 
00179 /** Set our_team value.
00180  * Our team color
00181  * @param new_our_team new our_team value
00182  */
00183 void
00184 GameStateInterface::set_our_team(const if_gamestate_team_t new_our_team)
00185 {
00186   data->our_team = new_our_team;
00187 }
00188 
00189 /** Get our_goal_color value.
00190  * Our own goal color
00191  * @return our_goal_color value
00192  */
00193 GameStateInterface::if_gamestate_goalcolor_t
00194 GameStateInterface::our_goal_color() const
00195 {
00196   return data->our_goal_color;
00197 }
00198 
00199 /** Get maximum length of our_goal_color value.
00200  * @return length of our_goal_color value, can be length of the array or number of 
00201  * maximum number of characters for a string
00202  */
00203 size_t
00204 GameStateInterface::maxlenof_our_goal_color() const
00205 {
00206   return 1;
00207 }
00208 
00209 /** Set our_goal_color value.
00210  * Our own goal color
00211  * @param new_our_goal_color new our_goal_color value
00212  */
00213 void
00214 GameStateInterface::set_our_goal_color(const if_gamestate_goalcolor_t new_our_goal_color)
00215 {
00216   data->our_goal_color = new_our_goal_color;
00217 }
00218 
00219 /** Get half value.
00220  * Current game half
00221  * @return half value
00222  */
00223 GameStateInterface::if_gamestate_half_t
00224 GameStateInterface::half() const
00225 {
00226   return data->half;
00227 }
00228 
00229 /** Get maximum length of half value.
00230  * @return length of half value, can be length of the array or number of 
00231  * maximum number of characters for a string
00232  */
00233 size_t
00234 GameStateInterface::maxlenof_half() const
00235 {
00236   return 1;
00237 }
00238 
00239 /** Set half value.
00240  * Current game half
00241  * @param new_half new half value
00242  */
00243 void
00244 GameStateInterface::set_half(const if_gamestate_half_t new_half)
00245 {
00246   data->half = new_half;
00247 }
00248 
00249 /** Get kickoff value.
00250  * Whether we have kickoff
00251  * @return kickoff value
00252  */
00253 bool
00254 GameStateInterface::is_kickoff() const
00255 {
00256   return data->kickoff;
00257 }
00258 
00259 /** Get maximum length of kickoff value.
00260  * @return length of kickoff value, can be length of the array or number of 
00261  * maximum number of characters for a string
00262  */
00263 size_t
00264 GameStateInterface::maxlenof_kickoff() const
00265 {
00266   return 1;
00267 }
00268 
00269 /** Set kickoff value.
00270  * Whether we have kickoff
00271  * @param new_kickoff new kickoff value
00272  */
00273 void
00274 GameStateInterface::set_kickoff(const bool new_kickoff)
00275 {
00276   data->kickoff = new_kickoff;
00277 }
00278 
00279 /** Get role value.
00280  * Current role of this robot
00281  * @return role value
00282  */
00283 GameStateInterface::if_gamestate_role_t
00284 GameStateInterface::role() const
00285 {
00286   return data->role;
00287 }
00288 
00289 /** Get maximum length of role value.
00290  * @return length of role value, can be length of the array or number of 
00291  * maximum number of characters for a string
00292  */
00293 size_t
00294 GameStateInterface::maxlenof_role() const
00295 {
00296   return 1;
00297 }
00298 
00299 /** Set role value.
00300  * Current role of this robot
00301  * @param new_role new role value
00302  */
00303 void
00304 GameStateInterface::set_role(const if_gamestate_role_t new_role)
00305 {
00306   data->role = new_role;
00307 }
00308 
00309 /** Get score_cyan value.
00310  * Score of team cyan
00311  * @return score_cyan value
00312  */
00313 unsigned int
00314 GameStateInterface::score_cyan() const
00315 {
00316   return data->score_cyan;
00317 }
00318 
00319 /** Get maximum length of score_cyan value.
00320  * @return length of score_cyan value, can be length of the array or number of 
00321  * maximum number of characters for a string
00322  */
00323 size_t
00324 GameStateInterface::maxlenof_score_cyan() const
00325 {
00326   return 1;
00327 }
00328 
00329 /** Set score_cyan value.
00330  * Score of team cyan
00331  * @param new_score_cyan new score_cyan value
00332  */
00333 void
00334 GameStateInterface::set_score_cyan(const unsigned int new_score_cyan)
00335 {
00336   data->score_cyan = new_score_cyan;
00337 }
00338 
00339 /** Get score_magenta value.
00340  * Score of team magenta
00341  * @return score_magenta value
00342  */
00343 unsigned int
00344 GameStateInterface::score_magenta() const
00345 {
00346   return data->score_magenta;
00347 }
00348 
00349 /** Get maximum length of score_magenta value.
00350  * @return length of score_magenta value, can be length of the array or number of 
00351  * maximum number of characters for a string
00352  */
00353 size_t
00354 GameStateInterface::maxlenof_score_magenta() const
00355 {
00356   return 1;
00357 }
00358 
00359 /** Set score_magenta value.
00360  * Score of team magenta
00361  * @param new_score_magenta new score_magenta value
00362  */
00363 void
00364 GameStateInterface::set_score_magenta(const unsigned int new_score_magenta)
00365 {
00366   data->score_magenta = new_score_magenta;
00367 }
00368
00369 /* =========== message create =========== */
00370 Message *
00371 GameStateInterface::create_message(const char *type) const
00372 {
00373   if ( strncmp("SetTeamColorMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00374     return new SetTeamColorMessage();
00375   } else if ( strncmp("SetKickoffMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00376     return new SetKickoffMessage();
00377   } else if ( strncmp("SetStateTeamMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00378     return new SetStateTeamMessage();
00379   } else {
00380     throw UnknownTypeException("The given type '%s' does not match any known "
00381                                "message type for this interface type.", type);
00382   }
00383 }
00384
00385 
00386 /** Copy values from other interface.
00387  * @param other other interface to copy values from
00388  */
00389 void
00390 GameStateInterface::copy_values(const Interface *other)
00391 {
00392   const GameStateInterface *oi = dynamic_cast<const GameStateInterface *>(other);
00393   if (oi == NULL) {
00394     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00395                                 type(), other->type());
00396   }
00397   memcpy(data, oi->data, sizeof(GameStateInterface_data_t));
00398 }
00399
00400 /* =========== messages =========== */
00401 /** @class GameStateInterface::SetTeamColorMessage <interfaces/GameStateInterface.h>
00402  * SetTeamColorMessage Fawkes BlackBoard Interface Message.
00403  * 
00404     
00405  */
00406
00407 
00408 /** Constructor with initial values.
00409  * @param ini_our_team initial value for our_team
00410  */
00411 GameStateInterface::SetTeamColorMessage::SetTeamColorMessage(const if_gamestate_team_t ini_our_team) : Message("SetTeamColorMessage")
00412 {
00413   data_size = sizeof(SetTeamColorMessage_data_t);
00414   data_ptr  = malloc(data_size);
00415   memset(data_ptr, 0, data_size);
00416   data      = (SetTeamColorMessage_data_t *)data_ptr;
00417   data->our_team = ini_our_team;
00418 }
00419 /** Constructor */
00420 GameStateInterface::SetTeamColorMessage::SetTeamColorMessage() : Message("SetTeamColorMessage")
00421 {
00422   data_size = sizeof(SetTeamColorMessage_data_t);
00423   data_ptr  = malloc(data_size);
00424   memset(data_ptr, 0, data_size);
00425   data      = (SetTeamColorMessage_data_t *)data_ptr;
00426 }
00427 
00428 /** Destructor */
00429 GameStateInterface::SetTeamColorMessage::~SetTeamColorMessage()
00430 {
00431   free(data_ptr);
00432 }
00433 
00434 /** Copy constructor.
00435  * @param m message to copy from
00436  */
00437 GameStateInterface::SetTeamColorMessage::SetTeamColorMessage(const SetTeamColorMessage *m) : Message("SetTeamColorMessage")
00438 {
00439   data_size = m->data_size;
00440   data_ptr  = malloc(data_size);
00441   memcpy(data_ptr, m->data_ptr, data_size);
00442   data      = (SetTeamColorMessage_data_t *)data_ptr;
00443 }
00444
00445 /* Methods */
00446 /** Get our_team value.
00447  * Our team color
00448  * @return our_team value
00449  */
00450 GameStateInterface::if_gamestate_team_t
00451 GameStateInterface::SetTeamColorMessage::our_team() const
00452 {
00453   return data->our_team;
00454 }
00455 
00456 /** Get maximum length of our_team value.
00457  * @return length of our_team value, can be length of the array or number of 
00458  * maximum number of characters for a string
00459  */
00460 size_t
00461 GameStateInterface::SetTeamColorMessage::maxlenof_our_team() const
00462 {
00463   return 1;
00464 }
00465 
00466 /** Set our_team value.
00467  * Our team color
00468  * @param new_our_team new our_team value
00469  */
00470 void
00471 GameStateInterface::SetTeamColorMessage::set_our_team(const if_gamestate_team_t new_our_team)
00472 {
00473   data->our_team = new_our_team;
00474 }
00475 
00476 /** Clone this message.
00477  * Produces a message of the same type as this message and copies the
00478  * data to the new message.
00479  * @return clone of this message
00480  */
00481 Message *
00482 GameStateInterface::SetTeamColorMessage::clone() const
00483 {
00484   return new GameStateInterface::SetTeamColorMessage(this);
00485 }
00486 /** @class GameStateInterface::SetKickoffMessage <interfaces/GameStateInterface.h>
00487  * SetKickoffMessage Fawkes BlackBoard Interface Message.
00488  * 
00489     
00490  */
00491
00492 
00493 /** Constructor with initial values.
00494  * @param ini_kickoff initial value for kickoff
00495  */
00496 GameStateInterface::SetKickoffMessage::SetKickoffMessage(const bool ini_kickoff) : Message("SetKickoffMessage")
00497 {
00498   data_size = sizeof(SetKickoffMessage_data_t);
00499   data_ptr  = malloc(data_size);
00500   memset(data_ptr, 0, data_size);
00501   data      = (SetKickoffMessage_data_t *)data_ptr;
00502   data->kickoff = ini_kickoff;
00503   add_fieldinfo(IFT_BOOL, "kickoff", 1, &data->kickoff);
00504 }
00505 /** Constructor */
00506 GameStateInterface::SetKickoffMessage::SetKickoffMessage() : Message("SetKickoffMessage")
00507 {
00508   data_size = sizeof(SetKickoffMessage_data_t);
00509   data_ptr  = malloc(data_size);
00510   memset(data_ptr, 0, data_size);
00511   data      = (SetKickoffMessage_data_t *)data_ptr;
00512   add_fieldinfo(IFT_BOOL, "kickoff", 1, &data->kickoff);
00513 }
00514 
00515 /** Destructor */
00516 GameStateInterface::SetKickoffMessage::~SetKickoffMessage()
00517 {
00518   free(data_ptr);
00519 }
00520 
00521 /** Copy constructor.
00522  * @param m message to copy from
00523  */
00524 GameStateInterface::SetKickoffMessage::SetKickoffMessage(const SetKickoffMessage *m) : Message("SetKickoffMessage")
00525 {
00526   data_size = m->data_size;
00527   data_ptr  = malloc(data_size);
00528   memcpy(data_ptr, m->data_ptr, data_size);
00529   data      = (SetKickoffMessage_data_t *)data_ptr;
00530 }
00531
00532 /* Methods */
00533 /** Get kickoff value.
00534  * Whether we have kickoff
00535  * @return kickoff value
00536  */
00537 bool
00538 GameStateInterface::SetKickoffMessage::is_kickoff() const
00539 {
00540   return data->kickoff;
00541 }
00542 
00543 /** Get maximum length of kickoff value.
00544  * @return length of kickoff value, can be length of the array or number of 
00545  * maximum number of characters for a string
00546  */
00547 size_t
00548 GameStateInterface::SetKickoffMessage::maxlenof_kickoff() const
00549 {
00550   return 1;
00551 }
00552 
00553 /** Set kickoff value.
00554  * Whether we have kickoff
00555  * @param new_kickoff new kickoff value
00556  */
00557 void
00558 GameStateInterface::SetKickoffMessage::set_kickoff(const bool new_kickoff)
00559 {
00560   data->kickoff = new_kickoff;
00561 }
00562 
00563 /** Clone this message.
00564  * Produces a message of the same type as this message and copies the
00565  * data to the new message.
00566  * @return clone of this message
00567  */
00568 Message *
00569 GameStateInterface::SetKickoffMessage::clone() const
00570 {
00571   return new GameStateInterface::SetKickoffMessage(this);
00572 }
00573 /** @class GameStateInterface::SetStateTeamMessage <interfaces/GameStateInterface.h>
00574  * SetStateTeamMessage Fawkes BlackBoard Interface Message.
00575  * 
00576     
00577  */
00578
00579 
00580 /** Constructor with initial values.
00581  * @param ini_state_team initial value for state_team
00582  */
00583 GameStateInterface::SetStateTeamMessage::SetStateTeamMessage(const if_gamestate_team_t ini_state_team) : Message("SetStateTeamMessage")
00584 {
00585   data_size = sizeof(SetStateTeamMessage_data_t);
00586   data_ptr  = malloc(data_size);
00587   memset(data_ptr, 0, data_size);
00588   data      = (SetStateTeamMessage_data_t *)data_ptr;
00589   data->state_team = ini_state_team;
00590 }
00591 /** Constructor */
00592 GameStateInterface::SetStateTeamMessage::SetStateTeamMessage() : Message("SetStateTeamMessage")
00593 {
00594   data_size = sizeof(SetStateTeamMessage_data_t);
00595   data_ptr  = malloc(data_size);
00596   memset(data_ptr, 0, data_size);
00597   data      = (SetStateTeamMessage_data_t *)data_ptr;
00598 }
00599 
00600 /** Destructor */
00601 GameStateInterface::SetStateTeamMessage::~SetStateTeamMessage()
00602 {
00603   free(data_ptr);
00604 }
00605 
00606 /** Copy constructor.
00607  * @param m message to copy from
00608  */
00609 GameStateInterface::SetStateTeamMessage::SetStateTeamMessage(const SetStateTeamMessage *m) : Message("SetStateTeamMessage")
00610 {
00611   data_size = m->data_size;
00612   data_ptr  = malloc(data_size);
00613   memcpy(data_ptr, m->data_ptr, data_size);
00614   data      = (SetStateTeamMessage_data_t *)data_ptr;
00615 }
00616
00617 /* Methods */
00618 /** Get state_team value.
00619  * Team referred to by game state
00620  * @return state_team value
00621  */
00622 GameStateInterface::if_gamestate_team_t
00623 GameStateInterface::SetStateTeamMessage::state_team() const
00624 {
00625   return data->state_team;
00626 }
00627 
00628 /** Get maximum length of state_team value.
00629  * @return length of state_team value, can be length of the array or number of 
00630  * maximum number of characters for a string
00631  */
00632 size_t
00633 GameStateInterface::SetStateTeamMessage::maxlenof_state_team() const
00634 {
00635   return 1;
00636 }
00637 
00638 /** Set state_team value.
00639  * Team referred to by game state
00640  * @param new_state_team new state_team value
00641  */
00642 void
00643 GameStateInterface::SetStateTeamMessage::set_state_team(const if_gamestate_team_t new_state_team)
00644 {
00645   data->state_team = new_state_team;
00646 }
00647 
00648 /** Clone this message.
00649  * Produces a message of the same type as this message and copies the
00650  * data to the new message.
00651  * @return clone of this message
00652  */
00653 Message *
00654 GameStateInterface::SetStateTeamMessage::clone() const
00655 {
00656   return new GameStateInterface::SetStateTeamMessage(this);
00657 }
00658 /** Check if message is valid and can be enqueued.
00659  * @param message Message to check
00660  */
00661 bool
00662 GameStateInterface::message_valid(const Message *message) const
00663 {
00664   const SetTeamColorMessage *m0 = dynamic_cast<const SetTeamColorMessage *>(message);
00665   if ( m0 != NULL ) {
00666     return true;
00667   }
00668   const SetKickoffMessage *m1 = dynamic_cast<const SetKickoffMessage *>(message);
00669   if ( m1 != NULL ) {
00670     return true;
00671   }
00672   const SetStateTeamMessage *m2 = dynamic_cast<const SetStateTeamMessage *>(message);
00673   if ( m2 != NULL ) {
00674     return true;
00675   }
00676   return false;
00677 }
00678 
00679 /// @cond INTERNALS
00680 EXPORT_INTERFACE(GameStateInterface)
00681 /// @endcond
00682 
00683
00684 } // end namespace fawkes