Main Page | Data Structures | Directories | File List | Data Fields | Globals

module_buttons.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "lcconfig.h"
00003 #include "lcstring.h"
00004 #include "geometry.h"
00005 #include "module_buttons.h"
00006 #include "cliglobs.h"
00007 #include "mouse.h"
00008 #include "lcintl.h"
00009 #include "lctypes.h"
00010 #include "lclib.h"
00011 #include "lchelp.h"
00012 
00013 #define DEBUG_MODULES 0
00014 
00015 static int module_rows;
00016 static int module_cols;
00017 
00018 static Mouse_Handle * mhandle;
00019 
00020 static Rect * mbw = &scr.module_buttons;
00021 
00022 int module_type[NUMOF_MODULES];
00023 char *module_graphic[NUMOF_MODULES];
00024 int module_tflag[NUMOF_MODULES];
00025 char module_help[NUMOF_MODULES][20];
00026 
00027 int module_help_flag[NUMOF_MODULES];
00028 
00029 int sbut[NUMOF_MODULES];
00030 
00031 int selected_module = 0;
00032 int selected_module_type;
00033 int selected_module_group;
00034 int selected_module_cost;
00035 
00036 
00037 void
00038 init_modules (void)
00039 {
00040     int i;
00041 
00042     /* Get max number of rows for space allotted */
00043     module_rows = (mbw->h / (SELECT_BUTTON_INTERVAL));
00044     module_cols = (mbw->w / (SELECT_BUTTON_INTERVAL));
00045 
00046     /* Tell the mouse where we are */
00047     mhandle = mouse_register(&scr.module_buttons,&module_buttons_handler);
00048 
00049 #if (DEBUG_MODULES)
00050     printf("debug_modules:\t\tr=%d\tc=%d\n",module_rows, module_cols);
00051 #endif
00052 
00053     /* sbut converts a group into a column major index of the button array. */
00054     sbut[0] = 16;               /* buldoze */
00055     sbut[1] = 13;               /* powerline */
00056     sbut[2] = 15;               /* solar power */
00057     sbut[3] = 14;               /* substation */
00058     sbut[4] = 0;                /* residence */
00059     sbut[5] = 1;                /* farm */
00060     sbut[6] = 2;                /* market */
00061     sbut[7] = 19;               /* track */
00062     sbut[8] = 10;               /* coalmine */
00063     sbut[9] = 28;               /* rail */
00064     sbut[10] = 29;              /* coal power */
00065     sbut[11] = 25;              /* road */
00066     sbut[12] = 27;              /* light industry */
00067     sbut[13] = 11;              /* university */
00068     sbut[14] = 3;               /* commune */
00069     sbut[15] = 4;               /* oremine */
00070     sbut[16] = 5;               /* tip */
00071     sbut[17] = 9;               /* export */
00072     sbut[18] = 12;              /* heavy industry */
00073     sbut[19] = 6;               /* parkland */
00074     sbut[20] = 30;              /* recycle */
00075     sbut[21] = 20;              /* water */
00076     sbut[22] = 26;              /* health */
00077     sbut[23] = 31;              /* rocket */
00078     sbut[24] = 24;              /* windmill */
00079     sbut[25] = 17;              /* monument */
00080     sbut[26] = 21;              /* school */
00081     sbut[27] = 22;              /* blacksmith */
00082     sbut[28] = 8;               /* mill */
00083     sbut[29] = 18;              /* pottery */
00084     sbut[30] = 23;              /* fire station */
00085     sbut[31] = 7;               /* cricket  */
00086 
00087 
00088     /* load graphics and initialize help */
00089 
00090     module_graphic[sbut[0]] = load_graphic ("buldoze-button.csi");
00091     module_type[sbut[0]] = CST_GREEN;
00092     strcpy (module_help[sbut[0]], "bulldoze.hlp");
00093 
00094     module_graphic[sbut[1]] = load_graphic ("powerline-button.csi");
00095     module_type[sbut[1]] = CST_POWERL_H_L;
00096     strcpy (module_help[sbut[1]], "powerline.hlp");
00097 
00098     module_graphic[sbut[2]] = load_graphic ("powerssolar-button.csi");
00099     module_type[sbut[2]] = CST_POWERS_SOLAR;
00100     strcpy (module_help[sbut[2]], "powerssolar.hlp");
00101 
00102     module_graphic[sbut[3]] = load_graphic ("substation-button.csi");
00103     module_type[sbut[3]] = CST_SUBSTATION_R;
00104     strcpy (module_help[sbut[3]], "substation.hlp");
00105 
00106     module_graphic[sbut[4]] = load_graphic ("residence-button.csi");
00107     module_type[sbut[4]] = CST_RESIDENCE_LL;
00108     strcpy (module_help[sbut[4]], "residential.hlp");
00109 
00110     module_graphic[sbut[5]] = load_graphic ("organic-farm-button.csi");
00111     module_type[sbut[5]] = CST_FARM_O0;
00112     strcpy (module_help[sbut[5]], "farm.hlp");
00113 
00114     module_graphic[sbut[6]] = load_graphic ("market-button.csi");
00115     module_type[sbut[6]] = CST_MARKET_EMPTY;
00116     strcpy (module_help[sbut[6]], "market.hlp");
00117 
00118     module_help_flag[sbut[7]] = 1;        /* No help for track */
00119     module_graphic[sbut[7]] = load_graphic ("track-button.csi");
00120     module_type[sbut[7]] = CST_TRACK_LR;
00121     strcpy (module_help[sbut[7]], "track.hlp");
00122 
00123     module_graphic[sbut[8]] = load_graphic ("coalmine-button.csi");
00124     module_type[sbut[8]] = CST_COALMINE_EMPTY;
00125     strcpy (module_help[sbut[8]], "coalmine.hlp");
00126 
00127     module_graphic[sbut[9]] = load_graphic ("rail-button.csi");
00128     module_type[sbut[9]] = CST_RAIL_LR;
00129     strcpy (module_help[sbut[9]], "rail.hlp");
00130 
00131     module_graphic[sbut[10]] = load_graphic ("powerscoal-button.csi");
00132     module_type[sbut[10]] = CST_POWERS_COAL_EMPTY;
00133     strcpy (module_help[sbut[10]], "powerscoal.hlp");
00134 
00135     module_graphic[sbut[11]] = load_graphic ("road-button.csi");
00136     module_type[sbut[11]] = CST_ROAD_LR;
00137     strcpy (module_help[sbut[11]], "road.hlp");
00138 
00139     module_graphic[sbut[12]] = load_graphic ("industryl-button.csi");
00140     module_type[sbut[12]] = CST_INDUSTRY_L_C;
00141     strcpy (module_help[sbut[12]], "industryl.hlp");
00142 
00143     module_graphic[sbut[13]] = load_graphic ("university-button.csi");
00144     module_type[sbut[13]] = CST_UNIVERSITY;
00145     strcpy (module_help[sbut[13]], "university.hlp");
00146 
00147     module_graphic[sbut[14]] = load_graphic ("commune-button.csi");
00148     module_type[sbut[14]] = CST_COMMUNE_1;
00149     strcpy (module_help[sbut[14]], "commune.hlp");
00150 
00151     module_graphic[sbut[15]] = load_graphic ("oremine-button.csi");
00152     module_type[sbut[15]] = CST_OREMINE_1;
00153     strcpy (module_help[sbut[15]], "oremine.hlp");
00154 
00155     module_graphic[sbut[16]] = load_graphic ("tip-button.csi");
00156     module_type[sbut[16]] = CST_TIP_0;
00157     strcpy (module_help[sbut[16]], "tip.hlp");
00158 
00159     module_graphic[sbut[17]] = load_graphic ("port-button.csi");
00160     module_type[sbut[17]] = CST_EX_PORT;
00161     strcpy (module_help[sbut[17]], "port.hlp");
00162 
00163     module_graphic[sbut[18]] = load_graphic ("industryh-button.csi");
00164     module_type[sbut[18]] = CST_INDUSTRY_H_C;
00165     strcpy (module_help[sbut[18]], "industryh.hlp");
00166 
00167     module_graphic[sbut[19]] = load_graphic ("parkland-button.csi");
00168     module_type[sbut[19]] = CST_PARKLAND_PLANE;
00169     strcpy (module_help[sbut[19]], "park.hlp");
00170 
00171     module_graphic[sbut[20]] = load_graphic ("recycle-button.csi");
00172     module_type[sbut[20]] = CST_RECYCLE;
00173     strcpy (module_help[sbut[20]], "recycle.hlp");
00174 
00175     module_graphic[sbut[21]] = load_graphic ("water-button.csi");
00176     module_type[sbut[21]] = CST_WATER;
00177     strcpy (module_help[sbut[21]], "river.hlp");
00178 
00179     module_graphic[sbut[22]] = load_graphic ("health-button.csi");
00180     module_type[sbut[22]] = CST_HEALTH;
00181     strcpy (module_help[sbut[22]], "health.hlp");
00182 
00183     module_graphic[sbut[23]] = load_graphic ("rocket-button.csi");
00184     module_type[sbut[23]] = CST_ROCKET_1;
00185     strcpy (module_help[sbut[23]], "rocket.hlp");
00186 
00187     module_graphic[sbut[24]] = load_graphic ("windmill-button.csi");
00188     module_type[sbut[24]] = CST_WINDMILL_1_R;
00189     strcpy (module_help[sbut[24]], "windmill.hlp");
00190 
00191     module_graphic[sbut[25]] = load_graphic ("monument-button.csi");
00192     module_type[sbut[25]] = CST_MONUMENT_0;
00193     strcpy (module_help[sbut[25]], "monument.hlp");
00194 
00195     module_graphic[sbut[26]] = load_graphic ("school-button.csi");
00196     module_type[sbut[26]] = CST_SCHOOL;
00197     strcpy (module_help[sbut[26]], "school.hlp");
00198 
00199     module_graphic[sbut[27]] = load_graphic ("blacksmith-button.csi");
00200     module_type[sbut[27]] = CST_BLACKSMITH_0;
00201     strcpy (module_help[sbut[27]], "blacksmith.hlp");
00202 
00203     module_graphic[sbut[28]] = load_graphic ("mill-button.csi");
00204     module_type[sbut[28]] = CST_MILL_0;
00205     strcpy (module_help[sbut[28]], "mill.hlp");
00206 
00207     module_graphic[sbut[29]] = load_graphic ("pottery-button.csi");
00208     module_type[sbut[29]] = CST_POTTERY_0;
00209     strcpy (module_help[sbut[29]], "pottery.hlp");
00210 
00211     module_graphic[sbut[30]] = load_graphic ("firestation-button.csi");
00212     module_type[sbut[30]] = CST_FIRESTATION_1;
00213     strcpy (module_help[sbut[30]], "firestation.hlp");
00214 
00215     module_graphic[sbut[31]] = load_graphic ("cricket-button.csi");
00216     module_type[sbut[31]] = CST_CRICKET_1;
00217     strcpy (module_help[sbut[31]], "cricket.hlp");
00218 
00219 
00220 
00221     /* disable all the buttons */
00222     for (i = 0; i < NUMOF_MODULES; i++) {
00223         module_tflag[i] = 0;
00224     }
00225 
00226     /* select track (which will be enabled later) */
00227 #if defined (commentout)
00228     selected_module_type = CST_TRACK_LR;
00229     selected_module_cost = GROUP_TRACK_COST;
00230     selected_module_group = get_group_of_type(selected_module_type);
00231 #endif
00232     selected_module = sbut[7];
00233     /* GCS: Move to later in initialization because I don't want to draw_main_window_box() yet */
00234 #if defined (commentout)
00235     set_selected_module (CST_TRACK_LR);
00236 #endif
00237 }
00238 
00239 
00240 /* update_avail_modules: Determine which modules are available, based on
00241    tech level */
00242 
00243 void
00244 update_avail_modules (int popup)
00245 {
00246     int i;
00247     int f; /* remembers if a change is made to what is available, so we can
00248               draw if necessary */
00249 
00250     for (i = 0; i < NUMOF_MODULES; i++)
00251     {
00252         int g = inv_sbut(i);
00253         f = module_tflag[i];
00254         if (tech_level >= main_groups[g].tech * MAX_TECH_LEVEL/1000)
00255         {
00256             if (module_tflag[i] == 0)
00257                 if (popup)
00258                     activate_module (i);
00259             f = 1;
00260         }
00261         else if (module_tflag[i] != 0 &&
00262                  tech_level
00263                  < ((main_groups[g].tech - (main_groups[g].tech/10)) * MAX_TECH_LEVEL/1000) )
00264             f = 0;
00265         if (module_tflag[i] != f)
00266         {
00267             module_tflag[i] = f;
00268             draw_module (i, module_graphic[i]);
00269         }
00270     }
00271     /* XXX: Why is this here? Should be with rest of tech gained messages! */
00272     /* GCS: Because the modern windmill tech reuses the same module number
00273        as the primative windmill? */
00274     if (tech_level > MODERN_WINDMILL_TECH && modern_windmill_flag == 0)
00275     {
00276         ok_dial_box ("mod_wind_up.mes", GOOD, 0L);
00277         modern_windmill_flag = 1;
00278     }
00279 }
00280 
00281 /* Display message for module when it is activated (see above) */
00282 
00283 void
00284 activate_module (int module)
00285 {
00286     module = inv_sbut (module);
00287 
00288     if (module == GROUP_WINDMILL)
00289         ok_dial_box ("windmillup.mes", GOOD, 0L);
00290     else if (module == GROUP_COAL_POWER)
00291         ok_dial_box ("coalpowerup.mes", GOOD, 0L);
00292     /*    else if (module == (GROUP_SOLAR_POWER - 1)) */
00293     else if (module == GROUP_SOLAR_POWER)
00294         ok_dial_box ("solarpowerup.mes", GOOD, 0L);
00295     else if (module == GROUP_COALMINE)
00296         ok_dial_box ("coalmineup.mes", GOOD, 0L);
00297     else if (module == GROUP_RAIL)
00298         ok_dial_box ("railwayup.mes", GOOD, 0L);
00299     else if (module == GROUP_ROAD)
00300         ok_dial_box ("roadup.mes", GOOD, 0L);
00301     else if (module == GROUP_INDUSTRY_L)
00302         ok_dial_box ("ltindustryup.mes", GOOD, 0L);
00303     else if (module == GROUP_UNIVERSITY)
00304         ok_dial_box ("universityup.mes", GOOD, 0L);
00305     else if (module == GROUP_OREMINE)
00306     {
00307         if (GROUP_OREMINE_TECH > 0)
00308             ok_dial_box ("oremineup.mes", GOOD, 0L);
00309     }
00310     else if (module == GROUP_PORT)      /* exports are the same */
00311         ok_dial_box ("import-exportup.mes", GOOD, 0L);
00312     else if (module == GROUP_INDUSTRY_H)
00313         ok_dial_box ("hvindustryup.mes", GOOD, 0L);
00314     else if (module == GROUP_PARKLAND)
00315     {
00316         if (GROUP_PARKLAND_TECH > 0)
00317             ok_dial_box ("parkup.mes", GOOD, 0L);
00318     }
00319     else if (module == GROUP_RECYCLE)
00320         ok_dial_box ("recycleup.mes", GOOD, 0L);
00321     else if (module == GROUP_RIVER)
00322     {
00323         if (GROUP_WATER_TECH > 0)
00324             ok_dial_box ("riverup.mes", GOOD, 0L);
00325     }
00326     else if (module == GROUP_HEALTH)
00327         ok_dial_box ("healthup.mes", GOOD, 0L);
00328     else if (module == GROUP_ROCKET)
00329         ok_dial_box ("rocketup.mes", GOOD, 0L);
00330     else if (module == GROUP_SCHOOL)
00331     {
00332         if (GROUP_SCHOOL_TECH > 0)
00333             ok_dial_box ("schoolup.mes", GOOD, 0L);
00334     }
00335     else if (module == GROUP_BLACKSMITH)
00336     {
00337         if (GROUP_BLACKSMITH_TECH > 0)
00338             ok_dial_box ("blacksmithup.mes", GOOD, 0L);
00339     }
00340     else if (module == GROUP_MILL)
00341     {
00342         if (GROUP_MILL_TECH > 0)
00343             ok_dial_box ("millup.mes", GOOD, 0L);
00344     }
00345     else if (module == GROUP_POTTERY)
00346     {
00347         if (GROUP_POTTERY_TECH > 0)
00348             ok_dial_box ("potteryup.mes", GOOD, 0L);
00349     }
00350     else if (module == GROUP_FIRESTATION)
00351         ok_dial_box ("firestationup.mes", GOOD, 0L);
00352     else if (module == GROUP_CRICKET)
00353         ok_dial_box ("cricketup.mes", GOOD, 0L);
00354 }
00355     
00356 
00357 /* handle_module_buttons:  mouse handler for module window */
00358 
00359 void 
00360 module_buttons_handler (int x, int y, int mbutton)
00361 {
00362     int module;
00363     int row, col;
00364 
00365 
00366     /* Figure out which row and column the click came in */
00367     row = (y / (SELECT_BUTTON_INTERVAL));
00368     if ((y % SELECT_BUTTON_INTERVAL) <= 8) 
00369       return;  /* in the gap */
00370     
00371     col = (x / (SELECT_BUTTON_INTERVAL));
00372     if ((x % SELECT_BUTTON_INTERVAL) <= 8) 
00373       return;
00374     
00375     module = row + (module_rows * col);
00376 
00377     select_module (module, mbutton);
00378 }
00379 
00380 
00381 /* select_module: low level click handler;  display help or change to
00382    module, depending on mbutton (right or left click) */
00383 
00384 void
00385 select_module (int module, int mbutton)
00386 {
00387     if (module_tflag[module] == 0 && mbutton != LC_MOUSE_RIGHTBUTTON) {
00388         ok_dial_box ("not_enough_tech.mes", BAD, 0L);
00389         return;
00390     }
00391     if (mbutton == LC_MOUSE_RIGHTBUTTON 
00392         || (module_help_flag[module] == 0 
00393             && !suppress_firsttime_module_help))
00394     {
00395         activate_help (module_help[module]);
00396         if (mbutton != LC_MOUSE_RIGHTBUTTON)
00397             module_help_flag[module] = 1;
00398         if (mbutton == LC_MOUSE_RIGHTBUTTON)
00399             return;
00400     }
00401 
00402     unhighlight_module_button (selected_module);
00403     highlight_module_button (module);
00404     selected_module = module;
00405 
00406     set_selected_module (module_type[module]);
00407 }
00408 
00409 
00410 /* highlight_module_button: draw a nice border around a selected module */
00411 
00412 void
00413 highlight_module_button (int module)
00414 {
00415     int x, y;
00416     int row, col;
00417 
00418     if (module == 0) {
00419         row = 0;
00420         col = 0;
00421     } else {
00422         row = module % module_rows;
00423         col = module / module_rows;
00424     }
00425 
00426     x = 8 + (col * 24) + mbw->x;
00427     y = 8 + (row * 24) + mbw->y;
00428 
00429     hide_mouse ();
00430     draw_small_bezel(x + 1, y + 1,
00431                      SELECT_BUTTON_WIDTH - 2, SELECT_BUTTON_WIDTH - 2,
00432                      yellow(0));
00433     redraw_mouse ();
00434 }
00435 
00436 
00437 /* unhighlight_module_button: replace highlight border with regular one */
00438 
00439 void
00440 unhighlight_module_button (int module)
00441 {
00442     int x, y;
00443 
00444     int row, col;
00445 
00446     if (module == 0) {
00447         row = 0;
00448         col = 0;
00449     } else {
00450         row = module % module_rows;
00451         col = module / module_rows;
00452     }
00453 
00454     x = 8 + (col * 24) + mbw->x;
00455     y = 8 + (row * 24) + mbw->y;
00456 
00457     hide_mouse ();
00458     draw_small_bezel(x + 1, y + 1, 
00459                      SELECT_BUTTON_WIDTH - 2, SELECT_BUTTON_WIDTH - 2,
00460                      blue(0));
00461     redraw_mouse ();
00462 }
00463 
00464 
00465 /* draw_selected_module_cost: write info about the module to the status area */
00466 
00467 void
00468 draw_selected_module_cost ()
00469 {
00470     Rect* b = &scr.select_message;
00471     char s[100];
00472     char coststr[20];
00473     char bldzstr[20];
00474 
00475     selected_module_cost = get_type_cost (selected_module_type);
00476 
00477     commify(coststr,20,selected_module_cost);
00478     commify(bldzstr,20,main_groups[selected_module_group].bul_cost);
00479 
00480     if (selected_module_group == GROUP_BARE) 
00481         snprintf (s, 100, _("Bulldoze - cost varies"));
00482     else
00483         snprintf (s, 100, _("%s %s  Bulldoze %s"),
00484                   _(main_groups[selected_module_group].name),
00485                   coststr, bldzstr);
00486 
00487     Fgl_fillbox (b->x, b->y, 42 * 8, 8, TEXT_BG_COLOUR);
00488     Fgl_write (b->x, b->y, s);
00489 }
00490 
00491 
00492 /* draw_modules:  fill a box and then draw all graphics */
00493 
00494 void
00495 draw_modules (void)
00496 {
00497     int i;
00498     Fgl_fillbox (mbw->x, mbw->y, mbw->w, mbw->h, white (20));
00499 
00500     for (i = 0; i < NUMOF_MODULES; i++) 
00501       draw_module (sbut[i], module_graphic[sbut[i]]);
00502 
00503     highlight_module_button(selected_module);
00504 
00505 }
00506 
00507 void
00508 draw_module (int module, char *graphic)
00509 {
00510     int x, y, xx, yy;
00511     int row, col;
00512     
00513     if (module == 0) {
00514         row = 0;
00515         col = 0;
00516     } else {
00517         row = module % module_rows;
00518         col = module / module_rows;
00519     }
00520 
00521     x = 8 + (col * 24);
00522     y = 8 + (row * 24);
00523 
00524     hide_mouse ();
00525 
00526     Fgl_putbox (x + mbw->x, y + mbw->y, 16, 16, graphic);
00527     unhighlight_module_button (module);
00528 
00529     /* Modules we don't have enough tech for are greyed out. */
00530     if (module_tflag[module] == 0)
00531     {
00532         for (yy = -3; yy < 19; yy++)
00533             for (xx = -3; xx < 19; xx += 2)
00534                 Fgl_setpixel (x + xx + (yy % 2) + mbw->x,
00535                               y + yy + mbw->y, white (15));
00536     }
00537     redraw_mouse ();
00538 }
00539 
00540 
00541 /* inv_sbut:  the order and the number of the module aren't the same; 
00542    inv_sbut converts between a geographical location and an array location */
00543 
00544 int
00545 inv_sbut (int button)
00546 {
00547     int i, j;
00548     for (i = 0; i < 32; i++)    
00549         if (sbut[i] == button)
00550         {
00551             j = i;
00552             return (j);
00553         }
00554     /* XXX: do we need this debug stuff? */
00555     /* GCS: This is a fatal error.  Probably OK. */
00556     printf ("Button=%d\n", button);
00557     for (i = 0; i < 32; i++)
00558         printf ("%5d", sbut[i]);
00559     printf ("\n");
00560     do_error ("An inv_sbut error has happened.");
00561     return (-1);                        /* can't get here */
00562 }
00563 
00564 void
00565 set_selected_module (int type)
00566 {
00567     selected_module_type = type;
00568     selected_module_group = get_group_of_type(selected_module_type);
00569     if (selected_module_type == CST_RESIDENCE_LL) {
00570         choose_residence ();
00571     }
00572 
00573 #ifdef LC_X11  /* XXX: WCK: shouldn't be any platform specific code here */
00574     if (selected_module_group == GROUP_BARE) 
00575         XDefineCursor (display.dpy, display.win, pirate_cursor);
00576     else
00577         XDefineCursor (display.dpy, display.win, None);
00578 #endif
00579 
00580     draw_selected_module_cost();
00581 
00582     if (selected_module_type == CST_GREEN) {
00583         draw_main_window_box (red (8));
00584     } else {
00585         draw_main_window_box (green (8));
00586         monument_bul_flag = 0;
00587         river_bul_flag = 0;
00588         shanty_bul_flag = 0;
00589     }
00590 
00591 
00592     selected_module_type = type;
00593     selected_module_group = get_group_of_type(selected_module_type);
00594     draw_selected_module_cost ();   /* sets module cost */
00595 }

Generated on Sun Dec 26 11:23:25 2004 for lincity by  doxygen 1.3.9.1