3 * Andrew Sayman - 3/27/05
5 * Copyright (C) 2005 Andrew Sayman
6 * Copyright (C) 2005 Kevin Webb
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.
22 #include "ComputerPlayerAI.h"
25 #include "GarbageGenerator.h"
30 #define loop(v,m) for(int v = 0; v<(m); v++)
31 #define loopi(m) loop(i,m)
33 /* Begin ComputerPlayerAI */
34 ComputerPlayerAI::ComputerPlayerAI ()
37 last_shatter_height
= 0;
39 queue
= new GarbageQueue();
42 int ComputerPlayerAI::baseSteps()
44 return GC_STEPS_PER_SECOND
;
47 int ComputerPlayerAI::stateSteps()
49 if (state
== AI_WAITING
) {
50 return (GC_CREEP_ADVANCE_VELOCITY
* 5) // increase five lines
51 + (GC_DYING_DELAY
* 5); // five combos
53 if (state
== AI_SHATTERING
) {
54 return garbageShatterDelay();
58 int ComputerPlayerAI::lossHeight()
63 void ComputerPlayerAI::resetAlarm()
65 last_time
= Game::time_step
;
68 int ComputerPlayerAI::alarm()
70 return last_time
+ baseSteps() + stateSteps();
73 int ComputerPlayerAI::garbageShatterDelay()
75 int delay
= GC_INITIAL_POP_DELAY
+ (last_shatter_height
* GC_PLAY_WIDTH
* GC_INTERNAL_POP_DELAY
) + GC_FINAL_POP_DELAY
;
79 GarbageQueue
*ComputerPlayerAI::garbageQueue ()
84 GarbageQueue
*ComputerPlayerAI::garbageAmount( )
86 GarbageQueue
*q
= new GarbageQueue();
87 size_t const size
= 20;
89 BufferElement garbage
;
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;
101 num_normals
= garbageQueue()->height() + working_height
;
103 LOG("garbageQueue height " << garbageQueue()->height());
104 LOG("grays: " << num_grays
<< " normals: " << num_normals
);
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);
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.");
128 if (gray_height
!= 0) assert(garbageQueue()->height() != 0);
129 loopi(last_shatter_height
) {
130 if (Random::chanceIn(GC_GARBAGE_TO_GARBAGE_SHATTER
)) {
131 garbageQueue()->add(1, 6, GF_NORMAL
);
132 MESSAGE("Adding garbage on AI shatter " << garbageQueue()->height());
137 last_shatter_height
= 0;
141 bool ComputerPlayerAI::determineLoss()
143 GarbageQueue
*queue
= garbageQueue();
144 static int height
= queue
->height();
145 int h
= queue
->height();
147 MESSAGE("Height change in determine loss old: " << height
<< " new: " << h
);
150 return queue
->height() > lossHeight();
153 /* End ComputerPlayerAI */
155 int EasyAI::baseSteps()
157 //cout << "easy baseSteps" << endl;
158 int a
= ComputerPlayerAI::baseSteps() * 15;
162 int EasyAI::lossHeight()
167 int MediumAI::baseSteps()
169 return ComputerPlayerAI::baseSteps() * 10;
172 int MediumAI::lossHeight()
178 int HardAI::baseSteps()
180 return ComputerPlayerAI::baseSteps() * 5;
183 int HardAI::lossHeight()