Each module now includes its own header first. Removed sstream.h and the definition...
[crack-attack.git] / src / GarbageQueue.cxx
blobeaf4fc7cc8305a0738594070ecf9bf0d8da8c7f8
1 /*
2 * GarbageQueue.cxx
3 *
4 * Crack Attack! is the legal property of its developers, whose names
5 * are too numerous to list here. Please refer to the COPYRIGHT file
6 * distributed with this source distribution for a full listing.
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 "GarbageQueue.h"
25 #include "GarbageGenerator.h"
26 #include "GarbageManager.h"
28 #include <cassert>
30 GarbageQueue::GarbageQueue () {
31 //garbage_queue// = new vector<GarbageQueueElement();
32 cached_height = -1;
35 GarbageQueue::~GarbageQueue () {
36 reset();
39 void GarbageQueue::reset () {
40 garbage_queue.clear();
41 cached_height = -1;
44 // FIXME: This makes xtreme an error condition
45 int GarbageQueue::removeWithSpecials ()
47 if ((*(garbage_queue.begin())).flavor == GF_GRAY) {
48 return removeToFirst(GF_NORMAL);
49 } else {
50 return removeToFirst(GF_GRAY);
52 return 0;
55 int GarbageQueue::removeToFirst ( int flavor )
57 int num_removed = 0;
58 assert((*(garbage_queue.begin())).flavor != flavor);
59 if (garbage_queue.empty()) return 0;
60 std::vector<GarbageQueueElement>::iterator iter;
61 for (iter = garbage_queue.begin(); iter != garbage_queue.end(); ++iter) {
62 if ((*iter).flavor == flavor) break;
63 ++num_removed;
65 if (num_removed == 0) return 0;
66 #ifdef DEVELOPMENT
67 int prev_height = height();
68 cached_height = -1;
69 MESSAGE("Removing " << num_removed);
70 MESSAGE("Height before erase " << height());
71 #endif
72 garbage_queue.erase(garbage_queue.begin(), iter);
73 cached_height = -1;
74 #ifdef DEVELOPMENT
75 int current_height = height();
76 MESSAGE("Height after erase " << current_height);
77 //assert((prev_height - num_removed)==current_height);
78 if (prev_height - num_removed != current_height) {
79 MESSAGE("***********Assertion would've failed here in GarbageQueue.cxx:75***********");
80 MESSAGE("prev_height - num_removed != current_height (" <<
81 prev_height << " - " << num_removed << " != " <<
82 current_height << ")");
84 #endif
85 return num_removed;
88 void GarbageQueue::add ( int height, int width, int flavor)
90 GarbageQueueElement e;
91 e.active = true;
92 e.height = height;
93 e.width = width;
94 e.flavor = flavor;
95 add(e);
98 static void show_element (GarbageQueueElement &e) {
99 #ifndef NDEBUG
100 printf("Element: h %d w %d f %d\n",+
101 e.height,
102 e.width,
103 e.flavor);
104 #endif
107 void GarbageQueue::add ( GarbageQueueElement &element )
109 element.active = true;
110 MESSAGE("Adding garbage with height " << element.height);
111 show_element(element);
112 assert(element.height <= GC_PLAY_HEIGHT);
113 assert(element.width <= GC_PLAY_WIDTH);
114 garbage_queue.push_back(element);
115 //int old_height = cached_height;
116 cached_height = -1;
117 //assert(height()-element.height == old_height);
120 int GarbageQueue::height ( )
122 int garbage_height = 0;
123 std::vector<GarbageQueueElement>::iterator iter;
124 if (cached_height != -1) return cached_height;
125 for (iter = garbage_queue.begin(); iter != garbage_queue.end(); ++iter) {
126 garbage_height += (*iter).height;
128 cached_height = garbage_height;
129 return garbage_height;
132 int GarbageQueue::specialHeight ( )
134 int garbage_height = 0;
135 std::vector<GarbageQueueElement>::iterator iter;
136 if (cached_height != -1) return cached_height;
137 for (iter = garbage_queue.begin(); iter != garbage_queue.end(); ++iter) {
138 if (GarbageManager::isSpecialFlavor((*iter).flavor))
139 garbage_height += (*iter).height;
141 return garbage_height;
144 void GarbageQueue::sendToGenerator ( )
146 std::vector<GarbageQueueElement>::iterator iter;
147 for (iter = garbage_queue.begin(); iter != garbage_queue.end(); ++iter) {
148 GarbageGenerator::addToQueue(*iter);