Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Game.cc

Go to the documentation of this file.
00001 #include "objects.h"
00002 
00003 Horde bill;
00004 Network net;
00005 Library OS;
00006 Bucket bucket;
00007 Spark spark;
00008 //Scorelist scores;
00009 Game game;
00010 UI ui;
00011 
00012 int Game::RAND(int lb, int ub) {
00013         return (rand()%(ub-lb+1) + lb);
00014 }
00015 
00016 int Game::MAX(int x, int y) {
00017         return (x>y ? x : y);
00018 }
00019 
00020 int Game::MIN(int x, int y) {
00021         return (x<y ? x : y);
00022 }
00023 
00024 int Game::INTERSECT(int x1, int y1, int w1, int h1, int x2, int y2, int w2,
00025         int h2)
00026 {
00027         return (((x2-x1<=w1 && x2-x1>=0) || (x1-x2<=w2 && x1-x2>=0))
00028                 && ((y2-y1<=h1 && y2-y1>=0) || (y1-y2<=h2 && y1-y2>=0)));
00029 }
00030 
00031 void Game::setup_level (unsigned int lev) {
00032         level = lev;
00033         bill.setup();
00034         grabbed = EMPTY;
00035         ui.set_cursor(DEFAULTC);
00036         net.setup();
00037         iteration = efficiency = 0;
00038 }
00039 
00040 void Game::start(unsigned int lev) {
00041         state = PLAYING;
00042         score = 0;
00043         ui.restart_timer();
00044         ui.set_pausebutton(true);
00045         setup_level(lev);
00046 }
00047 
00048 void Game::quit() {
00049 
00050         exit(0);
00051 }
00052 
00053 void Game::update_info() {
00054         static char str[80];
00055         sprintf (str, "Bill:%d/%d  System:%d/%d/%d  Level:%d  Score:%d",
00056                 bill.on_screen, bill.off_screen, net.base, net.off,
00057                 net.win, level, score);
00058         ui.draw_str(str, 5, scrheight-5);
00059         efficiency += ((100*net.base-10*net.win)/net.units);
00060 }
00061 
00062 void Game::update_score (int action) {
00063         switch (action){
00064                 case ENDLEVEL: score+=(level*efficiency/iteration); break;
00065                 default: score+=(action*action*BILLPOINTS);
00066         }
00067 }
00068 
00069 void Game::warp_to_level (unsigned int lev) {
00070         if (state==PLAYING) {
00071                 if (lev <= level) return;
00072                 setup_level(lev);
00073         }
00074         else {
00075                 if (lev <=0) return;
00076                 start(lev);
00077         }
00078 }
00079 
00080 void Game::button_press(int x, int y) {
00081         int i, counter=0, flag=0;
00082         if (state != PLAYING) return;
00083         ui.set_cursor(DOWNC);
00084         if (bucket.clicked(x, y)) {
00085                 ui.set_cursor(BUCKETC);
00086                 grabbed = BUCKET;
00087         }
00088         for (i=0; i < bill.MAX_BILLS && !flag; i++) {
00089                 if (bill.list[i].state == bill.list[i].OFF
00090                         || bill.list[i].state == bill.list[i].DYING)
00091                                 continue;
00092                 if (bill.list[i].state == bill.list[i].STRAY &&
00093                         bill.list[i].clickedstray(x, y))
00094                 {
00095                         ui.set_cursor (bill.list[i].cargo);
00096                         grabbed = i;
00097                         flag = 1;
00098                 }
00099                 else if (bill.list[i].state != bill.list[i].STRAY &&
00100                         bill.list[i].clicked(x, y))
00101                 {
00102                         if (bill.list[i].state == bill.list[i].AT)
00103                                 net.computers[bill.list[i].target_c].busy=0;
00104                         bill.list[i].index = -1;
00105                         bill.list[i].cels = bill.dcels;
00106                         bill.list[i].x_offset = -2;
00107                         bill.list[i].y_offset = -15;
00108                         bill.list[i].state = bill.list[i].DYING;
00109                         counter++;
00110                 }
00111         }
00112         if (counter) update_score(counter);
00113 }
00114 
00115 void Game::button_release(int x, int y) {
00116         int i;
00117         ui.set_cursor (DEFAULTC);
00118         if (state != PLAYING || grabbed == EMPTY)
00119                 return;
00120         if (grabbed == BUCKET) {
00121                 grabbed = EMPTY;
00122                 for (i=0; i<net.ncables; i++)
00123                         if (net.cables[i].onspark(x, y)) {
00124                                 net.cables[i].active=0;
00125                                 net.cables[i].delay = spark.delay(level);
00126                         }
00127                 return;
00128         }
00129         for (i=0; i<net.units; i++)
00130                 if (net.computers[i].oncomputer(x, y)
00131                         &&
00132                         net.computers[i].compatible (bill.list[grabbed].cargo)
00133                         &&
00134                         (net.computers[i].os == OS.WINGDOWS ||
00135                          net.computers[i].os == OS.OFF))
00136                 {
00137                         net.base++;
00138                         if (net.computers[i].os == OS.WINGDOWS)
00139                                 net.win--;
00140                         else
00141                                 net.off--;
00142                         net.computers[i].os = bill.list[grabbed].cargo;
00143                         bill.list[grabbed].state = bill.list[grabbed].OFF;
00144                         grabbed = EMPTY;
00145                         return;
00146                 }
00147         grabbed = EMPTY;
00148 }
00149 
00150 void Game::update() {
00151         switch (state) {
00152         case PLAYING:
00153                 ui.clear();
00154                 bucket.draw();
00155                 net.update();
00156                 net.draw();
00157                 bill.update();
00158                 bill.draw();
00159                 update_info();
00160                 if (!(bill.on_screen+bill.off_screen)) {
00161                         update_score(ENDLEVEL);
00162                         state = BETWEEN;
00163                 }
00164                 if ((net.base+net.off)<=1) state = END;
00165                 break;
00166         case END:
00167                 ui.clear();
00168                 net.toasters();
00169                 net.draw();
00170                 ui.refresh();
00171                 ui.popup_dialog(ENDGAME);
00172 /*              if (score > scores.score[9]) ui.popup_dialog(ENTERNAME);
00173                 scores.update();*/
00174                 ui.popup_dialog(HIGHSCORE);
00175                 ui.clear();
00176                 ui.draw_centered(logo);
00177                 ui.kill_timer();
00178                 ui.set_pausebutton (false);
00179                 state = WAITING;
00180                 break;
00181         case BETWEEN:
00182                 ui.update_scorebox(level, score);
00183                 ui.popup_dialog (SCORE);
00184                 state = PLAYING;
00185                 setup_level(++level);
00186                 break;
00187         }
00188         ui.refresh();
00189         iteration++;
00190 }
00191 
00192 void Game::main(int argc, char **argv) {
00193         int c;
00194         extern char *optarg;
00195 
00196         level = 0;
00197         ui.initialize(&argc, argv);
00198         while (argv && argv[0] && (c = getopt(argc, argv, "l:L:")) != -1)
00199                 switch(c) {
00200                         case 'l':
00201                         case 'L': level = MAX (1, atoi(optarg)); break;
00202                 }
00203         srand(time(NULL));
00204         ui.make_mainwin();
00205         ui.graph_init();
00206         ui.clear();
00207         logo.load("logo");
00208         ui.draw_centered(logo);
00209         ui.refresh();
00210         ui.make_windows();
00211 //      scores.read();
00212 //      scores.update();
00213 
00214         bill.load_pix();
00215         OS.load_pix();
00216         net.load_pix();
00217         bucket.load_pix();
00218         spark.load_pix();
00219 
00220         ui.load_cursors();
00221 
00222         state = WAITING;
00223         if (level) start(level);
00224         else ui.set_pausebutton(false);
00225         ui.MainLoop();
00226         exit(0);
00227 }
00228 
00229 int main(int argc, char **argv) {
00230         if (argc>1 && !strcmp(argv[1], "-v")) {
00231                 printf ("XBill version 2.0\n\n");
00232                 exit(0);
00233         }
00234         if (argc>1 && !strcmp(argv[1], "-h")) {
00235                 printf ("XBill version 2.0\n");
00236                 printf ("Options:\n");
00237                 printf ("-l n, -L n\tStart at level n.\n");
00238                 printf ("-v\t\tPrint version number and exit.\n");
00239                 printf ("-h\t\tPrint help and exit.\n");
00240                 printf ("leaves the window.\n");
00241                 printf ("All standard X Intrinsics options are also ");
00242                 printf ("supported.\n\n");
00243                 exit(0);
00244         }
00245         game.main(argc, argv);
00246 }

Generated on Sat Nov 5 16:17:19 2005 for OPIE by  doxygen 1.4.2