1 /*
2  * GarbageManager.h
3  * Daniel Nelson - 8/24/0
4  *
5  * Copyright (C) 2000  Daniel Nelson
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.
11  *
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.
16  *
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.
20  *
21  * Daniel Nelson - aluminumangel.org
22  * 174 W. 18th Ave.
23  * Columbus, OH  43210
24  */
25 
26 #ifndef GARBAGEMANAGER_H
27 #define GARBAGEMANAGER_H
28 
29 #include <cassert>
30 
31 using namespace std;
32 
33 #include "Garbage.h"
34 #include "GarbageFlavorImage.h"
35 
36 class ComboTabulator;
37 
38 /* static */ class GarbageManager {
39 public:
40   static void gameStart (   );
41   static bool newFallingGarbage ( int height, int width, int flavor );
42 
newAwakingGarbage(int x,int y,int height,int pop_delay,int awake_delay,ComboTabulator * combo,int pop_color)43   static inline void newAwakingGarbage ( int x, int y, int height,
44    int pop_delay, int awake_delay, ComboTabulator *combo, int pop_color )
45   {
46     if (garbage_count == GC_GARBAGE_STORE_SIZE) return;
47 
48     int id = findFreeId();
49     allocateId(id);
50     Garbage *garbage = garbageStore + id;
51 
52     garbage->initializeAwaking(x, y, height, pop_delay, awake_delay, combo,
53      pop_color);
54   }
55 
newFallingGarbage(int x,int y,int height,int width,int flavor)56   static inline void newFallingGarbage ( int x, int y, int height, int width,
57    int flavor )
58   {
59     if (garbage_count == GC_GARBAGE_STORE_SIZE) return;
60 
61     int id = findFreeId();
62     allocateId(id);
63     Garbage *garbage = garbageStore + id;
64 
65     garbage->initializeFalling(x, y, height, width, flavor);
66 
67     GarbageFlavorImage::requestGarbageFlavorImage(*garbage);
68   }
69 
deleteGarbage(Garbage * garbage)70   static inline void deleteGarbage ( Garbage *garbage )
71   {
72     GarbageFlavorImage::notifyGarbageDestruction(*garbage);
73     freeId(garbage->id);
74   }
75 
shiftUp()76   static inline void shiftUp (   )
77   {
78     int c = garbage_count;
79     for (int n = 0; c; n++)
80       if (storeMap[n]) {
81         c--;
82         garbageStore[n].y++;
83       }
84   }
85 
isSpecialFlavor(int flavor)86   static inline bool isSpecialFlavor ( int flavor )
87   {
88     return flavor;
89   }
90 
mapBlockCodeToGarbageFlavor(int code)91   static inline int mapBlockCodeToGarbageFlavor ( int code )
92   {
93     return code + (GF_GRAY + 1);
94   }
95 
96   static int garbage_count;
97   static Garbage garbageStore[GC_GARBAGE_STORE_SIZE];
98   static bool storeMap[GC_GARBAGE_STORE_SIZE];
99 
100 private:
findFreeId()101   static inline int findFreeId (   )
102   {
103     int n;
104     for (n = 0; storeMap[n]; n++);
105     return n;
106   }
107 
allocateId(int id)108   static inline void allocateId ( int id )
109   {
110     assert(!storeMap[id]);
111     storeMap[id] = true;
112     garbage_count++;
113   }
114 
freeId(int id)115   static inline void freeId ( int id )
116   {
117     assert(storeMap[id]);
118     storeMap[id] = false;
119     garbage_count--;
120   }
121 };
122 
123 #endif
124