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();
56 return GC_DYING_DELAY
* 5;
59 int ComputerPlayerAI::lossHeight()
64 void ComputerPlayerAI::resetAlarm()
66 last_time
= Game::time_step
;
69 int ComputerPlayerAI::alarm()
71 return last_time
+ baseSteps() + stateSteps();
74 int ComputerPlayerAI::garbageShatterDelay()
76 int delay
= GC_INITIAL_POP_DELAY
+ (last_shatter_height
* GC_PLAY_WIDTH
* GC_INTERNAL_POP_DELAY
) + GC_FINAL_POP_DELAY
;
80 GarbageQueue
*ComputerPlayerAI::garbageQueue ()
85 GarbageQueue
*ComputerPlayerAI::garbageAmount( )
87 GarbageQueue
*q
= new GarbageQueue();
88 int working_height
= GC_SAFE_HEIGHT
- 1 - garbageQueue()->height();
89 int num_grays
, num_normals
;
91 MESSAGE("Hard garbageAmount");
93 if (working_height
> 0) {
94 num_grays
= working_height
% 3;
99 num_normals
= garbageQueue()->height() + working_height
;
101 LOG("garbageQueue height " << garbageQueue()->height());
102 LOG("grays: " << num_grays
<< " normals: " << num_normals
);
104 q
->add(1, 6, GF_GRAY
);
106 int norm_div
= num_normals
/ 3;
107 int norm_mod
= num_normals
% 3;
108 int more_gray
= norm_mod
/ 2;
109 LOG("div: " << norm_div
<< " mod: " << norm_mod
<< " gray: " << more_gray
);
110 if (norm_div
> 0) q
->add(norm_div
, 6, GF_NORMAL
);
111 loopi(norm_mod
) q
->add(1, 6, GF_NORMAL
);
112 //loopi(more_gray) q->add(1, 6, GF_GRAY);
118 void ComputerPlayerAI::shatter()
120 MESSAGE("Resetting garbageQueue " << garbageQueue()->height());
121 if (garbageQueue()->height() > 0) {
122 state
= AI_SHATTERING
;
123 int gray_height
= garbageQueue()->specialHeight();
124 last_shatter_height
= garbageQueue()->removeWithSpecials();
125 MESSAGE(last_shatter_height
<< " shattered and " << garbageQueue()->height() << " remaining grays:" << gray_height
);
126 loopi(last_shatter_height
) {
127 if (Random::chanceIn(GC_GARBAGE_TO_GARBAGE_SHATTER
)) {
128 garbageQueue()->add(1, 6, GF_NORMAL
);
129 MESSAGE("Adding garbage on AI shatter " << garbageQueue()->height());
134 last_shatter_height
= 0;
138 bool ComputerPlayerAI::determineLoss()
140 GarbageQueue
*queue
= garbageQueue();
141 static int height
= queue
->height();
142 int h
= queue
->height();
144 MESSAGE("Height change in determine loss old: " << height
<< " new: " << h
);
147 return queue
->height() > lossHeight();
150 /* End ComputerPlayerAI */
152 int EasyAI::baseSteps()
154 //cout << "easy baseSteps" << endl;
155 int a
= ComputerPlayerAI::baseSteps() * 15;
159 int EasyAI::lossHeight()
164 int MediumAI::baseSteps()
166 return ComputerPlayerAI::baseSteps() * 10;
169 int MediumAI::lossHeight()
175 int HardAI::baseSteps()
177 return ComputerPlayerAI::baseSteps() * 5;
180 int HardAI::lossHeight()