Each module now includes its own header first. Removed sstream.h and the definition...
[crack-attack.git] / src / ComputerPlayerAI.cxx
blob7013d7e3de24cb0b33693095eaf05423b6492e77
1 /*
2 * ComputerPlayerAI.cxx
3 * Andrew Sayman - 3/27/05
5 * Copyright (C) 2005 Andrew Sayman
6 * Copyright (C) 2005 Kevin Webb
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "ComputerPlayerAI.h"
25 #include "Score.h"
26 #include "Garbage.h"
27 #include "GarbageGenerator.h"
28 #include "Game.h"
30 #include <cassert>
32 #define loop(v,m) for(int v = 0; v<(m); v++)
33 #define loopi(m) loop(i,m)
35 /* Begin ComputerPlayerAI */
36 ComputerPlayerAI::ComputerPlayerAI ()
38 resetAlarm();
39 last_shatter_height = 0;
40 state = AI_WAITING;
41 queue = new GarbageQueue();
44 int ComputerPlayerAI::baseSteps()
46 return GC_STEPS_PER_SECOND;
49 int ComputerPlayerAI::stateSteps()
51 if (state == AI_WAITING) {
52 return (GC_CREEP_ADVANCE_VELOCITY * 5) // increase five lines
53 + (GC_DYING_DELAY * 5); // five combos
55 if (state == AI_SHATTERING) {
56 return garbageShatterDelay();
58 return GC_DYING_DELAY * 5;
61 int ComputerPlayerAI::lossHeight()
63 return 4;
66 void ComputerPlayerAI::resetAlarm()
68 last_time = Game::time_step;
71 int ComputerPlayerAI::alarm()
73 return last_time + baseSteps() + stateSteps();
76 int ComputerPlayerAI::garbageShatterDelay()
78 int delay = GC_INITIAL_POP_DELAY + (last_shatter_height * GC_PLAY_WIDTH * GC_INTERNAL_POP_DELAY) + GC_FINAL_POP_DELAY;
79 return delay;
82 GarbageQueue *ComputerPlayerAI::garbageQueue ()
84 return queue;
87 GarbageQueue *ComputerPlayerAI::garbageAmount( )
89 GarbageQueue *q = new GarbageQueue();
90 int working_height = GC_SAFE_HEIGHT - 1 - garbageQueue()->height();
91 int num_grays, num_normals;
93 MESSAGE("Hard garbageAmount");
95 if (working_height > 0) {
96 num_grays = working_height % 3;
97 } else {
98 num_grays = 0;
101 num_normals = garbageQueue()->height() + working_height;
103 LOG("garbageQueue height " << garbageQueue()->height());
104 LOG("grays: " << num_grays << " normals: " << num_normals);
105 loopi(num_grays)
106 q->add(1, 6, GF_GRAY);
108 int norm_div = num_normals / 3;
109 int norm_mod = num_normals % 3;
110 //int more_gray = norm_mod / 2;
111 LOG("div: " << norm_div << " mod: " << norm_mod << " gray: " << more_gray);
112 if (norm_div > 0) q->add(norm_div, 6, GF_NORMAL);
113 loopi(norm_mod) q->add(1, 6, GF_NORMAL);
114 //loopi(more_gray) q->add(1, 6, GF_GRAY);
116 shatter();
117 return q;
120 void ComputerPlayerAI::shatter()
122 MESSAGE("Resetting garbageQueue " << garbageQueue()->height());
123 if (garbageQueue()->height() > 0) {
124 state = AI_SHATTERING;
125 //int gray_height = garbageQueue()->specialHeight();
126 last_shatter_height = garbageQueue()->removeWithSpecials();
127 MESSAGE(last_shatter_height << " shattered and " << garbageQueue()->height() << " remaining grays:" << gray_height);
128 loopi(last_shatter_height) {
129 if (Random::chanceIn(GC_GARBAGE_TO_GARBAGE_SHATTER)) {
130 garbageQueue()->add(1, 6, GF_NORMAL);
131 MESSAGE("Adding garbage on AI shatter " << garbageQueue()->height());
134 } else {
135 state = AI_WAITING;
136 last_shatter_height = 0;
140 bool ComputerPlayerAI::determineLoss()
142 GarbageQueue *queue = garbageQueue();
143 static int height = queue->height();
144 int h = queue->height();
145 if (h != height) {
146 MESSAGE("Height change in determine loss old: " << height << " new: " << h);
147 height = h;
149 return queue->height() > lossHeight();
152 /* End ComputerPlayerAI */
154 int EasyAI::baseSteps()
156 //cout << "easy baseSteps" << endl;
157 int a = ComputerPlayerAI::baseSteps() * 15;
158 return a;
161 int EasyAI::lossHeight()
163 return 4;
166 int MediumAI::baseSteps()
168 return ComputerPlayerAI::baseSteps() * 10;
171 int MediumAI::lossHeight()
173 return 10;
176 /* Begin HardAI */
177 int HardAI::baseSteps()
179 return ComputerPlayerAI::baseSteps() * 5;
182 int HardAI::lossHeight()
184 return 20;
187 /* End HardAI */