SPYSpheres
ZRLight.cpp
Go to the documentation of this file.
00001 //#include <string.h>
00002 #include <math.h>
00003 #include <cmath>
00004 #include <cstdlib>
00005 #include "ZRGame.h"
00006 #include "ZRGameInternal.h"
00007 /*#include <algorithm>*/
00008 
00009 /*#ifdef ZRSIMULATION
00010 #include "mex.h"
00011 #define GAME_TRACE(arg) DEBUG(arg)
00012 #else
00013 #define GAME_TRACE(arg)
00014 #endif*/
00015 
00016 void ZeroRoboticsGameImpl::initLight() {
00017   challInfo.light.direction = 1;
00018   #ifdef ALLIANCE
00019 
00020     #if (SPHERE_ID == SPHERE1)
00021       //float s = ((float) rand()) / ((float) RAND_MAX);
00022       challInfo.light.center = -0.2;  //s * 1.6 - 0.8;
00023       challInfo.light.nextSwitchTime = -1; // shouldn't be utilized
00024     #endif
00025 
00026     challInfo.light.greyWidth = LIGHT_GREY_WIDTH*.5;
00027     challInfo.light.lightWidth = LIGHT_WIDTH;
00028   #else
00029     #if (SPHERE_ID == SPHERE1)
00030       challInfo.light.center = 0.0;
00031       challInfo.light.nextSwitchTime = ((unsigned int) rand()) % 30 + 30;
00032     #endif
00033 
00034     challInfo.light.greyWidth = LIGHT_GREY_WIDTH;
00035   #endif
00036 }
00037 
00038 void ZeroRoboticsGameImpl::updateLight() {
00039   #ifdef ALLIANCE
00040 
00041     challInfo.light.center += LIGHT_SPEED;
00042     if (challInfo.light.center > ZONE_pY) {
00043       challInfo.light.center -= 2*ZONE_pY;
00044     }
00045 
00046   #else
00047 
00048     if (challInfo.currentTime < challInfo.light.nextSwitchTime) return;
00049 
00050     GAME_TRACE(("Light switching now"));
00051     switchLightDirection();
00052     challInfo.light.nextSwitchTime = challInfo.currentTime + LIGHT_SWITCH_PERIOD;
00053 
00054   #endif
00055 }
00056 
00057 void ZeroRoboticsGameImpl::switchLightDirection() {
00058   challInfo.light.direction *= -1;
00059 }
00060 
00061 int ZeroRoboticsGameImpl::sphereInSwitchingArea(float position[]) {
00062   float center = challInfo.light.center;
00063   float diff = position[1] - center;
00064   diff *= challInfo.light.direction; // adjust for light direction
00065 
00066   if (diff > -challInfo.light.greyWidth/2 && diff < challInfo.light.greyWidth/2) return 0;
00067   if (diff < 0) return -1;
00068   return 1;
00069 }
00070 
00071 int ZeroRoboticsGameImpl::sphereInMovingArea(float position[])
00072 {
00073 
00074   float interface = challInfo.light.center; // Start of Light Zone
00075   float outerface = interface + challInfo.light.lightWidth; // End of light zone
00076   if(outerface > ZONE_pY)
00077     outerface -= 2*ZONE_pY; // Wraparound
00078   float halfWidth = challInfo.light.greyWidth*.5; // Grey width on either side
00079   if ((interface - halfWidth <= position[1] && interface + halfWidth >= position[1]) || // In the first grey zone
00080       (outerface - halfWidth <= position[1] && outerface + halfWidth >= position[1]) || // In the second gray zone
00081       (position[0] >= ZONE_pX || position[1] >= ZONE_pY || position[2] >= ZONE_pZ) || // Out of bounds positive
00082       (position[0] <= ZONE_nX || position[1] <= ZONE_nY || position[2] <= ZONE_nZ)) // Out of bounds negatively
00083   {
00084     return 0; // Gray 
00085   }
00086   else if ((outerface > position[1] && (interface < position[1] || outerface < interface)) || // before the end of the light zone and Either after the beginning or the end has wrapped around
00087           (interface < position[1] && outerface < interface))  // or it's after the beginning and the end has wrapped
00088   {
00089     return 1; //Light
00090   }
00091 
00092   return -1; // Dark
00093 }
00094 
00095 bool ZeroRoboticsGameImpl::sphereInLight(float position[]) {
00096   #ifdef ALLIANCE
00097   return sphereInMovingArea(position) == 1;
00098   #else
00099   return sphereInSwitchingArea(position) == 1;
00100   #endif
00101 }
00102 
00103 bool ZeroRoboticsGameImpl::sphereInDark(float position[]) {
00104   #ifdef ALLIANCE
00105   return sphereInMovingArea(position) == -1;
00106   #else
00107   return sphereInSwitchingArea(position) == -1;
00108   #endif
00109 }
00110 
00111 float ZeroRoboticsGame::getLightSwitchTime() {
00112   return pimpl.challInfo.light.nextSwitchTime - pimpl.challInfo.currentTime;
00113 }
00114 float ZeroRoboticsGame::getLightInterfacePosition()
00115 {
00116   return pimpl.challInfo.light.center;
00117 }
00118 float ZeroRoboticsGame::getLightGreyBoundary()
00119 {
00120   return pimpl.challInfo.light.center + pimpl.challInfo.light.direction * LIGHT_GREY_WIDTH * 0.5;
00121 }
00122 float ZeroRoboticsGame::getDarkGreyBoundary()
00123 {
00124   return pimpl.challInfo.light.center - pimpl.challInfo.light.direction * LIGHT_GREY_WIDTH * 0.5;
00125 }
00126 
00127 int ZeroRoboticsGame::posInArea(float position[]){
00128   #ifdef ALLIANCE
00129   return pimpl.sphereInMovingArea(position);
00130   #else
00131   return pimpl.sphereInSwitchingArea(position);
00132   #endif  
00133 }
00134 
00135 bool ZeroRoboticsGame::posInLight(float position[]) {
00136   return pimpl.sphereInLight(position);
00137 }
00138 
00139 bool ZeroRoboticsGame::posInDark(float position[]){
00140   return pimpl.sphereInDark(position);
00141 }
00142 
00143 bool ZeroRoboticsGame::posInGrey(float position[]){
00144   return !pimpl.sphereInDark(position) && !pimpl.sphereInLight(position);
00145 }
00146