Copyright updates. I hope that I didn't miss anybody.
[crack-attack.git] / src / ComputerPlayer.cxx
blob1b77a26dbdb9448962b1502317ec60ffbcc40a20
1 /*
2 * ComputerPlayer.cxx
3 * Andrew Sayman - 3/27/05
5 * Copyright (C) 2005 Andrew Sayman
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "LevelLights.h"
23 #include "ComputerPlayer.h"
24 #include "Score.h"
26 using namespace std;
28 //#define WAIT_TIME ( GC_STEPS_PER_SECOND * 10 )
30 bool ComputerPlayer::lost;
31 ComputerPlayerAI *ComputerPlayer::ai;
33 void ComputerPlayer::gameStart()
35 if (!(MetaState::mode & CM_AI))
36 return;
37 //queue = new GarbageQueue();
39 if ((MetaState::mode & CM_AI_EASY))
40 ai = new EasyAI();
41 if ((MetaState::mode & CM_AI_MEDIUM))
42 ai = new MediumAI();
43 if ((MetaState::mode & CM_AI_HARD))
44 ai = new HardAI();
46 if (ai)
47 ai->garbageQueue(queue);
49 assert(ai != NULL);
51 lost = false;
54 static void show_element (GarbageQueueElement *e) {
55 #ifndef NDEBUG
56 printf("Element: %p h %d w %d f %d\n",
58 e->height,
59 e->width,
60 e->flavor);
61 #endif
64 int ComputerPlayer::gameFinish()
66 return ai->determineLoss() ? GS_WON : GS_LOST;
69 void ComputerPlayer::timeStep()
71 static bool first_time = true;
72 if (!(MetaState::mode & CM_AI))
73 return;
74 if (!ai) {
75 return;
78 // handle the lights
79 LevelLights::handleAI();
81 ComputerPlayerAI &localAi = *ai;
82 if (first_time) {
83 MESSAGE("AI will drop again in " << ((localAi.alarm() - Game::time_step) / GC_STEPS_PER_SECOND) << " seconds");
84 LOG("AI will drop again in " << ((localAi.alarm() - Game::time_step) / GC_STEPS_PER_SECOND) << " seconds");
85 first_time = false;
87 if (Game::time_step >= localAi.alarm()) {
88 //localAi.garbageQueue(queue);
89 localAi.garbageAmount()->sendToGenerator();//garbage_queue);
90 #ifndef NDEBUG
91 cout << "init pop: " << GC_INITIAL_POP_DELAY << endl;
92 cout << "steps per second: " << GC_STEPS_PER_SECOND << endl;
93 cout << "Height: " << ai->garbageQueue()->height() << endl;
94 #endif
95 //delete tmp;
96 //queue->reset();
97 localAi.resetAlarm();
98 MESSAGE("AI will drop again in " << ((localAi.alarm() - Game::time_step) / GC_STEPS_PER_SECOND) << " seconds");
99 LOG("AI will drop again in " << ((localAi.alarm() - Game::time_step) / GC_STEPS_PER_SECOND) << " seconds");
101 if(localAi.determineLoss()) {
102 Game::aiPlayerLoss();
106 void ComputerPlayer::addGarbage ( int height, int width, int flavor ) {
107 assert(ai != NULL);
108 MESSAGE("Adding garbage to queue");
109 ai->garbageQueue()->add(height, width, flavor);
112 bool ComputerPlayer::checkLevelLightDying()
114 int height = ai->garbageQueue()->height();
115 int ninety = ai->lossHeight() * .9;
116 //MESSAGE("ninety " << ninety);
117 if (ninety == ai->lossHeight())
118 ninety = ai->lossHeight() - 1;
119 if (height >= ninety)
120 return true;
121 return false;
124 bool ComputerPlayer::checkLevelLightBlue(int n)
126 double max = LL_NUMBER_LEVEL_LIGHTS;
127 double lh = ai->lossHeight();
128 //MESSAGE("lh " << lh << " max " << max);
129 double partition = lh / max;
130 double colorh = n * partition;
131 //MESSAGE("n " << n << " partition " << partition << " colorh " << colorh << " height " << ai->garbageQueue()->height());
132 if (colorh >= ai->garbageQueue()->height())
133 return true;
134 else
135 return false;