~ubuntu-branches/ubuntu/intrepid/gnome-games/intrepid-updates

« back to all changes in this revision

Viewing changes to xbill/Game.cc

  • Committer: Package Import Robot
  • Author(s): Jose Carlos Garcia Sogo
  • Date: 2002-02-15 20:12:21 UTC
  • Revision ID: package-import@ubuntu.com-20020215201221-pntf81dxhsatz4qy
Tags: upstream-1.4.0.3
ImportĀ upstreamĀ versionĀ 1.4.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "objects.h"
 
2
#include <glib.h>
 
3
/* for argp */
 
4
#include <libgnome/libgnome.h>
 
5
 
 
6
Horde bill;
 
7
Network net;
 
8
Library OS;
 
9
Bucket bucket;
 
10
Spark spark;
 
11
Game game;
 
12
UI ui;
 
13
 
 
14
int Game::RAND(int lb, int ub) {
 
15
        return (rand()%(ub-lb+1) + lb);
 
16
}
 
17
 
 
18
int Game::MAX(int x, int y) {
 
19
        return (x>y ? x : y);
 
20
}
 
21
 
 
22
int Game::MIN(int x, int y) {
 
23
        return (x<y ? x : y);
 
24
}
 
25
 
 
26
int Game::INTERSECT(int x1, int y1, int w1, int h1, int x2, int y2, int w2,
 
27
        int h2)
 
28
{
 
29
        return (((x2-x1<=w1 && x2-x1>=0) || (x1-x2<=w2 && x1-x2>=0))
 
30
                && ((y2-y1<=h1 && y2-y1>=0) || (y1-y2<=h2 && y1-y2>=0)));
 
31
}
 
32
 
 
33
void Game::setup_level (int lev) {
 
34
        level = lev;
 
35
        bill.setup();
 
36
        grabbed = EMPTY;
 
37
        ui.set_cursor(DEFAULTC);
 
38
        net.setup();
 
39
        iteration = efficiency = 0;
 
40
}
 
41
 
 
42
void Game::start(int lev) {
 
43
        /* Looks like the high number we can get is 8+3*level, so ensure
 
44
           G_MAXINT is not going to be passed and crash */
 
45
 
 
46
        if (lev > G_MAXINT/8+3) return;
 
47
 
 
48
        state = PLAYING;
 
49
        score = 0;
 
50
        ui.restart_timer();
 
51
        ui.set_pausebutton(TRUE);
 
52
        setup_level(lev);
 
53
}
 
54
 
 
55
void Game::quit() {
 
56
        exit(0);
 
57
}
 
58
 
 
59
void Game::update_info() {
 
60
        static char str[80];
 
61
        sprintf (str, "Bill:%d/%d  System:%d/%d/%d  Level: %d Score:%d",
 
62
                bill.on_screen, bill.off_screen, net.base, net.off,
 
63
                net.win, level, score);
 
64
        ui.draw_str(str, 5, scrheight-5);
 
65
        efficiency += ((100*net.base-10*net.win)/net.units);
 
66
}
 
67
  
 
68
void Game::update_score (int action) {
 
69
        switch (action){
 
70
                case ENDLEVEL: score+=(level*efficiency/iteration); break;
 
71
                default: score+=(action*action*BILLPOINTS);
 
72
        }
 
73
}
 
74
 
 
75
void Game::warp_to_level (int lev) {
 
76
        if (state==PLAYING) {
 
77
                if (lev <= level || lev > G_MAXINT/8+3 ) return;
 
78
                setup_level(lev);
 
79
        }
 
80
        else {
 
81
                if (lev<=0) return;
 
82
                start(lev);
 
83
        }
 
84
}
 
85
 
 
86
void Game::button_press(int x, int y) {
 
87
        int i, counter=0, flag=0;
 
88
        if (state != PLAYING) return;
 
89
        ui.set_cursor(DOWNC);
 
90
        if (bucket.clicked(x, y)) {
 
91
                ui.set_cursor(BUCKETC);
 
92
                grabbed = BUCKET;
 
93
        }
 
94
        for (i=0; i < bill.MAX_BILLS && !flag; i++) {
 
95
                if (bill.list[i].state == bill.list[i].OFF
 
96
                        || bill.list[i].state == bill.list[i].DYING)
 
97
                                continue;
 
98
                if (bill.list[i].state == bill.list[i].STRAY &&
 
99
                        bill.list[i].clickedstray(x, y))
 
100
                {
 
101
                        ui.set_cursor (bill.list[i].cargo);
 
102
                        grabbed = i;
 
103
                        flag = 1;
 
104
                }
 
105
                else if (bill.list[i].state != bill.list[i].STRAY &&
 
106
                        bill.list[i].clicked(x, y))
 
107
                {
 
108
                        if (bill.list[i].state == bill.list[i].AT) 
 
109
                                net.computers[bill.list[i].target_c].busy=0;
 
110
                        bill.list[i].index = -1;
 
111
                        bill.list[i].cels = bill.dcels;
 
112
                        bill.list[i].x_offset = -2;
 
113
                        bill.list[i].y_offset = -15;
 
114
                        bill.list[i].state = bill.list[i].DYING;
 
115
                        counter++;
 
116
                }
 
117
        }
 
118
        if (counter) update_score(counter);
 
119
}
 
120
 
 
121
void Game::button_release(int x, int y) {
 
122
        int i;
 
123
        ui.set_cursor (DEFAULTC);
 
124
        if (state != PLAYING || grabbed == EMPTY)
 
125
                return;
 
126
        if (grabbed == BUCKET) {
 
127
                grabbed = EMPTY;
 
128
                for (i=0; i<net.ncables; i++)
 
129
                        if (net.cables[i].onspark(x, y)) {
 
130
                                net.cables[i].active=0;
 
131
                                net.cables[i].delay = spark.delay(level);
 
132
                        }
 
133
                return;
 
134
        }
 
135
        for (i=0; i<net.units; i++)
 
136
                if (net.computers[i].oncomputer(x, y)
 
137
                        &&
 
138
                        net.computers[i].compatible (bill.list[grabbed].cargo)
 
139
                        &&
 
140
                        (net.computers[i].os == OS.WINGDOWS || 
 
141
                         net.computers[i].os == OS.OFF))
 
142
                {
 
143
                        net.base++; 
 
144
                        if (net.computers[i].os == OS.WINGDOWS)
 
145
                                net.win--;
 
146
                        else
 
147
                                net.off--;
 
148
                        net.computers[i].os = bill.list[grabbed].cargo;
 
149
                        bill.list[grabbed].state = bill.list[grabbed].OFF;
 
150
                        grabbed = EMPTY;
 
151
                        return;
 
152
                }
 
153
        grabbed = EMPTY;
 
154
}       
 
155
 
 
156
void Game::update() {
 
157
        void show_scores(int);
 
158
        int pos;
 
159
        switch (state) {
 
160
        case PLAYING:
 
161
                ui.clear(); 
 
162
                bucket.draw();
 
163
                net.update();
 
164
                net.draw();
 
165
                bill.update();
 
166
                bill.draw();
 
167
                update_info();
 
168
                if (!(bill.on_screen+bill.off_screen)) {
 
169
                        update_score(ENDLEVEL);
 
170
                        state = BETWEEN;
 
171
                }
 
172
                if ((net.base+net.off)<=1) state = END;
 
173
                break;
 
174
        case END:
 
175
                ui.clear();
 
176
                net.toasters();
 
177
                net.draw();
 
178
                ui.refresh();
 
179
                ui.popup_dialog(ENDGAME);
 
180
                pos = gnome_score_log(score, NULL, TRUE);
 
181
                show_scores(pos);
 
182
                ui.clear();
 
183
                ui.draw_centered(logo);
 
184
                ui.kill_timer();
 
185
                ui.set_pausebutton (FALSE);
 
186
                state = WAITING;
 
187
                break;
 
188
        case BETWEEN:
 
189
                ui.update_scorebox(level, score);
 
190
                ui.popup_dialog (SCORE);
 
191
                state = PLAYING;
 
192
                /* This is a bad kludge, but i don't think anybody can pass
 
193
                   the 268435458 level (G_MAXINT/8+3 in a normal pcs). */
 
194
                if (++level < G_MAXINT/8+3)
 
195
                        setup_level(level);
 
196
                else
 
197
                        setup_level(1);
 
198
                break;
 
199
        }
 
200
        ui.refresh();
 
201
        iteration++;
 
202
}
 
203
 
 
204
static void parseAnArg(poptContext con, enum poptCallbackReason reason,
 
205
                       const struct poptOption *opt, const char *arg,
 
206
                       void *data) {
 
207
  if (reason != POPT_CALLBACK_REASON_OPTION) return;
 
208
  if (opt->val == -1)
 
209
    game.level = game.MAX(1, atoi(arg));
 
210
}
 
211
static const struct poptOption options[] = {
 
212
  { NULL, '\0', POPT_ARG_CALLBACK,
 
213
    (void *) &parseAnArg, 0, NULL },
 
214
  { "warp", 'l', POPT_ARG_STRING, NULL, -1,
 
215
    "Start at a different level", "LEVEL" },
 
216
  POPT_AUTOHELP
 
217
  { NULL, '\0', 0, NULL, 0 }
 
218
};
 
219
 
 
220
void Game::main(int argc, char **argv) {
 
221
        int c;
 
222
        extern char *optarg;
 
223
 
 
224
        level = 0;
 
225
        ui.initialize(argc, argv, options);
 
226
        srand(time(NULL));
 
227
        ui.make_mainwin();
 
228
        ui.graph_init();
 
229
        ui.clear();
 
230
        logo.load("logo");
 
231
        ui.draw_centered(logo);
 
232
        ui.refresh();
 
233
        ui.make_windows();
 
234
        //scores.read();
 
235
        //scores.update();
 
236
 
 
237
        bill.load_pix();
 
238
        OS.load_pix();
 
239
        net.load_pix();
 
240
        bucket.load_pix();
 
241
        spark.load_pix();
 
242
 
 
243
        ui.load_cursors();
 
244
 
 
245
        state = WAITING;
 
246
        if (level) start(level);
 
247
        else ui.set_pausebutton(FALSE);
 
248
        ui.MainLoop();
 
249
        exit(0);
 
250
}
 
251
 
 
252
int main(int argc, char **argv) {
 
253
        if (gnome_score_init("gnome-xbill")) {
 
254
                printf("Couldn't set up score server\n");
 
255
                exit(1);
 
256
        }
 
257
        game.main(argc, argv);
 
258
}