Fixes for datatype size on amd64.
[crack-attack.git] / src / ComputerPlayer.cxx
blobe7fabc342b45faf44d592f22ae299387b3a05fe6
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 "ComputerPlayer.h"
24 #include <cassert>
25 #include "LevelLights.h"
26 #include "Score.h"
29 //#define WAIT_TIME ( GC_STEPS_PER_SECOND * 10 )
31 bool ComputerPlayer::lost;
32 bool ComputerPlayer::_impact;
33 ComputerPlayerAI *ComputerPlayer::ai;
35 void ComputerPlayer::gameStart()
37 if (!(MetaState::mode & CM_AI))
38 return;
40 if ((MetaState::mode & CM_AI_EASY))
41 ai = new EasyAI();
42 if ((MetaState::mode & CM_AI_MEDIUM))
43 ai = new MediumAI();
44 if ((MetaState::mode & CM_AI_HARD))
45 ai = new HardAI();
47 assert(ai != NULL);
49 lost = false;
52 int ComputerPlayer::gameFinish()
54 return ai->determineLoss() ? GS_WON : GS_LOST;
57 void ComputerPlayer::timeStep()
59 static bool first_time = true;
60 if (!(MetaState::mode & CM_AI))
61 return;
62 if (!ai) {
63 return;
66 // handle the lights
67 LevelLights::handleAI();
69 ComputerPlayerAI &localAi = *ai;
70 if (first_time) {
71 MESSAGE("AI will drop again in " << ((localAi.alarm() - Game::time_step) / GC_STEPS_PER_SECOND) << " seconds");
72 LOG("AI will drop again in " << ((localAi.alarm() - Game::time_step) / GC_STEPS_PER_SECOND) << " seconds");
73 first_time = false;
75 if (Game::time_step >= localAi.alarm()) {
76 localAi.garbageAmount()->sendToGenerator();
77 #ifndef NDEBUG
78 std::cout << "init pop: " << GC_INITIAL_POP_DELAY << std::endl;
79 std::cout << "steps per second: " << GC_STEPS_PER_SECOND << std::endl;
80 std::cout << "Height: " << ai->garbageQueue()->height() << std::endl;
81 #endif
82 localAi.resetAlarm();
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");
86 if(localAi.determineLoss()) {
87 Game::aiPlayerLoss();
91 void ComputerPlayer::addGarbage ( int height, int width, int flavor ) {
92 assert(ai != NULL);
93 MESSAGE("Adding garbage to queue");
94 ai->garbageQueue()->add(height, width, flavor);
95 _impact = true;
98 bool ComputerPlayer::checkLevelLightDying()
100 int height = ai->garbageQueue()->height();
101 int ninety = (int)(ai->lossHeight() * .9);
102 if (ninety == ai->lossHeight())
103 ninety = ai->lossHeight() - 1;
104 if (height >= ninety)
105 return true;
106 return false;
109 double ComputerPlayer::lightPartition (int n)
111 static double max = LL_NUMBER_LEVEL_LIGHTS;
112 double lh = ai->lossHeight();
113 double partition = lh / max;
114 double colorh = n * partition;
115 return colorh;
118 bool ComputerPlayer::checkLevelLightBlue(int n)
120 if (lightPartition(n) >= ai->garbageQueue()->height())
121 return true;
122 else
123 return false;
126 int ComputerPlayer::findTopRed()
128 for (int i=0; i < LL_NUMBER_LEVEL_LIGHTS; ++i) {
129 if (lightPartition(i) >= ai->garbageQueue()->height())
130 return i;
132 return 0;
135 int ComputerPlayer::levelLightImpact ( )
137 if (impact(true))
138 return findTopRed();
140 return 0;
143 bool ComputerPlayer::impact (bool reset)
145 bool ret = _impact;
146 if (reset) _impact = false;
147 return ret;