-rw-r--r-- | noncore/games/kbill/Game.cc | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/noncore/games/kbill/Game.cc b/noncore/games/kbill/Game.cc new file mode 100644 index 0000000..624b50b --- a/dev/null +++ b/noncore/games/kbill/Game.cc | |||
@@ -0,0 +1,246 @@ | |||
1 | #include "objects.h" | ||
2 | |||
3 | Horde bill; | ||
4 | Network net; | ||
5 | Library OS; | ||
6 | Bucket bucket; | ||
7 | Spark spark; | ||
8 | //Scorelist scores; | ||
9 | Game game; | ||
10 | UI ui; | ||
11 | |||
12 | int Game::RAND(int lb, int ub) { | ||
13 | return (rand()%(ub-lb+1) + lb); | ||
14 | } | ||
15 | |||
16 | int Game::MAX(int x, int y) { | ||
17 | return (x>y ? x : y); | ||
18 | } | ||
19 | |||
20 | int Game::MIN(int x, int y) { | ||
21 | return (x<y ? x : y); | ||
22 | } | ||
23 | |||
24 | int Game::INTERSECT(int x1, int y1, int w1, int h1, int x2, int y2, int w2, | ||
25 | int h2) | ||
26 | { | ||
27 | return (((x2-x1<=w1 && x2-x1>=0) || (x1-x2<=w2 && x1-x2>=0)) | ||
28 | && ((y2-y1<=h1 && y2-y1>=0) || (y1-y2<=h2 && y1-y2>=0))); | ||
29 | } | ||
30 | |||
31 | void Game::setup_level (unsigned int lev) { | ||
32 | level = lev; | ||
33 | bill.setup(); | ||
34 | grabbed = EMPTY; | ||
35 | ui.set_cursor(DEFAULTC); | ||
36 | net.setup(); | ||
37 | iteration = efficiency = 0; | ||
38 | } | ||
39 | |||
40 | void Game::start(unsigned int lev) { | ||
41 | state = PLAYING; | ||
42 | score = 0; | ||
43 | ui.restart_timer(); | ||
44 | ui.set_pausebutton(true); | ||
45 | setup_level(lev); | ||
46 | } | ||
47 | |||
48 | void Game::quit() { | ||
49 | |||
50 | exit(0); | ||
51 | } | ||
52 | |||
53 | void Game::update_info() { | ||
54 | static char str[80]; | ||
55 | sprintf (str, "Bill:%d/%d System:%d/%d/%d Level:%d Score:%d", | ||
56 | bill.on_screen, bill.off_screen, net.base, net.off, | ||
57 | net.win, level, score); | ||
58 | ui.draw_str(str, 5, scrheight-5); | ||
59 | efficiency += ((100*net.base-10*net.win)/net.units); | ||
60 | } | ||
61 | |||
62 | void Game::update_score (int action) { | ||
63 | switch (action){ | ||
64 | case ENDLEVEL: score+=(level*efficiency/iteration); break; | ||
65 | default: score+=(action*action*BILLPOINTS); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | void Game::warp_to_level (unsigned int lev) { | ||
70 | if (state==PLAYING) { | ||
71 | if (lev <= level) return; | ||
72 | setup_level(lev); | ||
73 | } | ||
74 | else { | ||
75 | if (lev<=0) return; | ||
76 | start(lev); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | void Game::button_press(int x, int y) { | ||
81 | int i, counter=0, flag=0; | ||
82 | if (state != PLAYING) return; | ||
83 | ui.set_cursor(DOWNC); | ||
84 | if (bucket.clicked(x, y)) { | ||
85 | ui.set_cursor(BUCKETC); | ||
86 | grabbed = BUCKET; | ||
87 | } | ||
88 | for (i=0; i < bill.MAX_BILLS && !flag; i++) { | ||
89 | if (bill.list[i].state == bill.list[i].OFF | ||
90 | || bill.list[i].state == bill.list[i].DYING) | ||
91 | continue; | ||
92 | if (bill.list[i].state == bill.list[i].STRAY && | ||
93 | bill.list[i].clickedstray(x, y)) | ||
94 | { | ||
95 | ui.set_cursor (bill.list[i].cargo); | ||
96 | grabbed = i; | ||
97 | flag = 1; | ||
98 | } | ||
99 | else if (bill.list[i].state != bill.list[i].STRAY && | ||
100 | bill.list[i].clicked(x, y)) | ||
101 | { | ||
102 | if (bill.list[i].state == bill.list[i].AT) | ||
103 | net.computers[bill.list[i].target_c].busy=0; | ||
104 | bill.list[i].index = -1; | ||
105 | bill.list[i].cels = bill.dcels; | ||
106 | bill.list[i].x_offset = -2; | ||
107 | bill.list[i].y_offset = -15; | ||
108 | bill.list[i].state = bill.list[i].DYING; | ||
109 | counter++; | ||
110 | } | ||
111 | } | ||
112 | if (counter) update_score(counter); | ||
113 | } | ||
114 | |||
115 | void Game::button_release(int x, int y) { | ||
116 | int i; | ||
117 | ui.set_cursor (DEFAULTC); | ||
118 | if (state != PLAYING || grabbed == EMPTY) | ||
119 | return; | ||
120 | if (grabbed == BUCKET) { | ||
121 | grabbed = EMPTY; | ||
122 | for (i=0; i<net.ncables; i++) | ||
123 | if (net.cables[i].onspark(x, y)) { | ||
124 | net.cables[i].active=0; | ||
125 | net.cables[i].delay = spark.delay(level); | ||
126 | } | ||
127 | return; | ||
128 | } | ||
129 | for (i=0; i<net.units; i++) | ||
130 | if (net.computers[i].oncomputer(x, y) | ||
131 | && | ||
132 | net.computers[i].compatible (bill.list[grabbed].cargo) | ||
133 | && | ||
134 | (net.computers[i].os == OS.WINGDOWS || | ||
135 | net.computers[i].os == OS.OFF)) | ||
136 | { | ||
137 | net.base++; | ||
138 | if (net.computers[i].os == OS.WINGDOWS) | ||
139 | net.win--; | ||
140 | else | ||
141 | net.off--; | ||
142 | net.computers[i].os = bill.list[grabbed].cargo; | ||
143 | bill.list[grabbed].state = bill.list[grabbed].OFF; | ||
144 | grabbed = EMPTY; | ||
145 | return; | ||
146 | } | ||
147 | grabbed = EMPTY; | ||
148 | } | ||
149 | |||
150 | void Game::update() { | ||
151 | switch (state) { | ||
152 | case PLAYING: | ||
153 | ui.clear(); | ||
154 | bucket.draw(); | ||
155 | net.update(); | ||
156 | net.draw(); | ||
157 | bill.update(); | ||
158 | bill.draw(); | ||
159 | update_info(); | ||
160 | if (!(bill.on_screen+bill.off_screen)) { | ||
161 | update_score(ENDLEVEL); | ||
162 | state = BETWEEN; | ||
163 | } | ||
164 | if ((net.base+net.off)<=1) state = END; | ||
165 | break; | ||
166 | case END: | ||
167 | ui.clear(); | ||
168 | net.toasters(); | ||
169 | net.draw(); | ||
170 | ui.refresh(); | ||
171 | ui.popup_dialog(ENDGAME); | ||
172 | /* if (score > scores.score[9]) ui.popup_dialog(ENTERNAME); | ||
173 | scores.update();*/ | ||
174 | ui.popup_dialog(HIGHSCORE); | ||
175 | ui.clear(); | ||
176 | ui.draw_centered(logo); | ||
177 | ui.kill_timer(); | ||
178 | ui.set_pausebutton (false); | ||
179 | state = WAITING; | ||
180 | break; | ||
181 | case BETWEEN: | ||
182 | ui.update_scorebox(level, score); | ||
183 | ui.popup_dialog (SCORE); | ||
184 | state = PLAYING; | ||
185 | setup_level(++level); | ||
186 | break; | ||
187 | } | ||
188 | ui.refresh(); | ||
189 | iteration++; | ||
190 | } | ||
191 | |||
192 | void Game::main(int argc, char **argv) { | ||
193 | int c; | ||
194 | extern char *optarg; | ||
195 | |||
196 | level = 0; | ||
197 | ui.initialize(&argc, argv); | ||
198 | while (argv && argv[0] && (c = getopt(argc, argv, "l:L:")) != -1) | ||
199 | switch(c) { | ||
200 | case 'l': | ||
201 | case 'L': level = MAX (1, atoi(optarg)); break; | ||
202 | } | ||
203 | srand(time(NULL)); | ||
204 | ui.make_mainwin(); | ||
205 | ui.graph_init(); | ||
206 | ui.clear(); | ||
207 | logo.load("logo"); | ||
208 | ui.draw_centered(logo); | ||
209 | ui.refresh(); | ||
210 | ui.make_windows(); | ||
211 | // scores.read(); | ||
212 | // scores.update(); | ||
213 | |||
214 | bill.load_pix(); | ||
215 | OS.load_pix(); | ||
216 | net.load_pix(); | ||
217 | bucket.load_pix(); | ||
218 | spark.load_pix(); | ||
219 | |||
220 | ui.load_cursors(); | ||
221 | |||
222 | state = WAITING; | ||
223 | if (level) start(level); | ||
224 | else ui.set_pausebutton(false); | ||
225 | ui.MainLoop(); | ||
226 | exit(0); | ||
227 | } | ||
228 | |||
229 | int main(int argc, char **argv) { | ||
230 | if (argc>1 && !strcmp(argv[1], "-v")) { | ||
231 | printf ("XBill version 2.0\n\n"); | ||
232 | exit(0); | ||
233 | } | ||
234 | if (argc>1 && !strcmp(argv[1], "-h")) { | ||
235 | printf ("XBill version 2.0\n"); | ||
236 | printf ("Options:\n"); | ||
237 | printf ("-l n, -L n\tStart at level n.\n"); | ||
238 | printf ("-v\t\tPrint version number and exit.\n"); | ||
239 | printf ("-h\t\tPrint help and exit.\n"); | ||
240 | printf ("leaves the window.\n"); | ||
241 | printf ("All standard X Intrinsics options are also "); | ||
242 | printf ("supported.\n\n"); | ||
243 | exit(0); | ||
244 | } | ||
245 | game.main(argc, argv); | ||
246 | } | ||