SPYSpheres
ZRGameInternal.h
Go to the documentation of this file.
00001 #ifndef _ZR_GAME_INTERNAL_H_
00002 #define _ZR_GAME_INTERNAL_H_
00003 #pragma once
00004 
00005 #include "Constants.h"
00006 #include "ZeroRoboticsGameBaseImpl.hpp"
00007 #include "comm.h"
00008 
00009 //typedef unsigned long int u4;
00010 //typedef struct ranctx {u4 a;u4 b;u4 c;u4 d; } ranctx;
00011 //#define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
00012 
00013 
00014 ///Forward declaration of ZR Game
00015 class ZeroRoboticsGame;
00016 
00017 
00018 /**
00019  * This class defines the hidden game implementation. Any internal
00020  * utility functions or game ru//les functsions hould be put here.  You may also
00021  * override any of the standard GSP functions by implementing
00022  * them here.  The functions init(), update(), and sendDebug() are required.
00023  */
00024 
00025 class ZeroRoboticsGameImpl : public ZeroRoboticsGameBaseImpl<ZeroRoboticsGameImpl>
00026 {
00027 public:
00028   /**
00029    * (Required) Runs initialization fo the ZR game at the start of a test. Use this function
00030    * to reset all member variables.
00031    */
00032 
00033   struct PlayerInfo
00034   {
00035     float           zrState[12];
00036     state_vector    sphState;
00037     float     fuelUsed;
00038     float   energy;       // The amount of energy
00039     bool    collisionActive;    // true if collision avoidance is activated
00040     bool    FlareisActive;      // true if solar flare is active
00041     float     userForces[6];    // stores userForces for download
00042     float     initQuat[4];       // initial quaternion when picking up items
00043     float     score;      // current total score in the game
00044     short     message;  // message to send to other satellite
00045     bool    acquiringMP[NUM_ITEMS]; // true when rotating within item to pick it up (but not picked up yet)
00046     unsigned short  mpTime[NUM_ITEMS];  // time when an item is picked up
00047     short mirrorTime;
00048     int mirrors;
00049     float lastAttTarget[4];
00050     float lastPosTarget[4];
00051   };
00052 
00053   struct OtherInfo
00054   {
00055     float           zrState[12];
00056     state_vector    sphState;
00057     unsigned short  mpTime[NUM_ITEMS];    // time when other player picks up items
00058     float score;          // latest known score of other player
00059     short message;          // message from other player
00060     float energy;         
00061     short mirrorTime;
00062   };
00063 
00064   struct Light
00065   {
00066     float center;    // center of the grey zone in 2D/3D. In Alliance, it is the center of the grey zone that has a higher Y-position than the dark zone and a lower Y-position than the light zone.
00067     float greyWidth;    // the greyWidth of the grey zone (distance between light and dark)
00068     float lightWidth;  // how wide the light zone is
00069     int direction; // the direction of the light side.
00070     int nextSwitchTime; // Timestap at which light should be switched
00071   };
00072 
00073   struct Picture
00074   {
00075     bool  picStored;          // true when a picture is stored here
00076     float value;            // points value associated with the picture
00077     int   round;            // In what minute of the game the picture was taken
00078   };
00079 
00080   struct Camera
00081   {
00082     unsigned int  memoryFilled;           // amount of memory currently in use
00083     unsigned int   memorySize;              // total amount of memory available
00084     bool     cameraOn;      // true if camera is enabled; its disabled for 3 seconds after each picture is taken
00085     unsigned short  cameraOffTime;        // time when camera was turned off (picture was taken)
00086     bool            cameraBlocked;    // true if the other player's SPHERE is in the way of the camera
00087     Picture         memory[CAMERA_MAX_MEMORY];    // stores pictures taken by the camera
00088     bool            tookPicture;    // true if the player took a picture this turn
00089     bool            uploadedPictures;   // true if the player uploaded pictures this turn
00090   };
00091 
00092   struct ChallengeInfo
00093   {
00094     int currentTime;
00095     int random;
00096     float mpLoc[NUM_ITEMS][3];
00097     PlayerInfo me;
00098     OtherInfo other;
00099     Light light;
00100     Camera camera;
00101     int currentPicID;
00102     unsigned int round;               // round of lights switched
00103   };
00104 
00105   ChallengeInfo challInfo;
00106 
00107   void init(void);
00108 
00109 /**
00110    * (Required) Runs an update of the game rules and calls the user function loop(). The return
00111    * value for this function indicates if the forceTorqueOut vector should be mixed into
00112    * thruster firings.  The function should return 0 if the user does not activate any
00113    * ZR API movement commands or implements their own thruster firing logic.
00114    *
00115    * Here we update player's scores
00116    *
00117    * /param forceTorqueOut forces and torques to be mixed into thruster firings
00118    * /return return 1 if forceTorqueOut should be mixed into thruster firings and 0 otherwise
00119    */
00120 
00121   bool update(float forceTorqueOut[6]);
00122 
00123   bool updateGameModeManeuver(float forceTorqueOut[6]);
00124   bool updateGameOverManeuver(float forceTorqueOut[6], unsigned char& finalScore);
00125   void updateStates();
00126   /**
00127    * (Required) Called on every gspControl control cycle.  It should be used to send debug
00128    * and telemetry information to the ground.
00129    */
00130 
00131   bool enforceBoundaries(float forceTorque[6]);
00132 
00133   /**
00134   * Limits the direction in which the programmed SPHERE can travel and slows down the velocity if
00135   * it travels in the direction of the limit
00136   */
00137 
00138   void limitDirection(state_vector ctrlState, float ctrlControl[6], unsigned int idx, float dir);
00139 
00140   /**
00141   * (Required) Called on every gspControl control cycle, It should be used to send debug
00142   * and telemetry information to the ground.
00143   */
00144 
00145   void sendDebug(void);
00146 
00147   /**
00148   * for Corona SPHERES: sends POI & Solar Flare info to Sat 2 at start of game
00149   */
00150   void sendInit(void);
00151 
00152   /**
00153    * (Required) Processes SPHERES telemetry data
00154    */
00155 
00156   void processRXData(default_rfm_packet packet);
00157 
00158   /**
00159    * (Required) Initial state where the satellite is initialized.
00160    */
00161 
00162   static const state_vector initState;
00163 
00164   //returns damage aquired every second if not in shadow zone and not turned off
00165   void  breakTie();
00166 
00167   void modify2DForceTorque(float forceTorque[6]);
00168   //void getFlareTimes(int Array[8]);
00169 
00170   /********************asteroid functions ************************/
00171   void resolveCollision(float zrState[12], float forceTorqueOut[6]); //Edits forceTorqueOut to resolve the SPHERE's movement
00172   float dist3d(float* pos1, float* pos2) const;
00173   /************************camera funtions****************/
00174 
00175   bool      photoBomb(float zrState[12], float poiLoc[3]);                //checks to see if the other sphere is in the view of the camera
00176 
00177   void    initItems();                          //Set up item positions;
00178   float     randomizeStartingLocs(int itemID, int coord);
00179   bool    itemPickUp(int objectNum);                  // True if memory pack was picked up.
00180   void    initCamera();
00181   void    disableCamera();
00182   void    activateCamera();
00183   void    memoryLoss();                       // Called by the system if the SPHERE is hit by a solar flare.
00184   bool    isFacingPos(float position[]);
00185   bool    isFacingOther();
00186   bool    isFacingEarth();
00187   float    takePicImpl();
00188   float picPointsImpl();
00189   void    resetPicsTaken();
00190 
00191   /*
00192   * LIGHT FUNCTIONS
00193   */
00194 
00195   // returns 1 if in light zone, 0 if in grey zone, and -1 if in dark zone
00196   int sphereInSwitchingArea(float position[]);
00197   int sphereInMovingArea(float position[]);
00198 
00199   bool sphereInLight(float position[]);
00200   bool sphereInDark(float position[]);
00201   void switchLightDirection();
00202   void initLight();
00203   void updateLight();
00204 
00205   /*
00206   * ENERGY FUNCTIONS
00207   */
00208   void initEnergy();
00209   void updateEnergy();
00210   bool tryToUseEnergy(float amount);
00211 
00212   /**
00213    * Retrieves singleton instance of the game implementation
00214    * \return The game implementation singleton
00215    */
00216   static ZeroRoboticsGameImpl &instance();
00217 
00218   /**
00219    * Constructor for binding an API implementation
00220    */
00221   ZeroRoboticsGameImpl(ZeroRoboticsAPIImpl &apiImpl);
00222 
00223   ///Reference to ZR API instance
00224   ZeroRoboticsAPIImpl &apiImpl;
00225 
00226   ///Back pointer to the game instance
00227   ZeroRoboticsGame *game;
00228 
00229 };
00230 
00231 ZeroRoboticsGameImpl *getGameImpl();
00232 
00233 
00234 void overrideTarget2D(float target[13]);
00235 
00236 // void overrideTarget(float target[13]);
00237 void swingTwistDecomposition(float q[4], float q_sw[4], float q_tw[4], float sw_vec[3]);
00238 void quatToPositive(float q[4]);
00239 
00240 #endif