//Declare any variables shared between functions here
bool energyLeft;
unsigned short phaseOfGame;
float Y;
bool skip;
float itemPosition[3];
bool mirrorsLeft;
float currentTime;
float faceOther[3];
float distance;
bool gameOver;
float X;
bool itemsLeft;
float oZRState[12];
void setPos(float x, float y, float z) {
float pos[3];
pos[0] = x; pos[1] = y; pos[2] = z;
api.setPositionTarget(pos);
}
void init(){
//This function is called once when your code is first loaded.
Y = 0;
X = 0;
distance = 0;
faceOther[0] = 0;
faceOther[1] = 0;
faceOther[2] = 0;
itemPosition[0] = 0;
itemPosition[1] = 0;
itemPosition[2] = 0;
oZRState[0] = 0;
oZRState[1] = 0;
oZRState[2] = 0;
oZRState[3] = 0;
oZRState[4] = 0;
oZRState[5] = 0;
oZRState[6] = 0;
oZRState[7] = 0;
oZRState[8] = 0;
oZRState[9] = 0;
oZRState[10] = 0;
oZRState[11] = 0;
energyLeft = 1;
mirrorsLeft = 1;
itemsLeft = 1;
phaseOfGame = 0;
currentTime = 0;
skip = 0;
gameOver = 0;
//IMPORTANT: make sure to set any variables that need an initial value.
//Do not assume variables will be set to 0 automatically!
}
void loop(){
//This function is called once per second. Use it to control the satellite.
currentTime = api.getTime();
if (currentTime <= 60) {
phaseOfGame = 0;
} else if (currentTime > 60 && currentTime <= 150) {
phaseOfGame = 1;
} else if (currentTime > 150) {
phaseOfGame = 2;
}
if (phaseOfGame == 0) {
if (game.getEnergy() < 0.5) {
pickEnergy();
}
if (game.checkHaveItemNoOne(5) && currentTime <= 31) {
game.getItemLoc(itemPosition, 5);
api.setPositionTarget(itemPosition);
} else if (game.checkHaveItemNoOne(2) && (!game.checkHaveItemNoOne(5) || currentTime <= 50)) {
game.getItemLoc(itemPosition, 2);
api.setPositionTarget(itemPosition);
} else if (!game.checkHaveItemNoOne(2) || currentTime > 50) {
pickEnergy();
}
}
game.getAttToOther(faceOther);
api.setAttitudeTarget(faceOther);
if (game.getEnergy() > 2 && game.checkInLightOther() && game.isFacingOther() && game.isCameraOn()) {
if (phaseOfGame != 2) {
if (game.getPicPoints() > 0.01) {
game.takePic();
}
} else {
game.takePic();
}
}
if (phaseOfGame == 1) {
if (game.checkInDark() && currentTime > 140) {
setPos(0, -0.05, 0);
game.useMirror();
if (!itemsLeft) {
gameOver = 1;
}
}
if (energyLeft) {
pickEnergy();
} else if (mirrorsLeft && (game.checkHaveItem(0) || game.checkHaveItem(1))) {
pickMirrors();
} else {
setPos(0, -0.05, 0);
}
if (game.checkInDark() && currentTime > 148) {
game.useMirror();
}
if (game.checkInLight() && game.getOtherEnergy() > 0.9) {
game.useMirror();
}
if (!mirrorsLeft && !energyLeft && game.getNumMirrorsHeld() >= 1 && game.getEnergy() >= 1) {
pickItems();
} else {
if (!energyLeft && !mirrorsLeft) {
setPos(0, -0.05, 0);
if (game.checkInLightOther()) {
pickItems();
}
}
}
if (game.getNumMirrorsHeld() == 0 && (game.checkHaveItem(7) || game.checkHaveItem(8))) {
pickItems();
}
}
if (phaseOfGame == 2) {
if (game.getOtherEnergy() > 0.9 && game.checkInLight()) {
game.useMirror();
}
if (itemsLeft) {
pickItems();
} else if (game.checkHaveItemNoOne(3)) {
game.getItemLoc(itemPosition, 3);
api.setPositionTarget(itemPosition);
}
if (gameOver || game.getEnergy() < 0.5) {
setPos(-0.05, 0.05, 0);
}
}
}
void pickEnergy() {
if (energyLeft) {
if (!skip && game.checkHaveItemNoOne(0)) {
game.getItemLoc(itemPosition, 0);
api.getOtherZRState(oZRState);
X = itemPosition[0] - oZRState[0];
Y = itemPosition[1] - oZRState[1];
distance = sqrtf(X * X + Y * Y);
if (distance < 0.38) {
skip = 1;
} else {
api.setPositionTarget(itemPosition);
}
}
if (skip || !game.checkHaveItemNoOne(0) && game.checkHaveItemNoOne(1)) {
game.getItemLoc(itemPosition, 1);
api.setPositionTarget(itemPosition);
}
if (!game.checkHaveItemNoOne(0) && !game.checkHaveItemNoOne(1)) {
energyLeft = 0;
skip = 0;
}
}
}
//End page pickEnergy
//Begin page pickItems
void pickItems() {
if (itemsLeft == 1) {
if (game.checkHaveItemNoOne(4)) {
game.getItemLoc(itemPosition, 4);
} else if (game.checkHaveItemNoOne(5)) {
game.getItemLoc(itemPosition, 5);
} else if (game.checkHaveItemNoOne(6)) {
game.getItemLoc(itemPosition, 6);
} else {
itemsLeft = 0;
}
api.setPositionTarget(itemPosition);
}
}
//End page pickItems
//Begin page pickMirrors
void pickMirrors() {
if (game.getEnergy() > 0.5 && mirrorsLeft && game.checkInDark()) {
if (game.checkHaveItemNoOne(8)) {
if (game.checkHaveItem(1)) {
game.getItemLoc(itemPosition, 8);
api.setPositionTarget(itemPosition);
} else if (game.checkHaveItem(0)) {
if (!game.checkHaveItemNoOne(7)) {
game.getItemLoc(itemPosition, 8);
api.setPositionTarget(itemPosition);
}
}
}
if (game.checkHaveItemNoOne(7)) {
if (game.checkHaveItem(0)) {
game.getItemLoc(itemPosition, 7);
api.setPositionTarget(itemPosition);
} else if (game.checkHaveItem(1)) {
if (!game.checkHaveItemNoOne(8)) {
game.getItemLoc(itemPosition, 7);
api.setPositionTarget(itemPosition);
}
}
}
if (!game.checkHaveItemNoOne(7) && !game.checkHaveItemNoOne(8)) {
mirrorsLeft = 0;
}
}
}
|