home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Xconq 7.1.0 / src / xconq-7.1.0 / mac / macinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  77.8 KB  |  2,653 lines  |  [TEXT/R*ch]

  1. /* Initialization for the Mac interface to Xconq.
  2.    Copyright (C) 1992, 1993, 1994, 1995, 1996 Stanley T. Shebs.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING. */
  8.  
  9. #include "conq.h"
  10. extern int numremotes;
  11. extern void low_send(int id, char *buf);
  12. extern int low_receive(int *id, char *buf, int maxchars, int timeout);
  13. extern int allbedesigners;
  14. extern void update_debugging PARAMS ((void));
  15. extern void toggle_debugging PARAMS ((int *flagp));
  16. #include "macconq.h"
  17.  
  18. /* Limit on the number of check boxes and sliders in the variants dialog. */
  19.  
  20. #define MAXCHECKBOXES 7
  21. #define MAXSLIDERS 3
  22.  
  23. #define toggle_checkbox(ih) \
  24.   SetCtlValue((ControlHandle) (ih), !GetCtlValue((ControlHandle) (ih)))
  25.  
  26. /* (not quite right - come up with a better test) */
  27.  
  28. #define game_already_loaded() (numutypes > 0)
  29.  
  30. #define t_color_defined(t) (hasColorQD && tcolors != NULL && tcolors[t] != NULL)
  31.  
  32. /* Private functions. */
  33.  
  34. static pascal Boolean filter_splash(DialogPtr dialog, EventRecord *evt, short *itemhit);
  35. static pascal void draw_game_list(WindowPtr win, short ditem);
  36. static pascal void draw_game_blurb(WindowPtr win, short ditem);
  37. static pascal void draw_game_picture(WindowPtr win, short ditem);
  38. static void center_pict(PicHandle picture, Rect *rect);
  39. static Module *module_from_cell(Cell cell);
  40. static pascal Boolean filter_new_game(DialogPtr dialog, EventRecord *evt, short *itemhit);
  41. static void display_selected_game(void);
  42. static void select_game(void);
  43. static pascal void draw_variant_slider(WindowPtr win, short ditem);
  44. static pascal void draw_variant_help(WindowPtr win, short ditem);
  45. static void set_variant_item(int di, int flag, int flag2, int val);
  46. static void interpret_variants(void);
  47. static void implement_variants(void);
  48. static void special_xform(int x, int y, int *sxp, int *syp);
  49. static pascal void draw_world_picture(WindowPtr win, short ditem);
  50. static pascal Boolean filter_world_setup(DialogPtr dialog, EventRecord *evt, short *itemhit);
  51. static int world_shape_dialog(void);
  52. static int real_time_dialog(void);
  53. static int more_variants_dialog(void);
  54. static pascal void draw_player_setup_list(WindowPtr win, short ditem);
  55. static pascal void draw_player_setup_advantage(WindowPtr win, short ditem);
  56. static int adjust_advantage(Player *player, Side *side, int amt);
  57. static void select_player(int n);
  58. static int player_setup_dialog(void);
  59. static void set_player_setup_button_states(void);
  60. static pascal void draw_progress(WindowPtr win, short ditem);
  61. static void add_default_unit_image(ImageFamily *imf, int u, char *name);
  62. static void add_default_terrain_image(ImageFamily *imf, int t, char *name);
  63. static void add_default_emblem_image(ImageFamily *imf, int e, char *name);
  64.  
  65. /* Global variables. */
  66.  
  67. BitMap *bordbitmaps;
  68. BitMap *connbitmaps;
  69.  
  70. ModalFilterUPP filter_splash_proc;
  71.  
  72. /* This is the dialog used to select a game to play. */
  73.  
  74. WindowPtr newgamewin = nil;
  75.  
  76. ListHandle newgamelist = nil;
  77.  
  78. TEHandle newgametext = nil;
  79.  
  80. PicHandle newgamepicture = nil;
  81.  
  82. static Module *selected_game = NULL;
  83.  
  84. UserItemUPP draw_game_list_proc;
  85. UserItemUPP draw_game_blurb_proc;
  86. UserItemUPP draw_game_picture_proc;
  87.  
  88. ModalFilterUPP filter_new_game_proc;
  89.  
  90. /* True if any variants are available. */
  91.  
  92. int any_variants;
  93.  
  94. /* These are true when a standard variant is available to be chosen. */
  95.  
  96. int vary_world_seen;
  97. int vary_see_all;
  98. int vary_world;
  99. int vary_sequential;
  100. int vary_real_time;
  101.  
  102. /* This is the dialog used to set the size and shape of the world. */
  103.  
  104. DialogPtr world_shape_win = nil;
  105.  
  106. /* These are the values that will be used to set world geometry. */
  107.  
  108. int new_circumference;
  109. int new_width;
  110. int new_height;
  111. int new_latitude;
  112. int new_longitude;
  113. int new_scale;
  114.  
  115. Handle hexagon_icon;
  116. Handle cylinder_icon;
  117.  
  118. UserItemUPP draw_world_picture_proc;
  119.  
  120. ModalFilterUPP filter_world_setup_proc;
  121.  
  122. /* This is the dialog used to set realtime limits. */
  123.  
  124. DialogPtr real_time_win = nil;
  125.  
  126. int new_time_for_game;
  127. int new_time_per_side;
  128. int new_time_per_turn;
  129.  
  130. /* This is the dialog used to set more variants. */
  131.  
  132. DialogPtr more_variants_win = nil;
  133.  
  134. /* This is the variant-setting dialog. */
  135.  
  136. DialogPtr variants_win = nil;
  137.  
  138. int numcheckboxes;
  139. int numsliders;
  140.  
  141. Variant *checkboxes[MAXCHECKBOXES];
  142. Variant *sliders[MAXSLIDERS];
  143.  
  144. /* These are the new user-chosen values for variants. */
  145.  
  146. int new_seen;
  147. int new_seeall;
  148. int new_sequential;
  149.  
  150. Obj *variants;
  151.  
  152. UserItemUPP draw_variant_slider_proc;
  153. UserItemUPP draw_variant_help_proc;
  154.  
  155. /* This is the player setup dialog. */
  156.  
  157. WindowPtr playersetupwin = nil;
  158.  
  159. PicHandle updownpicture = nil;
  160. PicHandle updownpictureup = nil;
  161. PicHandle updownpicturedown = nil;
  162.  
  163. int playerh = 22;
  164. int playerbaseline = 14;
  165.  
  166. int selectedplayer = -1;
  167.  
  168. #define selected_a_player() (between(0, selectedplayer, numsides - 1))
  169.  
  170. #define selected_player()  \
  171.   (selected_a_player() ? assignments[selectedplayer].player : NULL)
  172.  
  173. #define selected_side() (selected_a_player() ? assignments[selectedplayer].side : NULL)
  174.  
  175. UserItemUPP draw_player_setup_list_proc;
  176. UserItemUPP draw_player_setup_advantage_proc;
  177.  
  178. /* Caches of images and colors. */
  179.  
  180. ImageFamily **uimages = NULL;
  181. ImageFamily **timages = NULL;
  182. ImageFamily **eimages = NULL;
  183.  
  184. ImageColor **tcolors = NULL;
  185.  
  186. Pattern *animation_patterns;
  187.  
  188. CursHandle readprogressors[NUMcParens];
  189. CursHandle progressors[NUMcSynth];
  190. CursHandle movecursors[NUMcMoves];
  191. CursHandle nomovecursor;
  192. CursHandle allmovecursor;
  193. CursHandle grayarrowcursor;
  194. CursHandle opencrosscursor;
  195. CursHandle firecursor;
  196. CursHandle watchcursor;
  197.  
  198. /* This value is the current progress cursor. */
  199.  
  200. int curcurs = 0;
  201.  
  202. /* The initialization progress dialog. */
  203.  
  204. DialogPtr progresswin = nil;
  205.  
  206. UserItemUPP draw_progress_proc;
  207.  
  208. /* The current value of the percent progress. */
  209.  
  210. int progress;
  211.  
  212. /* The previous value of the percent progress. */
  213.  
  214. int lastprogress;
  215.  
  216. enum grays gridgray = whitegray;
  217. enum grays unseengray = whitegray;
  218. enum grays bggray = mediumgray;
  219.  
  220. RGBColor gridcolor;
  221. RGBColor unseencolor;
  222. RGBColor blackcolor;
  223.  
  224. int grid_matches_unseen = FALSE;
  225.  
  226. PicHandle dotdotdotpicture = nil;
  227.  
  228. int first_windows;
  229.  
  230. /* Set up any generic patterns that will be needed. */
  231.  
  232. void
  233. init_patterns()
  234. {
  235.     int i, j, k;
  236.     Handle animpat;
  237.  
  238.     animation_patterns = (Pattern *) xmalloc(8 * sizeof(Pattern));
  239.     animpat = GetResource('PAT#', 128);
  240.     HLock(animpat);
  241.     k = 0;
  242.     for (i = 0; i < 8; ++i) {
  243.         for (j = 0; j < 8; ++j) {
  244.             SET_PAT_ELT(animation_patterns[i], j, (*animpat)[k++]);
  245.         }
  246.     }
  247.     HUnlock(animpat);
  248. }
  249.  
  250. /* Set up any generic icons that will be needed. */
  251.  
  252. void
  253. init_icons()
  254. {
  255.     hexagon_icon = GetResource('ICON', 128);
  256.     cylinder_icon = GetResource('ICON', 129);
  257. }
  258.  
  259. /* Set up the various cursors we'll be using. */
  260.  
  261. void
  262. init_cursors()
  263. {
  264.     int i;
  265.  
  266.     /* Get all the cursors used to indicate reader progress. */
  267.     for (i = 0; i < NUMcParens; ++i) {
  268.         readprogressors[i] = GetCursor(cParens1 + i);
  269.     }
  270.     /* Get all the cursors used to indicate synthesis progress. */
  271.     for (i = 0; i < NUMcSynth; ++i) {
  272.         progressors[i] = GetCursor(cSynth1 + i);
  273.     }
  274.     /* Get all the cursors indicating the move direction. */
  275.     for (i = 0; i < NUMcMoves; ++i) {
  276.         movecursors[i] = GetCursor(cMove1 + i);
  277.     }
  278.     /* Miscellaneous cursors. */
  279.     nomovecursor = GetCursor(cNoMove);
  280.     allmovecursor = GetCursor(cAllMove);
  281.     grayarrowcursor = GetCursor(cGrayArrow);
  282.     opencrosscursor = GetCursor(cOpenCross);
  283.     firecursor = GetCursor(138);
  284.     watchcursor = GetCursor(watchCursor);
  285.     /* Designer-related cursors will be done later, if needed at all. */
  286.  
  287.     dotdotdotpicture = (PicHandle) GetResource('PICT', 134);
  288. }
  289.  
  290. static pascal Boolean
  291. filter_splash(DialogPtr dialog, EventRecord *evt, short *itemhit)
  292. {
  293.     char ch;
  294.  
  295.     /* Look for the right kind of event. */
  296.     switch (evt->what) {
  297.         case keyDown:
  298.             ch = evt->message & charCodeMask;
  299.             if (ch == 3 || ch == 13) {
  300.                 *itemhit = diSplashNew;
  301.                 return TRUE;
  302.             }
  303. #ifdef DEBUGGING
  304.             /* A secret way to get debugging at startup. */
  305.             /* (should have some sort of feedback?) */
  306.             if (ch == 'D') {
  307.                 toggle_debugging(&Debug);
  308.             }
  309.             if (ch == 'M') {
  310.                 toggle_debugging(&DebugM);
  311.             }
  312.             if (ch == 'G') {
  313.                 toggle_debugging(&DebugG);
  314.             }
  315.             if (ch == 'P') {
  316.                 toggle_profiling();
  317.             }
  318. #endif
  319.             break;
  320.     }
  321.     return FALSE;
  322. }
  323.  
  324. /* Display the initial splash screen, and let the player choose New/Open/Connect/Quit. */
  325.  
  326. int
  327. do_splash_box()
  328. {
  329.     short ditem;
  330.     Str255 tmpstr;
  331.     WindowPtr win;
  332.     PicHandle pic;
  333.     short itemtype;  Handle itemhandle;  Rect itemrect;
  334.  
  335.     if (filter_splash_proc == NULL)
  336.       filter_splash_proc = NewModalFilterProc(filter_splash);
  337.  
  338.     win = GetNewDialog(dSplash, NULL, (DialogPtr) -1L);
  339.     /* Fill in the kernel's version and copyright. */
  340.     GetDItem(win, diSplashVersion, &itemtype, &itemhandle, &itemrect);
  341.     c2p(version_string(), tmpstr);
  342.     SetIText(itemhandle, tmpstr);
  343.     GetDItem(win, diSplashCopyright, &itemtype, &itemhandle, &itemrect);
  344.     c2p(copyright_string(), tmpstr);
  345.     SetIText(itemhandle, tmpstr);
  346.     /* Substitute a color picture if possible. */
  347.     if (hasColorQD) {
  348.         GetDItem(win, diSplashPicture, &itemtype, &itemhandle, &itemrect);
  349.         pic = (PicHandle) GetResource('PICT', pSplashColor);
  350.         if (pic != nil) {
  351.             SetDItem(win, diSplashPicture, itemtype, (Handle) pic, &itemrect);
  352.         }
  353.     }
  354.     ShowWindow(win);
  355.     SelectWindow(win);
  356.     ModalDialog(filter_splash_proc, &ditem);
  357.     /* We don't loop around here, just return the ditem and let caller decide what to do. */
  358.     DisposDialog(win);
  359.     return ditem;
  360. }
  361.  
  362. /* Dialog app-defined item callback that displays the list of possible games. */
  363.  
  364. static pascal void
  365. draw_game_list(WindowPtr win, short ditem)
  366. {
  367.     short itemtype;  Handle itemhandle;  Rect itemrect;
  368.  
  369.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  370.     /* Draw the list of available games. */
  371.     LUpdate(newgamewin->visRgn, newgamelist);
  372.     /* Frame it nicely. */
  373.     --itemrect.left;
  374.     FrameRect(&itemrect);
  375. }
  376.  
  377. /* Dialog app-defined item callback to display the game description. */
  378.  
  379. static pascal void
  380. draw_game_blurb(WindowPtr win, short ditem)
  381. {
  382.     GrafPtr oldport;
  383.     short itemtype;  Handle itemhandle;  Rect itemrect;
  384.  
  385.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  386.     /* Make sure the text is up-to-date. */
  387.     GetPort(&oldport);
  388.     SetPort(newgamewin);
  389.     TEUpdate(&itemrect, newgametext);    
  390.     SetPort(oldport);
  391.     /* Frame it. */
  392.     InsetRect(&itemrect, -1, -1);
  393.     FrameRect(&itemrect);
  394. }
  395.  
  396. /* Dialog app-defined item callback to display the game picture, if available. */
  397.  
  398. static pascal void
  399. draw_game_picture(WindowPtr win, short ditem)
  400. {
  401.     RgnHandle tmprgn;
  402.     Rect cliprect;
  403.     short itemtype;  Handle itemhandle;  Rect itemrect;
  404.  
  405.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  406.     EraseRect(&itemrect);
  407.     if (newgamepicture != nil) {
  408.         tmprgn = NewRgn();
  409.         GetClip(tmprgn);
  410.         cliprect = itemrect;
  411.         ClipRect(&cliprect);
  412.         center_pict(newgamepicture, &itemrect);
  413.         DrawPicture(newgamepicture, &itemrect);
  414.         SetClip(tmprgn);
  415.         DisposeRgn(tmprgn);
  416.     }
  417. #ifdef DESIGNERS
  418.     /* A low-impact feedback that this game will launch directly into designing. */
  419.     if (allbedesigners) {
  420.         PenSize(3, 3);
  421.         itemrect.right -= 3;  itemrect.bottom -= 3;
  422.         FrameRect(&itemrect);
  423.         PenNormal();
  424.     }
  425. #endif /* DESIGNERS */
  426. }
  427.  
  428. /* Utility that figures out how to center a picture in a rectangle. */
  429.  
  430. void
  431. center_pict(PicHandle picture, Rect *rect)
  432. {
  433.     int picth, pictv;
  434.     Rect initrect, pictrect;
  435.  
  436.     initrect = *rect;
  437.     pictrect = (*picture)->picFrame;
  438.     picth = pictrect.right - pictrect.left;  pictv = pictrect.bottom - pictrect.top;
  439.     rect->left += (initrect.right - initrect.left) / 2 - picth / 2;
  440.     rect->top += (initrect.bottom - initrect.top) / 2 - pictv / 2;
  441.     rect->right = rect->left + picth;
  442.     rect->bottom = rect->top + pictv;
  443. }
  444.  
  445. static Module *
  446. module_from_cell(Cell cell)
  447. {
  448.     return (cell.v < numgames ? possible_games[cell.v] : NULL);
  449. }
  450.  
  451. /* This filter for the new game dialog just handles the game list, updating the
  452.    other items to reflect the currently selected game. */
  453.  
  454. int lastnewgameclick;
  455. Point lastnewgamemouse;
  456.  
  457. static pascal Boolean
  458. filter_new_game(DialogPtr dialog, EventRecord *evt, short *itemhit)
  459. {
  460.     GrafPtr oldport;
  461.     Point pt, origpt;
  462.     short ditem;
  463.     char ch;
  464.     short itemtype;  Handle itemhandle;  Rect itemrect;
  465.  
  466.     /* Look for the right kind of event. */
  467.     switch (evt->what) {
  468.         case mouseDown:
  469.             GetPort(&oldport);
  470.             SetPort(dialog);
  471.             pt = origpt = evt->where;
  472.             GlobalToLocal(&pt);
  473.             ditem = FindDItem(dialog, pt) + 1;
  474.             if (ditem == diNewGameList) {
  475.                 /* Loop around in list handling. */
  476.                 LClick(pt, evt->modifiers, newgamelist);
  477.                 /* We're finished clicking, show the results (if not in the list's scrollbar). */
  478.                 GetDItem(dialog, ditem, &itemtype, &itemhandle, &itemrect);
  479.                 if (pt.h < (itemrect.right - sbarwid)) {
  480.                     select_game();
  481.                     display_selected_game();
  482.                     SetPort(oldport);
  483.                     if (TickCount() - lastnewgameclick < GetDblTime()
  484.                         && between(-3, origpt.h - lastnewgamemouse.h, 3)
  485.                         && between(-3, origpt.v - lastnewgamemouse.v, 3)
  486.                         ) {
  487.                         *itemhit = diNewGameOK;
  488.                     } else {
  489.                         lastnewgameclick = TickCount();
  490.                         lastnewgamemouse = evt->where;
  491.                         *itemhit = diNewGameList;
  492.                     }
  493.                 }
  494.                 return TRUE;
  495.             } else {
  496.                 SetPort(oldport);
  497.                 return FALSE;
  498.             }
  499.             break;
  500.         case keyDown:
  501.             ch = evt->message & charCodeMask;
  502.             if (ch == 3 || ch == 13) {
  503.                 if (selected_game) {
  504.                     *itemhit = diNewGameOK;
  505.                     return TRUE;
  506.                 }
  507.             }
  508. #ifdef DESIGNERS
  509.             /* A secret way to get into designing at startup. */
  510.             if (ch == 'd') {
  511.                 allbedesigners = !allbedesigners;
  512.                 DrawDialog(newgamewin);
  513.             }
  514. #endif
  515. #ifdef DEBUGGING
  516.             /* A secret way to get debugging at startup. */
  517.             if (ch == 'D') {
  518.                 toggle_debugging(&Debug);
  519.                 DrawDialog(newgamewin);
  520.             }
  521.             if (ch == 'M') {
  522.                 toggle_debugging(&DebugM);
  523.                 DrawDialog(newgamewin);
  524.             }
  525.             if (ch == 'G') {
  526.                 toggle_debugging(&DebugG);
  527.                 DrawDialog(newgamewin);
  528.             }
  529.             if (ch == 'P') {
  530.                 toggle_profiling();
  531.                 DrawDialog(newgamewin);
  532.             }
  533. #endif
  534.             break;
  535.     }
  536.     return FALSE;
  537. }
  538.  
  539. /* Given a selected game, display assorted info about it - basically a preview so that
  540.    prospective players can see what they're getting into.  If no game has been selected,
  541.    then this routine clears the displays. */
  542.  
  543. static void
  544. display_selected_game()
  545. {
  546.     short itemtype;  Handle itemhandle;  Rect itemrect;
  547.     char *desc;
  548.     Str255 tmpstr;
  549.     PicHandle oldnewgamepicture;
  550.  
  551.     DGprintf("display selected game %s\n", module_desig(selected_game));
  552.     /* Set the blurb for the selected game. */
  553.     GetDItem(newgamewin, diNewGameBlurb, &itemtype, &itemhandle, &itemrect);
  554.     TESetSelect(0, 100000, newgametext);
  555.     TECut(newgametext);
  556.     if (selected_game /* && selected_game->complete */) {
  557.         if (selected_game->blurb) {
  558.             desc = selected_game->blurb;
  559.         } else {
  560.             desc = "??? experimental ???";
  561.         }
  562.     } else {
  563.         desc = "";
  564.     }
  565.     TESetText(desc, strlen(desc), newgametext);
  566.     TEUpdate(&itemrect, newgametext);
  567.     /* Load a picture if one can be found. */
  568.     /* (should look for picture explicitly named in a game-module slot first) */
  569.     oldnewgamepicture = newgamepicture;
  570.     newgamepicture = nil;
  571.     if (selected_game) {
  572.         sprintf(spbuf, "%s game", selected_game->name);
  573.         c2p(spbuf, tmpstr);
  574.         newgamepicture = (PicHandle) GetNamedResource('PICT', tmpstr);
  575.         if (newgamepicture == nil) {
  576.             sprintf(spbuf, "%s", selected_game->name);
  577.             c2p(spbuf, tmpstr);
  578.             newgamepicture = (PicHandle) GetNamedResource('PICT', tmpstr);
  579.         }
  580.     } else {
  581.         /* Nothing to display - how boring! */
  582.     }
  583.     /* Gray out the OK button if no game selected. */
  584.     GetDItem(newgamewin, diNewGameOK, &itemtype, &itemhandle, &itemrect);
  585.     HiliteControl((ControlHandle) itemhandle, (selected_game ? 0 : 255));
  586.     /* We have to force redraw to get the picture item updated. */
  587.     if (oldnewgamepicture != newgamepicture)
  588.       DrawDialog(newgamewin);
  589. }
  590.  
  591. /* This is a modal dialog from which the user selects a game and sets options. */
  592.  
  593. void
  594. new_game_dialog()
  595. {
  596.     int done = FALSE;
  597.     short ditem, i;
  598.     Point cellsize;
  599.     Cell tmpcell;
  600.     Rect listrect, destrect, viewrect;
  601.     Module *module;
  602.     char *gamename;
  603.     char tmpbuf[255];
  604.     short itemtype;  Handle itemhandle;  Rect itemrect;
  605.  
  606.     if (filter_new_game_proc == NULL)
  607.       filter_new_game_proc = NewModalFilterProc(filter_new_game);
  608.     if (draw_game_list_proc == NULL)
  609.       draw_game_list_proc = NewUserItemProc(draw_game_list);
  610.     if (draw_game_blurb_proc == NULL)
  611.       draw_game_blurb_proc = NewUserItemProc(draw_game_blurb);
  612.     if (draw_game_picture_proc == NULL)
  613.       draw_game_picture_proc = NewUserItemProc(draw_game_picture);
  614.  
  615.     collect_possible_games();
  616.     if (newgamewin == nil) {
  617.         newgamewin = GetNewDialog(dNewGame, NULL, (DialogPtr) -1);
  618.         SetPort(newgamewin);
  619.         TextFont(newYork);
  620. /*        TextSize(10); */
  621.         /* Set up the app-defined item that lists games. */
  622.         GetDItem(newgamewin, diNewGameList, &itemtype, &itemhandle, &itemrect);
  623.         SetDItem(newgamewin, diNewGameList, itemtype, (Handle) draw_game_list_proc, &itemrect);
  624.         SetPt(&cellsize, 0, 0);
  625.         listrect.top = 0;  listrect.left = 0;
  626.         listrect.bottom = 0;  listrect.right = 1;
  627.         itemrect.top += 1;
  628.         itemrect.bottom -= 1;  itemrect.right -= sbarwid + 1;
  629.         /* Create the list of games itself and fill it in. */
  630.         newgamelist = LNew(&itemrect, &listrect, cellsize, 0, newgamewin, 0,0,0,TRUE);
  631.         SetPt(&tmpcell, 0, 0);
  632.         for (i = 0; i < numgames; ++i) {
  633.             module = possible_games[i];
  634.             gamename = (module->title ? module->title : module->name);
  635.             sprintf(tmpbuf, "%s%s", (module->basemodulename ? "-  " : ""), gamename);
  636.             LAddRow(1, tmpcell.v, newgamelist);
  637.             LSetCell(tmpbuf, strlen(tmpbuf), tmpcell, newgamelist);
  638.             ++tmpcell.v;
  639.         }
  640.         GetDItem(newgamewin, diNewGameBlurb, &itemtype, &itemhandle, &itemrect);
  641.         SetDItem(newgamewin, diNewGameBlurb, itemtype, (Handle) draw_game_blurb_proc, &itemrect);
  642.         destrect = itemrect;
  643.         viewrect = itemrect;
  644. /*        TextSize(10);  */
  645.         newgametext = TENew(&destrect, &viewrect);
  646.         GetDItem(newgamewin, diNewGamePicture, &itemtype, &itemhandle, &itemrect);
  647.         SetDItem(newgamewin, diNewGamePicture, itemtype, (Handle) draw_game_picture_proc, &itemrect);
  648.         newgamepicture = nil;
  649.     }
  650.     update_new_game_list();
  651.     SetPt(&tmpcell, 0, 0);
  652.     LSetSelect(TRUE, tmpcell, newgamelist);  /* (do this only for new/changed list?) */
  653.     select_game();
  654.     display_selected_game();
  655.     SetCursor(&QD(arrow));
  656.     ShowWindow(newgamewin);
  657.     LDoDraw(1, newgamelist);
  658.     /* Loop around here until the player picks a game to play, or cancels. */
  659.     while (!done) {
  660.         draw_default_button(newgamewin, diNewGameOK);
  661.         ModalDialog(filter_new_game_proc, &ditem);
  662.         switch (ditem) {
  663.             case diNewGameOK:
  664.                 /* Make this dialog disappear now, startup may take a long time. */
  665.                 HideWindow(newgamewin);
  666.                 if (start_new_game()) {
  667.                     done = TRUE;
  668.                 } else {
  669.                     /* If we failed to start the new game, just go around again. */
  670.                     ShowWindow(newgamewin);
  671.                     SelectWindow(newgamewin);
  672.                 }
  673.                 break;
  674.             case diNewGameCancel:
  675.                 done = TRUE;
  676.                 break;
  677.             default:
  678.                 break;
  679.         }
  680.     }
  681.     /* We're done, OK to throw away the dialog. */
  682.     DisposDialog(newgamewin);
  683.     newgamewin = nil;
  684.     /* (should release TEs and lists also) */
  685. }
  686.  
  687. /* Select a game from the list of possible games. */
  688.  
  689. static void
  690. select_game()
  691. {
  692.     Cell tmpcell;
  693.  
  694.     SetPt(&tmpcell, 0, 0);
  695.     if (game_already_loaded()) {
  696.         /* selected_game is already set correctly. */
  697.     } else if (LGetSelect(TRUE, &tmpcell, newgamelist)) {
  698.         selected_game = module_from_cell(tmpcell);
  699.     } else {
  700.         selected_game = NULL;
  701.     }
  702. }
  703.  
  704. /* When the new game OK button is hit, this will do the work of getting things started,
  705.    possibly pausing to ask about player/side setup. */
  706.  
  707. int
  708. start_new_game()
  709. {
  710.     if (selected_game == NULL) { /* Just in case... */
  711.         beep();
  712.         return FALSE;
  713.     }
  714.     if (!game_already_loaded()) {
  715.         /* Suck in the selected module.  This step cannot be undone. */
  716.         mainmodule = selected_game;
  717.         load_game_module(selected_game, TRUE);
  718.         /* Change cursor back, in case it was different during loading. */
  719.         SetCursor(&QD(arrow));
  720.         /* If the loaded game is not valid, we will get an alert somewhere in here,
  721.            and possibly bomb out if the player chooses not to continue. */
  722.         check_game_validity();
  723.     }
  724.     return launch_game();
  725. }
  726.  
  727. /* This routine is for when we end up back in the new game dialog,
  728.    after something has already been loaded. */
  729.  
  730. /* (could also include any game that has this one as a base module) */
  731.  
  732. static void
  733. update_new_game_list()
  734. {
  735.     char *gamename;
  736.     Point tmpcell;
  737.     
  738.     if (newgamelist != nil && game_already_loaded()) {
  739.         SetPt(&tmpcell, 0, 0);
  740.         /* Clear out the entire list. */
  741.         LDelRow(0, 0, newgamelist);
  742.         /* Add the loaded game back. */ 
  743.         LAddRow(1, tmpcell.v, newgamelist);
  744.         /* Find a name to paste into the list. */
  745.         gamename = (selected_game->title ? selected_game->title : selected_game->name);
  746.         /* Set and select the one cell. */
  747.         LSetCell(gamename, strlen(gamename), tmpcell, newgamelist);
  748.         LSetSelect(TRUE, tmpcell, newgamelist);
  749.     }
  750. }
  751.  
  752. static pascal void
  753. draw_variant_slider(WindowPtr win, short ditem)
  754. {
  755.     Str255 tmpstr;
  756.     short itemtype;  Handle itemhandle;  Rect itemrect;
  757.  
  758.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  759.     /* Always frame. */
  760.     FrameRect(&itemrect);
  761.     if (between(diVariantsFirstSlider, ditem, diVariantsFirstSlider + numsliders - 1)) {
  762.         MoveTo(itemrect.left + 2, itemrect.top + 10);
  763.         c2p(sliders[ditem - diVariantsFirstSlider]->name, tmpstr);
  764.         DrawString(tmpstr);
  765.     } else {
  766.         gray_out_rect(&itemrect);
  767.     }
  768. }
  769.  
  770. static pascal void
  771. draw_variant_help(WindowPtr win, short ditem)
  772. {
  773.     short itemtype;  Handle itemhandle;  Rect itemrect;
  774.  
  775.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  776.     /* Always frame. */
  777.     FrameRect(&itemrect);
  778. }
  779.  
  780. /* The variants dialog basically handles all the player-settable options defined
  781.    by a game. */
  782.  
  783. static int
  784. variants_dialog()
  785. {
  786.     int i, done = FALSE, changed = FALSE;
  787.     char *gamename;
  788.     Str255 tmpstr;
  789.     short ditem;
  790.     short itemtype;  Handle itemhandle;  Rect itemrect;
  791.  
  792.     if (selected_game == NULL)
  793.       init_error("variants on a nonexistent game?");
  794.     interpret_variants();
  795.     /* If no variants defined, get out of here. */
  796.     if (!any_variants)
  797.       return TRUE;
  798.  
  799.     if (draw_variant_slider_proc == NULL)
  800.       draw_variant_slider_proc = NewUserItemProc(draw_variant_slider);
  801.     if (draw_variant_help_proc == NULL)
  802.       draw_variant_help_proc = NewUserItemProc(draw_variant_help);
  803.  
  804.     if (variants_win == nil) {
  805.         variants_win = GetNewDialog(dVariants, NULL, (DialogPtr) -1L);
  806.         /* Set the title of this dialog appropriately. */
  807.         gamename = (selected_game->title ? selected_game->title : selected_game->name);
  808.         sprintf(spbuf, "Variants for \"%s\"", gamename);
  809.         c2p(spbuf, tmpstr);
  810.         GetDItem(variants_win, diVariantsText, &itemtype, &itemhandle, &itemrect);
  811.         SetIText(itemhandle, tmpstr);
  812.         /* Give all the slider app items the same proc. */
  813.         for (i = diVariantsFirstSlider; i < (diVariantsFirstSlider + MAXSLIDERS); ++i) {
  814.             GetDItem(variants_win, i, &itemtype, &itemhandle, &itemrect);
  815.             SetDItem(variants_win, i, itemtype, (Handle) draw_variant_slider_proc, &itemrect);
  816.         }
  817.         GetDItem(variants_win, diVariantsHelp, &itemtype, &itemhandle, &itemrect);
  818.         SetDItem(variants_win, diVariantsHelp, itemtype, (Handle) draw_variant_help_proc, &itemrect);
  819.     }
  820.     /* Display/default the standard variants appropriately. */
  821.     set_variant_item(diVariantsWorldSeen, vary_world_seen, TRUE, new_seen);
  822.     set_variant_item(diVariantsSeeAll, vary_see_all, TRUE, new_seeall);
  823.     set_variant_item(diVariantsSequential, vary_sequential, TRUE, new_sequential);
  824.     set_variant_item(diVariantsWorldSize, vary_world, FALSE, 0);
  825.     set_variant_item(diVariantsRealTime, vary_real_time, FALSE, 0);
  826.     set_variant_item(diVariantsMoreVariants, /* more_variants */ FALSE, FALSE, 0);
  827.     /* For each random checkbox used, give it a title and make it displayable,
  828.        otherwise gray it. */
  829.     for (i = 0; i < MAXCHECKBOXES; ++i) {
  830.         GetDItem(variants_win, diVariantsFirstCheckBox + i, &itemtype, &itemhandle, &itemrect);
  831.         if (i < numcheckboxes) {
  832.             c2p(checkboxes[i]->name, tmpstr);
  833.             SetCTitle((ControlHandle) itemhandle, tmpstr);
  834.             ShowControl((ControlHandle) itemhandle);
  835.             /* should set its initial state based on variant's default */
  836.         } else {
  837.             sprintf(spbuf, "Unused");
  838.             c2p(spbuf, tmpstr);
  839.             SetCTitle((ControlHandle) itemhandle, tmpstr);
  840.             ShowControl((ControlHandle) itemhandle);
  841.             HiliteControl((ControlHandle) itemhandle, 255);
  842.         }
  843.     }
  844.     ShowWindow(variants_win);
  845.     while (!done) {
  846.         draw_default_button(variants_win, diVariantsOK);
  847.         ModalDialog(NULL, &ditem);
  848.         switch (ditem) {
  849.             case diVariantsOK:
  850.                 implement_variants();
  851.                 changed = TRUE;
  852.                 /* Fall through to next case. */
  853.             case diVariantsCancel:
  854.                 done = TRUE;
  855.                 break;
  856.             case diVariantsWorldSeen:
  857.             case diVariantsSeeAll:
  858.             case diVariantsSequential:
  859.                 /* Toggle check boxes. */
  860.                 GetDItem(variants_win, ditem, &itemtype, &itemhandle, &itemrect);
  861.                 toggle_checkbox(itemhandle);
  862.                 break;
  863.             case diVariantsWorldSize:
  864.                 /* Fire up a separate dialog for world geometry. */
  865.                 world_shape_dialog();
  866.                 break;
  867.             case diVariantsRealTime:
  868.                 /* Fire up a separate dialog for real time setup. */
  869.                 real_time_dialog();
  870.                 break;
  871.             case diVariantsMoreVariants:
  872.                 more_variants_dialog();
  873.                 break;
  874.             default:
  875.                 /* Handle all the checkboxes similarly. */
  876.                 if (between(diVariantsFirstCheckBox, ditem, diVariantsFirstCheckBox+MAXCHECKBOXES-1)) {
  877.                     GetDItem(variants_win, ditem, &itemtype, &itemhandle, &itemrect);
  878.                     toggle_checkbox(itemhandle);
  879.                 }
  880.                 break;
  881.         }
  882.     }
  883.     DisposDialog(variants_win);
  884.     variants_win = nil;
  885.     return changed;
  886. }
  887.  
  888. static void
  889. set_variant_item(di, flag, flag2, val)
  890. int di;
  891. int flag, flag2, val;
  892. {
  893.     short itemtype;  Handle itemhandle;  Rect itemrect;
  894.  
  895.     GetDItem(variants_win, di, &itemtype, &itemhandle, &itemrect);
  896.     ShowControl((ControlHandle) itemhandle);
  897.     HiliteControl((ControlHandle) itemhandle, (flag ? 0 : 255));
  898.     if (flag && flag2) {
  899.         SetCtlValue((ControlHandle) itemhandle, val);
  900.     }
  901. }
  902.  
  903. /* Go through all the game's variants and set up appropriate flags. */
  904.  
  905. static void
  906. interpret_variants()
  907. {
  908.     int i;
  909.     char *vartypename;
  910.     Obj *vartmp;
  911.     Variant *var;
  912.  
  913.     any_variants = FALSE;
  914.     vary_world_seen = vary_see_all = vary_sequential = FALSE;
  915.     vary_world = vary_real_time = FALSE;
  916.     if (selected_game == NULL || selected_game->variants == NULL)
  917.       return;
  918.     numcheckboxes = 0;
  919.     numsliders = 0;
  920.     for (i = 0; selected_game->variants[i].id != lispnil; ++i) {
  921.         var = &(selected_game->variants[i]);
  922.         any_variants = TRUE;
  923.         vartypename = c_string(var->id);
  924.         switch (keyword_code(vartypename)) {
  925.             case K_WORLD_SEEN:
  926.                 vary_world_seen = TRUE;
  927.                 new_seen = FALSE;
  928.                 if (var->dflt != lispnil) {
  929.                     vartmp = eval(var->dflt);
  930.                     if (numberp(vartmp)) {
  931.                         new_seen = c_number(vartmp);
  932.                     }
  933.                 }
  934.                 break;
  935.             case K_SEE_ALL:
  936.                 vary_see_all = TRUE;
  937.                 new_seeall = FALSE;
  938.                 if (var->dflt != lispnil) {
  939.                     vartmp = eval(var->dflt);
  940.                     if (numberp(vartmp)) {
  941.                         new_seeall = c_number(vartmp);
  942.                     }
  943.                 }
  944.                 break;
  945.             case K_SEQUENTIAL:
  946.                 vary_sequential = TRUE;
  947.                 new_sequential = FALSE;
  948.                 if (var->dflt != lispnil) {
  949.                     vartmp = eval(var->dflt);
  950.                     if (numberp(vartmp)) {
  951.                         new_sequential = c_number(vartmp);
  952.                     }
  953.                 }
  954.                 break;
  955.             case K_WORLD_SIZE:
  956.                 vary_world = TRUE;
  957.                 /* Start with some defaults. */
  958.                 new_circumference = DEFAULTCIRCUMFERENCE;
  959.                 new_width = DEFAULTWIDTH;  new_height = DEFAULTHEIGHT;
  960.                 new_latitude = 0;  new_longitude = 0;
  961.                 /* If we have explicit defaults, use them. */
  962.                 if (var->dflt != lispnil) {
  963.                     vartmp = var->dflt;
  964.                     new_width = c_number(eval(car(vartmp)));
  965.                     vartmp = cdr(vartmp);
  966.                     if (vartmp != lispnil) {
  967.                         new_height = c_number(eval(car(vartmp)));
  968.                         vartmp = cdr(vartmp);
  969.                     } else {
  970.                         new_height = new_width;
  971.                     }
  972.                     if (vartmp != lispnil) {
  973.                         new_circumference = c_number(eval(car(vartmp)));
  974.                         vartmp = cdr(vartmp);
  975.                     }
  976.                     if (vartmp != lispnil) {
  977.                         new_latitude = c_number(eval(car(vartmp)));
  978.                         vartmp = cdr(vartmp);
  979.                     }
  980.                     if (vartmp != lispnil) {
  981.                         new_longitude = c_number(eval(car(vartmp)));
  982.                     }
  983.                 }
  984.                 break;
  985.             case K_REAL_TIME:
  986.                 vary_real_time = TRUE;
  987.                 /* Start with some defaults. */
  988.                 new_time_for_game = new_time_per_side = new_time_per_turn = 0;
  989.                 /* If we have explicit defaults, use them. */
  990.                 if (var->dflt != lispnil) {
  991.                     vartmp = var->dflt;
  992.                     new_time_for_game = c_number(eval(car(vartmp)));
  993.                     vartmp = cdr(vartmp);
  994.                     if (vartmp != lispnil) {
  995.                         new_time_per_side = c_number(eval(car(vartmp)));
  996.                     } else {
  997.                         new_time_per_side = 0;
  998.                     }
  999.                     vartmp = cdr(vartmp);
  1000.                     if (vartmp != lispnil) {
  1001.                         new_time_per_turn = c_number(eval(car(vartmp)));
  1002.                     } else {
  1003.                         new_time_per_turn = 0;
  1004.                     }
  1005.                 }
  1006.                 break;
  1007.             default:
  1008.                 if (1 /* boolean variant */) {
  1009.                     if (numcheckboxes >= MAXCHECKBOXES) {
  1010.                         init_warning("too many variants, can't set all of them");
  1011.                         break;
  1012.                     }
  1013.                     checkboxes[numcheckboxes++] = var;
  1014.                 } else {
  1015.                     /* (should set up slider) */
  1016.                 }
  1017.                 break;
  1018.         }
  1019.     }
  1020. }
  1021.  
  1022. /* This is where we actually change the state of the game according to the variants. */
  1023.  
  1024. static void
  1025. implement_variants()
  1026. {
  1027.     int i;
  1028.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1029.  
  1030.     variants = lispnil;
  1031.     if (vary_world_seen) {
  1032.         GetDItem(variants_win, diVariantsWorldSeen, &itemtype, &itemhandle, &itemrect);
  1033.         push_key_int_binding(&variants, K_WORLD_SEEN, GetCtlValue((ControlHandle) itemhandle));
  1034.     }
  1035.     if (vary_see_all) {
  1036.         GetDItem(variants_win, diVariantsSeeAll, &itemtype, &itemhandle, &itemrect);
  1037.         push_key_int_binding(&variants, K_SEE_ALL, GetCtlValue((ControlHandle) itemhandle));
  1038.     }
  1039.     if (vary_sequential) {
  1040.         GetDItem(variants_win, diVariantsSequential, &itemtype, &itemhandle, &itemrect);
  1041.         push_key_int_binding(&variants, K_SEQUENTIAL, GetCtlValue((ControlHandle) itemhandle));
  1042.     }
  1043.     if (vary_world) {
  1044.         /* It is critically important that users not be able to reshape already-alloced
  1045.            areas, but do let them know that their request had to be overridden. */
  1046.         if (((area.width > 0 && area.width != new_width)
  1047.             || (area.height > 0 && area.height != new_height)
  1048.             || (world.circumference > 0 && world.circumference != new_circumference))
  1049.             && (1 /* some layers (probably) allocated already */)) {
  1050.             /* (this is misleading, is an "expected" alert) */
  1051.             init_warning("Area dimensions must remain %d x %d, %d around world",
  1052.                          area.width, area.height, world.circumference);
  1053.             new_width = area.width;  new_height = area.height;
  1054.             new_circumference = world.circumference;
  1055.         }
  1056.         /* Make a world-size-setting and glue it into the list of variants. */
  1057.         push_key_cdr_binding(&variants, K_WORLD_SIZE, 
  1058.                               cons(new_number(new_width),
  1059.                                    cons(new_number(new_height),
  1060.                                        cons(new_number(new_circumference),
  1061.                                             cons(new_number(new_longitude),
  1062.                                                  cons(new_number(new_latitude),
  1063.                                               lispnil))))));
  1064.     }
  1065.     if (vary_real_time) {
  1066.         push_key_cdr_binding(&variants, K_REAL_TIME, 
  1067.                          cons(new_number(new_time_for_game),
  1068.                           cons(new_number(new_time_per_side),
  1069.                            cons(new_number(new_time_per_turn),
  1070.                             lispnil))));
  1071.     }
  1072.     /* Implement the random checkbox variants. */
  1073.     for (i = 0; i < numcheckboxes; ++i) {
  1074.         GetDItem(variants_win, diVariantsFirstCheckBox + i, &itemtype, &itemhandle, &itemrect);
  1075.         push_int_binding(&variants, checkboxes[i]->id, GetCtlValue((ControlHandle) itemhandle));
  1076.     }
  1077.     /* (should implement the random slider variants) */
  1078.     do_module_variants(selected_game, variants);
  1079.     /* Recheck everything, the variants might have broken something. */
  1080.     if (any_variants) {
  1081.         check_game_validity();
  1082.     }
  1083. }
  1084.  
  1085. /* Specially adapted version of xform. */
  1086.  
  1087. static void
  1088. special_xform(int x, int y, int *sxp, int *syp)
  1089. {
  1090.     *sxp = (x + y / 2 - (new_height / 4)) / new_scale;
  1091.     *syp = (new_height - y) / new_scale;
  1092. }
  1093.  
  1094.  
  1095. /* This is a callback that draws the world and its areas. */
  1096.  
  1097. /* (Would be really cool to draw accurate curvature over globe here) */
  1098.  
  1099. static pascal void
  1100. draw_world_picture(WindowPtr win, short ditem)
  1101. {
  1102.     int llx, lly, lrx, lry, rx, ry, urx, ury, ulx, uly, lx, ly;
  1103.     Point dims, center, center1, center2;
  1104.     Rect worldrect, hemirect1, hemirect2, arearect;
  1105.     PolyHandle poly;
  1106.     RgnHandle two_hemi_rgn, tmprgn;
  1107.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1108.  
  1109.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1110.     /* Make a framed gray background for the world picture. */
  1111.     FillRect(&itemrect, QDPat(gray));
  1112.     FrameRect(&itemrect);
  1113.     /* Alter the itemrect so as to leave a bit of border. */
  1114.     InsetRect(&itemrect, 5, 5);
  1115.     dims.h = itemrect.right - itemrect.left;  dims.v = itemrect.bottom - itemrect.top;
  1116.     center.h = itemrect.left + dims.h / 2;  center.v = itemrect.top + dims.v / 2;
  1117.     worldrect = itemrect;
  1118.     new_scale = 1;
  1119.     if (new_circumference > dims.h || new_width > dims.h)
  1120.       new_scale = 2;
  1121.     if (new_circumference > 2 * dims.h || new_width > 2 * dims.h)
  1122.       new_scale = 4;
  1123.     /* Draw the whole world as a circle if it's not too big. */
  1124.     if (new_circumference < new_width) {
  1125.         /* If world circumference smaller than area, world is not being used at all. */
  1126.     } else if (new_circumference > 4 * dims.h || new_width > 4 * dims.h) {
  1127.         /* World is too big to draw, just make a white box. */
  1128.         InsetRect(&itemrect, -4, -4);
  1129.         FillRect(&itemrect, QDPat(white));
  1130.     } else {
  1131.         hemirect1.left = center.h - (new_circumference / new_scale) / 2;
  1132.         hemirect1.right = center.h;
  1133.         hemirect1.top = center.v - ((new_circumference / new_scale) / 2) / 2;
  1134.         hemirect1.bottom = center.v + ((new_circumference / new_scale) / 2) / 2;
  1135.         center1.h = hemirect1.left + (hemirect1.right - hemirect1.left) / 2;
  1136.         center1.v = hemirect1.top + (hemirect1.bottom - hemirect1.top) / 2;
  1137.         EraseOval(&hemirect1);
  1138.         FrameOval(&hemirect1);
  1139.         hemirect2 = hemirect1;
  1140.         OffsetRect(&hemirect2, (new_circumference / new_scale) / 2, 0);
  1141.         center2.h = hemirect2.left + (hemirect2.right - hemirect2.left) / 2;
  1142.         center2.v = hemirect2.top + (hemirect2.bottom - hemirect2.top) / 2;
  1143.         EraseOval(&hemirect2);
  1144.         FrameOval(&hemirect2);
  1145.         two_hemi_rgn = NewRgn();
  1146.         OpenRgn();
  1147.         FrameOval(&hemirect1);
  1148.         FrameOval(&hemirect2);
  1149.         CloseRgn(two_hemi_rgn);
  1150.         /* Draw some grid lines. */
  1151.         tmprgn = NewRgn();
  1152.         GetClip(tmprgn);
  1153.         SetClip(two_hemi_rgn);
  1154.         /* Latitude lines. */
  1155.         MoveTo(hemirect1.left,  center1.v);
  1156.         LineTo(hemirect1.right, center1.v);
  1157.         MoveTo(hemirect1.left,  center1.v + ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1158.         LineTo(hemirect1.right, center1.v + ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1159.         MoveTo(hemirect1.left,  center1.v + (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1160.         LineTo(hemirect1.right, center1.v + (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1161.         MoveTo(hemirect1.left,  center1.v - ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1162.         LineTo(hemirect1.right, center1.v - ((hemirect1.bottom - hemirect1.top) / 2) / 2);
  1163.         MoveTo(hemirect1.left,  center1.v - (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1164.         LineTo(hemirect1.right, center1.v - (((hemirect1.bottom - hemirect1.top) / 2) * 173) / 200);
  1165.         MoveTo(hemirect2.left,  center2.v);
  1166.         LineTo(hemirect2.right, center2.v);
  1167.         MoveTo(hemirect2.left,  center2.v + ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1168.         LineTo(hemirect2.right, center2.v + ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1169.         MoveTo(hemirect2.left,  center2.v + (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1170.         LineTo(hemirect2.right, center2.v + (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1171.         MoveTo(hemirect2.left,  center2.v - ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1172.         LineTo(hemirect2.right, center2.v - ((hemirect2.bottom - hemirect2.top) / 2) / 2);
  1173.         MoveTo(hemirect2.left,  center2.v - (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1174.         LineTo(hemirect2.right, center2.v - (((hemirect2.bottom - hemirect2.top) / 2) * 173) / 200);
  1175.         /* Longitude lines. */
  1176.         MoveTo(center1.h, hemirect1.top);
  1177.         LineTo(center1.h, hemirect1.bottom);
  1178.         MoveTo(center2.h, hemirect2.top);
  1179.         LineTo(center2.h, hemirect2.bottom);
  1180.         SetClip(tmprgn);
  1181.         DisposeRgn(tmprgn);
  1182.     }
  1183.     /* should use pair of world disks as clipping region */
  1184.     if ((new_width / new_scale) > (itemrect.right - itemrect.left)) {
  1185.         FillRect(&itemrect, QDPat(dkGray));
  1186.     } else if (new_width == new_circumference) {
  1187.         arearect.top = center.v - (new_height / new_scale) / 2;
  1188.         arearect.left = center.h - (new_width / new_scale) / 2;
  1189.         arearect.bottom = center.v + (new_height / new_scale) / 2;
  1190.         arearect.right = center.h + (new_width / new_scale) / 2;
  1191.         OffsetRect(&arearect, (new_latitude / new_scale), (new_longitude / new_scale));
  1192.         FillRect(&arearect, QDPat(dkGray));
  1193.     } else {
  1194.         /* Assume that area to be drawn is a hexagon. */
  1195.         poly = OpenPoly();        
  1196.         special_xform(0 + new_height/2, 0, &llx, &lly);
  1197.         MoveTo(llx, lly);
  1198.         special_xform(new_width-1, 0, &lrx, &lry);
  1199.          LineTo(lrx, lry);
  1200.         special_xform(new_width-1, new_height/2, &rx, &ry);
  1201.         LineTo(rx, ry);
  1202.          special_xform(new_width-1 - new_height/2, new_height-1, &urx, &ury);
  1203.         LineTo(urx, ury);
  1204.          special_xform(0, new_height-1, &ulx, &uly);
  1205.         LineTo(ulx, uly);
  1206.          special_xform(0, new_height/2, &lx, &ly);
  1207.         LineTo(lx, ly);
  1208.         LineTo(llx, lly);
  1209.         ClosePoly();
  1210.         OffsetPoly(poly, center.h - (new_width / 2) / new_scale, center.v - (new_height / 2) / new_scale);
  1211.         OffsetPoly(poly, (new_latitude / new_scale), (new_longitude / new_scale));
  1212.         FillPoly(poly, QDPat(dkGray));
  1213.     }
  1214. }
  1215.  
  1216. /* This draws an outline shape of the area alone.  If the pen is in inverted mode, then this
  1217.    is a very useful rubberbanding routine. */
  1218.  
  1219. static void
  1220. draw_world_outline(WindowPtr win, short ditem)
  1221. {
  1222.     int llx, lly, lrx, lry, rx, ry, urx, ury, ulx, uly, lx, ly;
  1223.     Point dims, center;
  1224.     Rect worldrect, arearect;
  1225.     PolyHandle poly;
  1226.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1227.  
  1228.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1229.     /* Alter the itemrect so as to leave a bit of border. */
  1230.     InsetRect(&itemrect, 5, 5);
  1231.     dims.h = itemrect.right - itemrect.left;  dims.v = itemrect.bottom - itemrect.top;
  1232.     center.h = itemrect.left + dims.h / 2;  center.v = itemrect.top + dims.v / 2;
  1233.     worldrect = itemrect;
  1234.     new_scale = 1;
  1235.     if (new_circumference > dims.h || new_width > dims.h)
  1236.       new_scale = 2;
  1237.     if (new_circumference > 2 * dims.h || new_width > 2 * dims.h)
  1238.       new_scale = 4;
  1239.     /* should use pair of world disks as clipping region */
  1240.     if ((new_width / new_scale) > (itemrect.right - itemrect.left)) {
  1241.         FrameRect(&itemrect);
  1242.     } else if (new_width == new_circumference) {
  1243.         arearect.top = center.v - (new_height / new_scale) / 2;
  1244.         arearect.left = center.h - (new_width / new_scale) / 2;
  1245.         arearect.bottom = center.v + (new_height / new_scale) / 2;
  1246.         arearect.right = center.h + (new_width / new_scale) / 2;
  1247.         OffsetRect(&arearect, (new_latitude / new_scale), (new_longitude / new_scale));
  1248.         FrameRect(&arearect);
  1249.     } else {
  1250.         /* Assume that area to be drawn is a hexagon. */
  1251.         poly = OpenPoly();        
  1252.         special_xform(0 + new_height/2, 0, &llx, &lly);
  1253.         MoveTo(llx, lly);
  1254.         special_xform(new_width-1, 0, &lrx, &lry);
  1255.          LineTo(lrx, lry);
  1256.         special_xform(new_width-1, new_height/2, &rx, &ry);
  1257.         LineTo(rx, ry);
  1258.          special_xform(new_width-1 - new_height/2, new_height-1, &urx, &ury);
  1259.         LineTo(urx, ury);
  1260.          special_xform(0, new_height-1, &ulx, &uly);
  1261.         LineTo(ulx, uly);
  1262.          special_xform(0, new_height/2, &lx, &ly);
  1263.         LineTo(lx, ly);
  1264.         LineTo(llx, lly);
  1265.         ClosePoly();
  1266.         OffsetPoly(poly, center.h - (new_width / 2) / new_scale, center.v - (new_height / 2) / new_scale);
  1267.         OffsetPoly(poly, (new_latitude / new_scale), (new_longitude / new_scale));
  1268.         FramePoly(poly);
  1269.     }
  1270. }
  1271.  
  1272. /* Given a point in the dialog item, figure out what width and height it corresponds to. */
  1273.  
  1274. static int
  1275. dims_from_point(WindowPtr win, short ditem, Point pt, int *widp, int *hgtp)
  1276. {
  1277.     Point dims, center;
  1278.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1279.  
  1280.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1281.     dims.h = itemrect.right - itemrect.left;  dims.v = itemrect.bottom - itemrect.top;
  1282.     center.h = itemrect.left + dims.h / 2;  center.v = itemrect.top + dims.v / 2;
  1283.     /* Compute width and height so that click was on a corner. */
  1284.     *widp = (2 * abs(pt.h - center.h) + abs(pt.v - center.v)) * new_scale;
  1285.     *hgtp = (2 * abs(pt.v - center.v)) * new_scale;
  1286.     return valid_area_shape(*widp, *hgtp, FALSE);
  1287. }
  1288.  
  1289. /* Grab events that we want to handle specially. */
  1290.  
  1291. static pascal Boolean
  1292. filter_world_setup(DialogPtr dialog, EventRecord *evt, short *itemhit)
  1293. {
  1294.     GrafPtr oldport;
  1295.     Point pt, nextpt;
  1296.     short ditem, ditem2;
  1297.     int wid, hgt;
  1298.     int drawn = FALSE;
  1299.     char ch;
  1300.     Str255 tmpstr;
  1301.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1302.     WindowPtr win = world_shape_win;
  1303.  
  1304.     switch (evt->what) {
  1305.         case mouseDown:
  1306.             GetPort(&oldport);
  1307.             SetPort(dialog);
  1308.             pt = evt->where;
  1309.             GlobalToLocal(&pt);
  1310.             ditem = FindDItem(win, pt) + 1;
  1311.             /* Get out of here if we're not messing with the picture. */
  1312.             if (ditem != diWorldShapePicture) {
  1313.                 SetPort(oldport);
  1314.                 return FALSE;
  1315.             }
  1316.             PenMode(patXor);
  1317.             PenPat(QDPat(gray));
  1318.             if (dims_from_point(win, ditem, pt, &wid, &hgt)) {
  1319.                 new_width = wid;  new_height = hgt;
  1320.                 draw_world_outline(win, ditem);
  1321.                 drawn = TRUE;
  1322.             }
  1323.             while (WaitMouseUp()) {
  1324.                 GetMouse(&nextpt);
  1325.                 if (!EqualPt(pt, nextpt)) {
  1326.                     /* Erase last outline. */
  1327.                     if (drawn) {
  1328.                         draw_world_outline(win, ditem);
  1329.                     }
  1330.                     pt = nextpt;
  1331.                     if (dims_from_point(win, ditem, pt, &wid, &hgt)) {
  1332.                         new_width = wid;  new_height = hgt;
  1333.                         draw_world_outline(win, ditem);
  1334.                         drawn = TRUE;
  1335.                     }
  1336.                 }
  1337.             }
  1338.             if (drawn) {
  1339.                 draw_world_outline(win, ditem);
  1340.             }
  1341.             PenNormal();
  1342.             /* Check if we released the mouse button outside the world picture;
  1343.                if so, treat it as a cancel. */
  1344.             ditem2 = FindDItem(win, pt) + 1;
  1345.             if (ditem == ditem2) {
  1346.                 if (dims_from_point(win, ditem, pt, &wid, &hgt)) {
  1347.                     put_number_into_ditem(diWorldShapeWidth, wid);
  1348.                     put_number_into_ditem(diWorldShapeHeight, hgt);
  1349.                     /* The outer dialog loop will do the redraw. */
  1350.                 }
  1351.                 SetPort(oldport);
  1352.                 *itemhit = diWorldShapePicture;
  1353.                 return TRUE;
  1354.             } else {
  1355.                 SetPort(oldport);
  1356.             }
  1357.             break;
  1358.         case keyDown:
  1359.             ch = evt->message & charCodeMask;
  1360.             /* Recognize returns and enters as equivalent to OK. */
  1361.             if (ch == 3 || ch == 13) {
  1362.                 *itemhit = diWorldShapeOK;
  1363.                 return TRUE;
  1364.             }
  1365.             break;
  1366.     }
  1367.     return FALSE;
  1368. }
  1369.  
  1370. /* This dialog asks for the shape and size of a world to generate randomly. */
  1371.  
  1372. static int
  1373. world_shape_dialog()
  1374. {
  1375.     int done = FALSE, maybechanged = TRUE, changed = FALSE;
  1376.     Str255 tmpstr;
  1377.     short ditem;
  1378.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1379.     WindowPtr win;
  1380.  
  1381.     if (draw_world_picture_proc == NULL)
  1382.       draw_world_picture_proc = NewUserItemProc(draw_world_picture);
  1383.     if (filter_world_setup_proc == NULL)
  1384.       filter_world_setup_proc = NewModalFilterProc(filter_world_setup);
  1385.  
  1386.     if (world_shape_win == nil) {
  1387.         world_shape_win = GetNewDialog(dWorldShape, NULL, (DialogPtr) -1L);
  1388.         GetDItem(world_shape_win, diWorldShapePicture, &itemtype, &itemhandle, &itemrect);
  1389.         SetDItem(world_shape_win, diWorldShapePicture, itemtype, (Handle) draw_world_picture_proc, &itemrect);
  1390.         /* Plug all the starting values into dialog items. */
  1391.         win = world_shape_win;
  1392.         put_number_into_ditem(diWorldShapeCircumference, new_circumference);
  1393.         put_number_into_ditem(diWorldShapeWidth, new_width);
  1394.         put_number_into_ditem(diWorldShapeHeight, new_height);
  1395.         put_number_into_ditem(diWorldShapeLatitude, new_latitude);
  1396.         put_number_into_ditem(diWorldShapeLongitude, new_longitude);
  1397.     }
  1398.     while (!done) {
  1399.         /* adjust items to reflect current status */
  1400.         if (maybechanged) {
  1401.             /* Dig the values out of the dialog boxes. */
  1402.             win = world_shape_win;
  1403.             get_number_from_ditem(diWorldShapeCircumference, new_circumference);
  1404.             get_number_from_ditem(diWorldShapeWidth, new_width);
  1405.             get_number_from_ditem(diWorldShapeHeight, new_height);
  1406.             get_number_from_ditem(diWorldShapeLatitude, new_latitude);
  1407.             get_number_from_ditem(diWorldShapeLongitude, new_longitude);
  1408.             GetDItem(world_shape_win, diWorldShapeIcon, &itemtype, &itemhandle, &itemrect);
  1409.             SetDItem(world_shape_win, diWorldShapeIcon, itemtype,
  1410.                      (new_circumference == new_width ? cylinder_icon : hexagon_icon), &itemrect);
  1411.             DrawDialog(world_shape_win); /* just to do picture item */
  1412.             maybechanged = FALSE;
  1413.         }
  1414.         /* If the proposed shape is valid, enable the OK button. */
  1415.         /* (still need to disable returns etc) */
  1416.         GetDItem(world_shape_win, diWorldShapeOK, &itemtype, &itemhandle, &itemrect);
  1417.         HiliteControl((ControlHandle) itemhandle,
  1418.                       (valid_area_shape(new_width, new_height, FALSE) ? 0 : 255));
  1419.         draw_default_button(world_shape_win, diWorldShapeOK);
  1420.         ModalDialog(filter_world_setup_proc, &ditem);
  1421.         switch (ditem) {
  1422.             case diWorldShapeOK:
  1423.                 changed = TRUE;
  1424.                 /* Fall through to next case. */
  1425.             case diWorldShapeCancel:
  1426.                 done = TRUE;
  1427.                 break;
  1428.             case diWorldShapePicture:
  1429.             case diWorldShapeCircumference:
  1430.             case diWorldShapeWidth:
  1431.             case diWorldShapeHeight:
  1432.             case diWorldShapeLatitude:
  1433.             case diWorldShapeLongitude:
  1434.                 maybechanged = TRUE;
  1435.                 break;
  1436.             case diWorldShapeIcon:
  1437.                 if (new_circumference != new_width)
  1438.                   new_circumference = new_width;
  1439.                 else
  1440.                   new_circumference = 360;
  1441.                 maybechanged = TRUE;
  1442.                 break;
  1443.             default:
  1444.                 break;
  1445.         }
  1446.     }
  1447.     DisposDialog(world_shape_win);
  1448.     world_shape_win = nil;
  1449.     return changed;
  1450. }
  1451.  
  1452. /* This dialog asks for real time parameters of the game. */
  1453.  
  1454. static int
  1455. real_time_dialog()
  1456. {
  1457.     int done = FALSE, maybechanged = TRUE, changed = FALSE;
  1458.     Str255 tmpstr;
  1459.     short ditem;
  1460.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1461.     WindowPtr win;
  1462.  
  1463.     if (real_time_win == nil) {
  1464.         real_time_win = GetNewDialog(dRealTime, NULL, (DialogPtr) -1L);
  1465.         /* Plug all the starting values into dialog items. */
  1466.         win = real_time_win;
  1467.         put_number_into_ditem(diRealTimeForGame, new_time_for_game);
  1468.         put_number_into_ditem(diRealTimePerSide, new_time_per_side);
  1469.         put_number_into_ditem(diRealTimePerTurn, new_time_per_turn);
  1470.     }
  1471.     while (!done) {
  1472.         /* adjust items to reflect current status */
  1473.         if (maybechanged) {
  1474.             /* Dig the values out of the dialog boxes. */
  1475.             win = real_time_win;
  1476.             get_time_from_ditem(diRealTimeForGame, new_time_for_game);
  1477.             get_time_from_ditem(diRealTimePerSide, new_time_per_side);
  1478.             get_time_from_ditem(diRealTimePerTurn, new_time_per_turn);
  1479.             maybechanged = FALSE;
  1480.         }
  1481.         /* If the proposed timings are valid, enable the OK button. */
  1482.         /* (still need to disable returns etc) */
  1483.         GetDItem(real_time_win, diRealTimeOK, &itemtype, &itemhandle, &itemrect);
  1484.         HiliteControl((ControlHandle) itemhandle,
  1485.                       (1 ? 0 : 255));
  1486.         draw_default_button(real_time_win, diRealTimeOK);
  1487.         ModalDialog(NULL, &ditem);
  1488.         switch (ditem) {
  1489.             case diRealTimeOK:
  1490.                 changed = TRUE;
  1491.                 /* Fall through to next case. */
  1492.             case diRealTimeCancel:
  1493.                 done = TRUE;
  1494.                 break;
  1495.             case diRealTimeForGame:
  1496.             case diRealTimePerSide:
  1497.             case diRealTimePerTurn:
  1498.                 maybechanged = TRUE;
  1499.                 break;
  1500.             default:
  1501.                 break;
  1502.         }
  1503.     }
  1504.     DisposDialog(real_time_win);
  1505.     real_time_win = nil;
  1506.     return changed;
  1507. }
  1508.  
  1509. static int
  1510. more_variants_dialog()
  1511. {
  1512.     int done = FALSE, maybechanged = TRUE, changed = FALSE;
  1513.     char *gamename;
  1514.     Str255 tmpstr;
  1515.     short ditem;
  1516.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1517.  
  1518.     if (more_variants_win == nil) {
  1519.         more_variants_win = GetNewDialog(dMoreVariants, NULL, (DialogPtr) -1L);
  1520.         gamename = (selected_game->title ? selected_game->title : selected_game->name);
  1521.         sprintf(spbuf, "More Variants for \"%s\"", gamename);
  1522.         c2p(spbuf, tmpstr);
  1523.         GetDItem(variants_win, diVariantsText, &itemtype, &itemhandle, &itemrect);
  1524.         SetIText(itemhandle, tmpstr);
  1525.     }
  1526.     while (!done) {
  1527.         /* adjust items to reflect current status */
  1528.         if (maybechanged) {
  1529.             /* Dig the values out of the dialog boxes. */
  1530.             maybechanged = FALSE;
  1531.         }
  1532.         draw_default_button(more_variants_win, diMoreVariantsOK);
  1533.         ModalDialog(NULL, &ditem);
  1534.         switch (ditem) {
  1535.             case diMoreVariantsOK:
  1536.                 changed = TRUE;
  1537.                 /* Fall through to next case. */
  1538.             case diMoreVariantsCancel:
  1539.                 done = TRUE;
  1540.                 break;
  1541.             default:
  1542.                 break;
  1543.         }
  1544.     }
  1545.     DisposDialog(more_variants_win);
  1546.     more_variants_win = nil;
  1547.     return changed;
  1548. }
  1549.  
  1550. /* This is a callback that displays the list of sides and their assigned players. */
  1551.  
  1552. static pascal void
  1553. draw_player_setup_list(WindowPtr win, short ditem)
  1554. {
  1555.     int i, advantage = -1, numvisentries;
  1556.     Rect entryrect, tmprect;
  1557.     char *sidetitle, playertitle[BUFSIZE];
  1558.     Player *player;
  1559.     Side *side;
  1560.     RgnHandle tmprgn;
  1561.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1562.  
  1563.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1564.     /* Default to normal size and spacing. */
  1565.     playerh = 22;
  1566.     playerbaseline = 14;
  1567.     numvisentries = (itemrect.bottom - itemrect.top) / playerh;
  1568.     /* If many sides, double our space by squashing things down. */
  1569.     if (numvisentries < numsides) {
  1570.         EraseRect(&itemrect);
  1571.         playerh = 10;
  1572.         playerbaseline = 8;
  1573.         numvisentries = (itemrect.bottom - itemrect.top) / playerh;
  1574.         /* (should switch to smaller font also?) */
  1575.     }
  1576.     tmprgn = NewRgn();
  1577.     GetClip(tmprgn);
  1578.     ClipRect(&itemrect);
  1579.     entryrect = itemrect;
  1580.     entryrect.bottom = entryrect.top + playerh;
  1581.     InsetRect(&entryrect, 1, 1);
  1582.     for (i = 0; i < g_sides_max(); ++i) {
  1583.         if (i >= numvisentries) {
  1584.             /* Not visible, don't draw. */
  1585.         } else if (i < numsides) {
  1586.             PenNormal();
  1587.             /* Clear this entry. */
  1588.             tmprect = entryrect;
  1589.             InsetRect(&tmprect, -1, -1);
  1590.             EraseRect(&tmprect);
  1591.             /* Describe the side. */
  1592.             side = assignments[i].side;
  1593.             draw_side_emblem(playersetupwin, entryrect.left + 2, entryrect.top + 2,
  1594.                              16, 16, side_number(side), shadow_emblem);
  1595.             sidetitle = short_side_title(side);
  1596.             if (empty_string(sidetitle))
  1597.               sidetitle = "(unnamed)";
  1598.             MoveTo(entryrect.left + 2 + 16 + 2, entryrect.top + playerbaseline);
  1599.             TextFont(newYork);
  1600.             TextFace(0);
  1601.             DrawText(sidetitle, 0, strlen(sidetitle));
  1602.             /* Only draw other info for sides "in" this game. */
  1603.             if (side->ingame) {
  1604.                 /* Describe the intended player for the side. */
  1605.                 player = assignments[i].player;
  1606.                 long_player_title(playertitle, player, "mac");
  1607.                 MoveTo(entryrect.left + 2 + 130, entryrect.top + playerbaseline);
  1608.                 TextFace(italic);
  1609.                 DrawText(playertitle, 0, strlen(playertitle));
  1610.                 /* Indicate the player's initial advantage. */
  1611.                 sprintf(spbuf, "%d", player->advantage);
  1612.                 MoveTo(entryrect.left + 2 + 260, entryrect.top + playerbaseline);
  1613.                 TextFace(0);
  1614.                 DrawText(spbuf, 0, strlen(spbuf));
  1615.                 if (player->password) {
  1616.                     MoveTo(entryrect.right - 15, entryrect.top + playerbaseline);
  1617.                     DrawText((strcmp(player->password, "x") == 0 ? "r" : "R"), 0, 1);
  1618.                 }
  1619.             }
  1620.             /* (should be able to draw/update selection separately) */
  1621.             if (i == selectedplayer) {
  1622.                 tmprect = entryrect;
  1623.                 InsetRect(&tmprect, -1, -1);
  1624.                 InvertRect(&tmprect);
  1625.                 InsetRect(&tmprect, 3, 3);
  1626.                 InvertRect(&tmprect);
  1627.             } else {
  1628.                 /* Sides may be present, but not in the game; gray them out. */
  1629.                 if (!side->ingame)
  1630.                   gray_out_rect(&entryrect);
  1631.                 FrameRect(&entryrect);
  1632.             }
  1633.         } else {
  1634.             /* Draw the available positions as gray outlines. */
  1635.             PenPat(QDPat(gray));
  1636.             FrameRect(&entryrect);
  1637.         }
  1638.         OffsetRect(&entryrect, 0, playerh);
  1639.     }
  1640.     PenNormal();
  1641.     SetClip(tmprgn);
  1642.     DisposeRgn(tmprgn);
  1643. }
  1644.  
  1645. /* Callback to draw the little up/down arrow. */
  1646.  
  1647. static pascal void
  1648. draw_player_setup_advantage(WindowPtr win, short ditem)
  1649. {
  1650.     int advantage, halfhgt;
  1651.     Side *side;
  1652.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1653.  
  1654.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  1655.     EraseRect(&itemrect);
  1656.     if (updownpicture != nil) {
  1657.         DrawPicture(updownpicture, &itemrect);
  1658.         /* Never gray out the border, just the arrows inside. */
  1659.         InsetRect(&itemrect, 1, 1);
  1660.         if (!selected_a_player()) {
  1661.             gray_out_rect(&itemrect);
  1662.         } else {
  1663.             advantage = selected_player()->advantage;
  1664.             side = selected_side();
  1665.             halfhgt = (itemrect.bottom - itemrect.top) / 2;
  1666.             if (advantage <= side->minadvantage) {
  1667.                 /* Gray out the down arrow. */
  1668.                 itemrect.top += halfhgt;
  1669.                 gray_out_rect(&itemrect);
  1670.                 itemrect.top -= halfhgt;
  1671.             }
  1672.             if (side->maxadvantage > 0 && advantage >= side->maxadvantage) {
  1673.                 /* Gray out the up arrow. */
  1674.                 itemrect.bottom -= halfhgt;
  1675.                 gray_out_rect(&itemrect);
  1676.                 itemrect.bottom += halfhgt;
  1677.             }
  1678.         }
  1679.     }
  1680. }
  1681.  
  1682. static int
  1683. adjust_advantage(Player *player, Side *side, int amt)
  1684. {
  1685.     int newadvantage;
  1686.  
  1687.     if (player == NULL)
  1688.       return FALSE;
  1689.     newadvantage = player->advantage + amt;
  1690.     if (amt > 0 && side->maxadvantage > 0 && newadvantage > side->maxadvantage) {
  1691.         beep();
  1692.         return FALSE;
  1693.     }
  1694.     if (amt < 0 && newadvantage < side->minadvantage) {
  1695.         beep();
  1696.         return FALSE;
  1697.     }
  1698.     if (newadvantage != player->advantage) {
  1699.         player->advantage = newadvantage;
  1700.         return TRUE;
  1701.     } else
  1702.       return FALSE;
  1703. }
  1704.  
  1705. void
  1706. select_player(int n)
  1707. {
  1708.     if ((!between(0, n, numsides-1))
  1709.         || (assignments[n].side && !(assignments[n].side->ingame))) {
  1710.         n = -1;
  1711.     }
  1712.     selectedplayer = n;
  1713.     /* (update ditem directly?) */
  1714. }
  1715.  
  1716. /* Bring up a dialog that allows rearrangement of the side/player assignments.
  1717.    This is a movable modal because we might have to be here a while waiting
  1718.    for remote players to join in. */
  1719.  
  1720. int
  1721. player_setup_dialog()
  1722. {
  1723.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1724.  
  1725.     if (draw_player_setup_list_proc == NULL)
  1726.       draw_player_setup_list_proc = NewUserItemProc(draw_player_setup_list);
  1727.     if (draw_player_setup_advantage_proc == NULL)
  1728.       draw_player_setup_advantage_proc = NewUserItemProc(draw_player_setup_advantage);
  1729.     /* Create the window. */
  1730.     if (playersetupwin == nil) {
  1731.         playersetupwin = GetNewDialog(dPlayerSetup, NULL, (DialogPtr) -1L);
  1732.         GetDItem(playersetupwin, diPlayerSetupList, &itemtype, &itemhandle, &itemrect);
  1733.         SetDItem(playersetupwin, diPlayerSetupList, itemtype, (Handle) draw_player_setup_list_proc, &itemrect);
  1734.         GetDItem(playersetupwin, diPlayerSetupAdvantage, &itemtype, &itemhandle, &itemrect);
  1735.         SetDItem(playersetupwin, diPlayerSetupAdvantage, itemtype, (Handle) draw_player_setup_advantage_proc, &itemrect);
  1736.         updownpicture = (PicHandle) GetResource('PICT', pUpDownPicture);
  1737.     }
  1738.     playerh = 22;
  1739.     playerbaseline = 14;
  1740.     /* Always start out with the first player selected. */
  1741.     selectedplayer = 0;
  1742.     set_player_setup_button_states();
  1743.     draw_default_button(playersetupwin, diPlayerSetupOK);
  1744.     ShowWindow(playersetupwin);
  1745.     return TRUE;
  1746. }
  1747.  
  1748. int numremotewaiting;
  1749.  
  1750. /* Set the state of the various buttons and controls.  In general, a side/player
  1751.    pair must be selected before anything can be done. */    
  1752.  
  1753. void
  1754. set_player_setup_button_states()
  1755. {
  1756.     int locked = g_player_sides_locked();
  1757.     int player = (selected_a_player() && !locked);
  1758.     int i, numingame = 0;
  1759.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1760.  
  1761.     numremotewaiting = 0;
  1762.     for (i = 0; i < numsides; ++i) {
  1763.         if (assignments[i].side && (assignments[i].side)->ingame) {
  1764.             ++numingame;
  1765.             if ((assignments[i].player)->password) {
  1766.                 if (strcmp((assignments[i].player)->password, "x") == 0)
  1767.                   ++numremotewaiting;
  1768.             }
  1769.         }
  1770.     }
  1771.  
  1772. #define set_player_button(di, val)  \
  1773.     GetDItem(playersetupwin, (di), &itemtype, &itemhandle, &itemrect);  \
  1774.     SetDItem(playersetupwin, (di), ((itemtype & 127) | ((val) ? 0 : 128)), itemhandle, &itemrect);  \
  1775.     HiliteControl((ControlHandle) itemhandle, ((val) ? 0 : 255));
  1776.  
  1777.     set_player_button(diPlayerSetupOK, numremotewaiting == 0);
  1778.     set_player_button(diPlayerSetupAdd, !locked && (numsides < g_sides_max()));
  1779.     set_player_button(diPlayerSetupRemove, player && !locked && (numsides > g_sides_min()));
  1780.     set_player_button(diPlayerSetupRename, player && !(selected_side()->nameslocked));
  1781.     set_player_button(diPlayerSetupAI, player);
  1782.     set_player_button(diPlayerSetupRemote, player);
  1783.     set_player_button(diPlayerSetupExchange, player && numingame > 1);
  1784. }
  1785.  
  1786. /* Handle any event that we don't want DialogSelect to get. */
  1787.  
  1788. void
  1789. handle_player_setup_event(EventRecord *event)
  1790. {
  1791.     char ch;
  1792.     Point mouse;
  1793.  
  1794.     switch (event->what) {
  1795.         case keyDown:
  1796.         case autoKey:
  1797.             ch = event->message & charCodeMask;
  1798.             /* Check for menukey equivalents. */
  1799.             if (event->modifiers & cmdKey) {
  1800.                 if (event->what == keyDown) {
  1801.                     if (ch == '.') {
  1802.                         /* (should) Cancel out of player setup. */
  1803.                         beep();
  1804.                     }
  1805.                 }
  1806.             } else {
  1807.                 if (event->what == keyDown) {
  1808.                     if (ch == 3 || ch == 13) {
  1809.                         /* The mouse position doesn't actually get used here. */
  1810.                         mouse.h = mouse.v = 0;
  1811.                         if (hit_player_setup_dialog(diPlayerSetupOK, mouse))
  1812.                             launch_game_2();
  1813.                     }
  1814.                     /* Shortcuts to tweak the advantage. */
  1815.                     else if (ch == '+') {
  1816.                         adjust_advantage(selected_player(), selected_side(), 1);
  1817.                     } else if (ch == '-') {
  1818.                         adjust_advantage(selected_player(), selected_side(), -1);
  1819.                     }
  1820.                 }
  1821.             }
  1822.             break;
  1823.         case kHighLevelEvent:
  1824.             /* Hear from anyone attempting to join via AppleEvent. */
  1825.             AEProcessAppleEvent(event);
  1826.             break;
  1827.     }
  1828. }
  1829.  
  1830. int
  1831. hit_player_setup_dialog(int ditem, Point pt)
  1832. {
  1833.     int playersok = FALSE, done = FALSE, changedselected = FALSE, changedall = FALSE;
  1834.     int willhost = FALSE;
  1835.     Player *tmpplayer;
  1836.     Side *tmpside;
  1837.     short itemtype;  Handle itemhandle;  Rect itemrect;
  1838.  
  1839.     tmpplayer = selected_player();
  1840.     tmpside = selected_side();
  1841.  
  1842.     switch (ditem) {
  1843.         case diPlayerSetupOK:
  1844.             playersok = TRUE;
  1845.             /* Fall through. */
  1846.         case diPlayerSetupCancel:
  1847.             done = TRUE;
  1848.             break;
  1849.         case diPlayerSetupAdd:
  1850.             add_side_and_player();
  1851.             /* Select the newly-created side/player pair. */
  1852.             select_player(numsides - 1);
  1853.             init_emblem_images(selected_side());
  1854.             changedall = TRUE;
  1855.             break;
  1856.         case diPlayerSetupRemove:
  1857.             if (remove_side_and_player()) {
  1858.                 /* (should fix up) */
  1859.             } else {
  1860.                 beep();
  1861.             }
  1862.             break;
  1863.         case diPlayerSetupRename:
  1864.             tmpside->name = NULL;
  1865.             tmpside->noun = NULL;
  1866.             /* always need to clear this cache before renaming... */
  1867.             tmpside->pluralnoun = NULL;
  1868.             tmpside->adjective = NULL;
  1869.             make_up_side_name(tmpside);
  1870.             init_emblem_images(tmpside);
  1871.             changedselected = TRUE;
  1872.             break;
  1873.         case diPlayerSetupAI:
  1874.             /* Click between mplayer and not, for now */
  1875.             if (tmpplayer->aitypename) {
  1876.                 tmpplayer->aitypename = NULL;
  1877.             } else {
  1878.                 tmpplayer->aitypename = "mplayer";
  1879.             }
  1880.             changedselected = TRUE;
  1881.             break;
  1882.         case diPlayerSetupRemote:
  1883.             /* Toggle whether to wait for human to connect in */
  1884.             /* (should not use password as flag...) */
  1885.             if (tmpplayer->password) {
  1886.                 tmpplayer->password = NULL;
  1887.             } else {
  1888.                 tmpplayer->password = "x";
  1889.                 willhost = TRUE;
  1890.             }
  1891.             changedselected = TRUE;
  1892.             break;
  1893.         case diPlayerSetupExchange:
  1894.             /* Make the next player in the exchange be the selected one,
  1895.                then multiple clicks on the exchange button will move the
  1896.                selected player down through the list. */
  1897.             selectedplayer = exchange_players(selectedplayer, -1);
  1898.             changedall = TRUE;
  1899.             break;
  1900.         case diPlayerSetupList:
  1901.             GetDItem(playersetupwin, diPlayerSetupList, &itemtype, &itemhandle, &itemrect);
  1902.             select_player((pt.v - itemrect.top) / playerh);
  1903.             if (tmpplayer != selected_player())
  1904.               changedselected = TRUE;
  1905.             break;
  1906.         case diPlayerSetupAdvantage:
  1907.             GetDItem(playersetupwin, diPlayerSetupAdvantage, &itemtype, &itemhandle, &itemrect);
  1908.             if (tmpplayer != NULL) {
  1909.                 if (pt.v - itemrect.top < ((itemrect.bottom - itemrect.top) / 2)) {
  1910.                     changedselected = adjust_advantage(tmpplayer, tmpside, 1);
  1911.                 } else {
  1912.                     changedselected = adjust_advantage(tmpplayer, tmpside, -1);
  1913.                 }
  1914.             }
  1915.             break;
  1916.         default:
  1917.             break;
  1918.     }
  1919.     if (!hosting && willhost) {
  1920.         connection_method_dialog();
  1921.         if (connection_method > 0) {
  1922.             hosting = TRUE;
  1923.         }
  1924.     }
  1925.     if (changedall || changedselected) {
  1926.         set_player_setup_button_states();
  1927.         /* (should minimize redrawing) */
  1928.         DrawDialog(playersetupwin);
  1929.         draw_default_button(playersetupwin, diPlayerSetupOK);
  1930.     }
  1931.     if (done) {
  1932.         /* Get rid of this dialog, unlikely to need it again. */
  1933.         DisposDialog(playersetupwin);
  1934.         playersetupwin = nil;
  1935.     }
  1936.     return playersok;
  1937. }
  1938.  
  1939. void download_to_player(Player *player);
  1940.  
  1941. void
  1942. add_remote_player(char *name)
  1943. {
  1944.     int i;
  1945.     Player *player;
  1946.  
  1947.     if (name == NULL)
  1948.       name = "somebody";
  1949.     for (i = 0; i < numsides; ++i) {
  1950.         if (assignments[i].side && (assignments[i].side)->ingame) {
  1951.             player = assignments[i].player;
  1952.             if (player->password && strcmp(player->password, "x") == 0) {
  1953.                 player->password = name;
  1954.                 download_to_player(player);
  1955.                 set_player_setup_button_states();
  1956.                 return;
  1957.             }
  1958.         }
  1959.     }
  1960.     /* No open positions available (should alert?) */
  1961.     beep();
  1962. }
  1963.  
  1964. void send_assignment PARAMS ((int id, Side *side, Player *player));
  1965.  
  1966. void
  1967. send_assignment(id, side, player)
  1968. int id;
  1969. Side *side;
  1970. Player *player;
  1971. {
  1972.     int remoteid;
  1973.  
  1974.     low_send(id, "assign");
  1975.     low_receive(&remoteid, spbuf, BUFSIZE, 0);
  1976. }
  1977.  
  1978. /* (should be a generic routine?) */
  1979. void
  1980. download_to_player(Player *player)
  1981. {
  1982.     int i, remoteid;
  1983.  
  1984.     /* Have the remote program load the game. */
  1985.     low_send(player->id, "load");
  1986.     low_receive(&remoteid, spbuf, BUFSIZE, 0);
  1987.     /* Send it the variant choices. */
  1988.     low_send(player->id, "variants");
  1989.     low_receive(&remoteid, spbuf, BUFSIZE, 0);
  1990.     /* Send it the current list of side/player assignments. */
  1991.     for (i = 0; i < numsides; ++i) {
  1992.         send_assignment(player->id, assignments[i].side, assignments[i].player);
  1993.     }
  1994.     ++numremotes;
  1995. }
  1996.  
  1997. /* This dialog opens a player-specified file as a game. */
  1998.  
  1999. int
  2000. open_game_dialog()
  2001. {
  2002.     Point pnt;
  2003.     SFTypeList typelist;
  2004.     SFReply reply;
  2005.  
  2006.     /* Gotta be somewhere... */
  2007.     SetPt(&pnt, 100, 100);
  2008.     /* Game module are text files. */
  2009.     typelist[0] = 'TEXT';
  2010.     SFGetFile(pnt, "\p", NULL, 1, typelist, NULL, &reply);
  2011.     if (reply.good) {
  2012.         return open_game_from_name_and_volume(reply.fName, reply.vRefNum);
  2013.     }
  2014.     return FALSE;
  2015. }
  2016.  
  2017. /* Open a game file using an FSSpec, which will typically come from an AppleEvent. */
  2018.  
  2019. int
  2020. open_game_from_fsspec(FSSpec *fsspec)
  2021. {
  2022.     short vrefnum;
  2023.  
  2024.     /* (should check the return result) */
  2025.     OpenWD(fsspec->vRefNum, fsspec->parID, XconqSignature, &vrefnum);
  2026.     return open_game_from_name_and_volume(fsspec->name, vrefnum);
  2027. }
  2028.  
  2029. /* Given a filename and volume number, load, check, and launch. */
  2030.  
  2031. int
  2032. open_game_from_name_and_volume(Str255 name, int vrefnum)
  2033. {
  2034.     char modulename[256];
  2035.     Module *module;
  2036.  
  2037.     /* Set the current volume to be where the file is, so path-less
  2038.        fopen() in module loading below will find the file.  Note that this
  2039.        won't work for loading multiple modules from multiple non-library
  2040.        locations, but this dialog can only load one file at a time anyway. */
  2041.     SetVol(nil, vrefnum);
  2042.     clear_game_modules();
  2043.     /* Build a module with the given filename. */
  2044.     module = get_game_module(NULL);
  2045.     p2c(name, modulename);
  2046.     module->filename = copy_string(modulename);
  2047.     module->hook = NULL;
  2048.     /* This module is effectively the "selected game", whether or not
  2049.        it's loadable. */
  2050.     selected_game = module;
  2051.     /* Load the module. */
  2052.     mainmodule = module;
  2053.     load_game_module(module, TRUE);
  2054.     /* Change cursor back, in case it was different during loading. */
  2055.     SetCursor(&QD(arrow));
  2056.     if (!module->loaded) {
  2057.         init_warning("Could not load a module from %s", module->filename);
  2058.         return FALSE;
  2059.     }
  2060.     /* If the loaded module has bugs, we will get alerts here. */
  2061.     check_game_validity();
  2062.     /* We can now join up with the "new game" startup sequence. */
  2063.     return launch_game();
  2064. }
  2065.  
  2066. /* This is just the collection of init steps shared by both new and open dialogs.
  2067.    If the player doesn't cancel out of anything, then the game will be running when
  2068.    this routine returns. */
  2069.  
  2070. int
  2071. launch_game()
  2072. {
  2073.     /* Bring up the pre-player-setup variants. */
  2074.     if (!variants_dialog()) {
  2075.         /* Cancelled from variants dialog, go back to wherever. */
  2076.         return FALSE;
  2077.     }
  2078.     /* Load any colors first; we can use them to substitute for patterns. */
  2079.     init_terrain_colors();
  2080.     /* Set up the images we'll be needing. */
  2081.     init_terrain_images();
  2082.     init_unit_images();
  2083.     /* Trial side/player assignment, will create the default player if nececessary. */
  2084.     make_trial_assignments();
  2085.     /* Get the emblems so they can be displayed in the player dialog. */
  2086.     init_all_emblem_images();
  2087.     /* Ask for modifications to the trial assignment. */
  2088.     return player_setup_dialog();
  2089. }
  2090.  
  2091. void
  2092. launch_game_2()
  2093. {
  2094.     /* Complain if any images were missing.  Will also note if image resource files
  2095.        were never found - might be one of the explanations. */
  2096.     check_for_missing_images();
  2097.     /* A last-minute patch to ensure reasonable area dimensions. */
  2098.     if (area.width == 0 || area.height == 0)
  2099.       set_area_shape(DEFAULTWIDTH, DEFAULTHEIGHT, TRUE);
  2100.     /* About to do lengthy init calcs, show the progress bar. */
  2101.     open_progress_dialog();
  2102.     /* Synthesize the remainder of the initial game setup. */
  2103.     calculate_globals();
  2104.     run_synth_methods();
  2105.     final_init();
  2106.     assign_players_to_sides();
  2107.     /* No more interminable init calcs (we hope), so close the progress bar. */
  2108.     close_progress_dialog();
  2109.     /* Init the display now. */
  2110.     init_display();
  2111.     /* Do first set of turn calcs. */
  2112.     run_game(0);
  2113. }
  2114.  
  2115. /* This counts up all the images that had to be manufactured, and maybe puts up an
  2116.    alert asking if the player wants to go with the substitutes. */
  2117.  
  2118. void
  2119. check_for_missing_images()
  2120. {
  2121.     int u, t, e;
  2122.     Side *side;
  2123.     Str255 tmpstr;
  2124.  
  2125.     for_all_terrain_types(t) {
  2126.         if (timages[t]->ersatz && !t_color_defined(t))
  2127.           record_missing_image(TTYP, t_type_name(t));
  2128.     }
  2129.     for_all_unit_types(u) {
  2130.         if (uimages[u]->ersatz)
  2131.           record_missing_image(UTYP, u_type_name(u));
  2132.     }
  2133.     for_all_sides(side) {
  2134.         e = side_number(side);
  2135.         if (eimages[e]->ersatz)
  2136.           record_missing_image(3, side_desig(side)); 
  2137.     }
  2138.     if (missing_images(tmpbuf)) {
  2139.         c2p(tmpbuf, tmpstr);
  2140.         ParamText(tmpstr, (foundimagesfile ? "\p" : "\p & no image resource files found"), "\p", "\p");
  2141.         switch (CautionAlert(aImagesMissing, nil)) {
  2142.             case 1:
  2143.                 /* We decided to live with low-quality images. */
  2144.                 break;
  2145.             case 2:
  2146.                 /* Ugliness is unbearable, get out now. */
  2147.                 ExitToShell();
  2148.                 break;
  2149.         }
  2150.     }
  2151. }
  2152.  
  2153. /* Rotate around "marching parentheses" cursors to indicate reading is happening. */
  2154.  
  2155. void
  2156. announce_read_progress()
  2157. {
  2158.     if (readprogressors[curcurs] != nil)
  2159.       SetCursor(*(readprogressors[curcurs]));
  2160.     curcurs = (curcurs + 1) % NUMcParens;
  2161. }
  2162.  
  2163. /* Display progress in lengthy initialization processes. */
  2164.  
  2165. /* This is the app-defined item that actually draws the progress bar. */
  2166.  
  2167. static pascal void
  2168. draw_progress(WindowPtr win, short ditem)
  2169. {
  2170.     short itemtype;  Handle itemhandle;  Rect itemrect;
  2171.  
  2172.     GetDItem(win, ditem, &itemtype, &itemhandle, &itemrect);
  2173.     if (progress >= 0) {
  2174.         /* Only draw the gray bg initially or when "progress" decreases (would be a
  2175.            bug in synth method, but useful to get visual evidence if it happens). */
  2176.         if (lastprogress < 0 || lastprogress > progress) {
  2177.             FillRect(&itemrect, QDPat(gray));
  2178.             FrameRect(&itemrect);
  2179.         }
  2180.         itemrect.right =
  2181.             itemrect.left + (progress * (itemrect.right - itemrect.left)) / 100;
  2182.         FillRect(&itemrect, QDPat(black));
  2183.     } else {
  2184.         EraseRect(&itemrect);
  2185.     }
  2186. }
  2187.  
  2188. /* This starts off the lengthy process. */
  2189.  
  2190. void
  2191. open_progress_dialog()
  2192. {
  2193.     short itemtype;  Handle itemhandle;  Rect itemrect;
  2194.  
  2195.     if (draw_progress_proc == NULL)
  2196.       draw_progress_proc = NewUserItemProc(draw_progress);
  2197.     progresswin = GetNewDialog(dProgress, NULL, (DialogPtr) -1L);
  2198.     GetDItem(progresswin, diProgressBar, &itemtype, &itemhandle, &itemrect);
  2199.     SetDItem(progresswin, diProgressBar, itemtype, (Handle) draw_progress_proc, &itemrect);
  2200.     curcurs = 0;
  2201.     if (progressors[curcurs] != nil)
  2202.       SetCursor(*(progressors[curcurs]));
  2203. }
  2204.  
  2205. /* This is called to announce the beginning of a lengthy process. */
  2206.  
  2207. void
  2208. announce_lengthy_process(char *msg)
  2209. {
  2210.     Str255 tmpstr;
  2211.     short itemtype;  Handle itemhandle;  Rect itemrect;
  2212.  
  2213.     if (progresswin == nil)
  2214.       return;
  2215.     /* Paste in the description of what is going to take so long. */
  2216.     GetDItem(progresswin, diProgressText, &itemtype, &itemhandle, &itemrect);
  2217.     c2p(msg, tmpstr);
  2218.     SetIText(itemhandle, tmpstr);
  2219.     /* Initialize the progress bar itself. */
  2220.     progress = -1;
  2221.     lastprogress = 0;
  2222.     ShowWindow(progresswin);
  2223.     SelectWindow(progresswin);
  2224.     Dprintf("%s;", msg);
  2225.     curcurs = 0;
  2226.     if (progressors[curcurs] != nil)
  2227.       SetCursor(*(progressors[curcurs]));
  2228. }
  2229.  
  2230. /* This is called periodically to say how much progress has been made. */
  2231.  
  2232. void
  2233. announce_progress(int pctdone)
  2234. {
  2235.     if (progresswin == nil)
  2236.       return;
  2237.     /* (should notice that stop button was hit and use to escape from program) */
  2238.     SelectWindow(progresswin);
  2239.     lastprogress = progress;
  2240.     progress = min(max(0, pctdone), 100);
  2241.     /* Redraw the progress bar app-defined item, but only if there was
  2242.        any change to report. */
  2243.     /* (should restrict to bar alone, otherwise flickers a lot) */
  2244.     if (progress != lastprogress)
  2245.       DrawDialog(progresswin);
  2246.     /* Switch to the next cursor in the animation sequence. */
  2247.     if (progressors[curcurs] != nil)
  2248.       SetCursor(*(progressors[curcurs]));
  2249.     curcurs = (curcurs + 1) % NUMcSynth;
  2250.     Dprintf(" %d%%", pctdone);
  2251. }
  2252.  
  2253. /* This is called to signal the end of the lengthy process. */
  2254.  
  2255. void
  2256. finish_lengthy_process()
  2257. {
  2258.     short itemtype;  Handle itemhandle;  Rect itemrect;
  2259.  
  2260.     if (progresswin == nil)
  2261.       return;
  2262.     SelectWindow(progresswin);
  2263.     /* Replace the text with a "reassuring" but uninformative message. */
  2264.     GetDItem(progresswin, diProgressText, &itemtype, &itemhandle, &itemrect);
  2265.     SetIText(itemhandle, "\pKeeping busy...");
  2266.     progress = -1;
  2267.     /* Force a redraw, which will cause the progress bar to be erased. */
  2268.     DrawDialog(progresswin);
  2269.     Dprintf(" done.\n");
  2270.     curcurs = 0;
  2271.     if (progressors[curcurs] != nil)
  2272.       SetCursor(*(progressors[curcurs]));
  2273. }
  2274.  
  2275. /* When we're completely done with all lengthy init processes, discard the dialog. */
  2276.  
  2277. void
  2278. close_progress_dialog()
  2279. {
  2280.     /* Restore the normal cursor. */
  2281.     SetCursor(&QD(arrow));
  2282.     DisposDialog(progresswin);
  2283.     progresswin = nil;
  2284. }
  2285.  
  2286. /* Kernel callback to put in a default player, assumed to be on
  2287.    "this" screen (which we call "mac", for lack of inspiration). */
  2288.  
  2289. Player *
  2290. add_default_player()
  2291. {
  2292.     Player *player = add_player();
  2293.     
  2294.     player->displayname = "mac";
  2295.     return player;
  2296. }
  2297.  
  2298. /* This is a kernel callback that sets the given side to have a user
  2299.    interface.  On the Mac, we just record the side as `dside', will
  2300.    finish opening later. */
  2301.  
  2302. void
  2303. init_ui(Side *side)
  2304. {
  2305.     if (side != NULL) {
  2306.         side->ui = (UI *) xmalloc(sizeof(UI));
  2307.         side->ui->active = FALSE;
  2308.         dside = side;
  2309.         /* Make sure this is allocated.  Note that xmalloc clears, so this
  2310.            will look like it has an empty Pascal string. */
  2311.         if (curdatestr == NULL)
  2312.           curdatestr = (char *) xmalloc(100);
  2313.         interp_mac_ui_data(find_at_key(dside->uidata, "mac"));
  2314.     }
  2315. }
  2316.  
  2317. /* Flag the screen as officially "open", init random globals, and open the
  2318.    initial set of windows. */
  2319.  
  2320. void
  2321. init_display()
  2322. {
  2323.     int p;
  2324.     Handle handle;
  2325.  
  2326.     /* If dside never got assigned, try some hasty repairs. */
  2327.     if (dside == NULL) {
  2328.         init_warning("Nobody wanted a display!");
  2329.         /* Make an arbitrary choice - pick the first side. */
  2330.         init_ui(side_n(1));
  2331.         if (dside == NULL) {
  2332.             /* Geez, nothing is going right. */
  2333.             init_error("Can't set up the display!");
  2334.         }
  2335.     }
  2336.     if (bordbitmaps == NULL) {
  2337.         bordbitmaps = (BitMap *) xmalloc(NUMPOWERS * sizeof(BitMap));
  2338.     }
  2339.     if (connbitmaps == NULL) {
  2340.         connbitmaps = (BitMap *) xmalloc(NUMPOWERS * sizeof(BitMap));
  2341.     }
  2342.     /* Init polygon-caching machinery. */
  2343.     for (p = 0; p < NUMPOWERS; ++p) {
  2344.         polygons[p] = nil;
  2345.         lastpolyx[p] = lastpolyy[p] = 0;
  2346.         cellrgns[p] = gridcellrgns[p] = nil;
  2347.         lastcellrgnx[p] = lastcellrgny[p] = 0;
  2348.         lastgridcellrgnx[p] = lastgridcellrgny[p] = 0;
  2349.         cellrgns30[p] = gridcellrgns30[p] = nil;
  2350.         lastcellrgnx30[p] = lastcellrgny30[p] = 0;
  2351.         lastgridcellrgnx30[p] = lastgridcellrgny30[p] = 0;
  2352.         cellrgns15[p] = gridcellrgns15[p] = nil;
  2353.         lastcellrgnx15[p] = lastcellrgny15[p] = 0;
  2354.         lastgridcellrgnx15[p] = lastgridcellrgny15[p] = 0;
  2355.         bordbitmaps[p].rowBytes = 0;
  2356.         connbitmaps[p].rowBytes = 0; 
  2357.     }
  2358.     handle = GetResource('ICN#', 140);
  2359.     if (handle != nil) {
  2360.         HLock(handle);
  2361.         bordbitmaps[4].baseAddr = *handle;
  2362.         bordbitmaps[4].rowBytes = 4;
  2363.         SetRect(&(bordbitmaps[4].bounds), 0, 0, 32, 32);
  2364.     }
  2365.     handle = GetResource('ICN#', 141);
  2366.     if (handle != nil) {
  2367.         HLock(handle);
  2368.         connbitmaps[4].baseAddr = *handle;
  2369.         connbitmaps[4].rowBytes = 4;
  2370.         SetRect(&(connbitmaps[4].bounds), 0, 0, 32, 32);
  2371.     }
  2372.     /* Init assorted list heads and window pointers. */
  2373.     maplist = NULL;
  2374.     listlist = NULL;
  2375.     unitcloseuplist = NULL;
  2376.     historywin = nil;
  2377.     init_ui_chars();
  2378.     /* Flag the interface as ready to display stuff.  After this point,
  2379.        any events causing display activity will actually be rendered
  2380.        on the screen. */
  2381.     dside->ui->active = TRUE;
  2382.     /* Now bring up the initial collection of windows. */
  2383.     if (0 /* should restore preferred windows */) {
  2384.     } else {
  2385.         /* Indicate that this is the first time these windows are being
  2386.            shown; (should) adjust to fit the screen size. */
  2387.         first_windows = TRUE;
  2388.         /* The game window is always useful. */
  2389.         create_game_window();
  2390.         ShowWindow(gamewin);
  2391.         /* The notice window is always useful. */
  2392.         create_notice_window();
  2393.         ShowWindow(noticewin);
  2394.         /* This will be the main playing display, so it should be up. */
  2395.         create_map(5);
  2396.         /* This needs to come up so that players unfamiliar with the game
  2397.            will have some guidance. */
  2398.         instructions_dialog();
  2399.         first_windows = FALSE;
  2400.     }
  2401. #ifdef DESIGNERS
  2402.     /* If we're designing, bring up the palette automatically. */
  2403.     if (dside->designer) {
  2404.         create_design_window();
  2405.     }
  2406. #endif /* DESIGNERS */
  2407.     Dprintf("Opened Mac display!\n");
  2408. }
  2409.  
  2410. /* Allocate and fill in images for all the unit types. */
  2411.  
  2412. void
  2413. init_unit_images()
  2414. {
  2415.     int u;
  2416.  
  2417.     uimages = (ImageFamily **) xmalloc(numutypes * sizeof(ImageFamily *));
  2418.  
  2419.     for_all_unit_types(u) {
  2420.         uimages[u] = get_unit_type_images(dside, u, mac_interp_imf, mac_load_imf,
  2421.                                           add_default_unit_image);
  2422.     }
  2423. }
  2424.  
  2425. /* The default unit image is a box with the name of the unit inside. */
  2426.  
  2427. static void
  2428. add_default_unit_image(ImageFamily *imf, int u, char *name)
  2429. {
  2430.     Rect tmprect;
  2431.     PicHandle pichandle;
  2432.     RgnHandle tmprgn;
  2433.     Image *img;
  2434.     MacImage *macimg;
  2435.  
  2436.     img = get_img(imf, 32, 32);
  2437.     if (img == NULL)
  2438.       return;
  2439.     img->istile = FALSE;
  2440.     macimg = get_mac_image(img);
  2441.     tmprgn = NewRgn();
  2442.     GetClip(tmprgn);
  2443.     SetRect(&tmprect, 0, 0, 32, 32);
  2444.     ClipRect(&tmprect);
  2445.     pichandle = OpenPicture(&tmprect);
  2446.     InsetRect(&tmprect, 2, 2);
  2447.     FillRect(&tmprect, QDPat(white));
  2448.     InsetRect(&tmprect, 1, 1);
  2449.     FrameRect(&tmprect);
  2450.     MoveTo(4, 20);
  2451.     DrawText(name, 0, strlen(name));
  2452.     ClosePicture();
  2453.     SetClip(tmprgn);
  2454.     DisposeRgn(tmprgn);
  2455.     macimg->monopict = pichandle;
  2456. }
  2457.  
  2458. /* Allocate space and load a named color for each terrain type
  2459.    that defines one. */
  2460.  
  2461. void
  2462. init_terrain_colors()
  2463. {
  2464.     int t;
  2465.  
  2466.     if (hasColorQD) {
  2467.         tcolors = (ImageColor **) xmalloc(numttypes * sizeof(ImageColor *));
  2468.         for_all_terrain_types(t) {
  2469.             if (!empty_string(t_color(t))) {
  2470.                 tcolors[t] = new_image_color(t_color(t));
  2471.                 mac_load_image_color(tcolors[t]);
  2472.             }
  2473.         }
  2474.     }
  2475.     interp_named_color(g_grid_color(), &gridgray, &gridcolor);
  2476.     interp_named_color(g_unseen_color(), &unseengray, &unseencolor);
  2477.     /* If the grid color and the unseen terrain color are the same, then we can
  2478.        draw more efficiently. */
  2479.     if (strcmp(g_grid_color(), g_unseen_color()) == 0
  2480.         && empty_string(g_unseen_image_name()))
  2481.       grid_matches_unseen = TRUE;
  2482.     blackcolor.red = blackcolor.green = blackcolor.blue = 0;
  2483. }
  2484.  
  2485. /* Set up the collection of terrain images/patterns, making a default pattern
  2486.    if necessary. */
  2487.  
  2488. void
  2489. init_terrain_images()
  2490. {
  2491.     int t;
  2492.  
  2493.     timages = (ImageFamily **) xmalloc(numttypes * sizeof(ImageFamily *));
  2494.  
  2495.     for_all_terrain_types(t) {
  2496.         timages[t] = get_terrain_type_images(dside, t, mac_interp_imf, mac_load_imf,
  2497.                                              add_default_terrain_image);
  2498.     }
  2499.     get_unseen_images(dside, mac_interp_imf, mac_load_imf, NULL);
  2500. }
  2501.  
  2502. /* Synthesize a terrain pattern, using the type to make a distinguishing
  2503.    pattern. Basically the bits of the type number make a sort of bar code,
  2504.    but enclosed in a box and with a horizontal bar through the middle, so
  2505.    as to make it easier to tell which is which on a map.  (Trust me, I've
  2506.    already experimented with this.) */
  2507.  
  2508. static void
  2509. add_default_terrain_image(ImageFamily *imf, int t, char *name)
  2510. {
  2511.     Image *img;
  2512.     MacImage *macimg;
  2513.  
  2514.     img = get_img(imf, 8, 8);
  2515.     if (img == NULL)
  2516.       return;
  2517.     img->minw = img->minh = 1;
  2518.     img->maxw = img->maxh = 9999;
  2519.     img->istile = TRUE;
  2520.     macimg = get_mac_image(img);
  2521.     SET_IMG_PAT (macimg, 0, 0x01);
  2522.     SET_IMG_PAT (macimg, 1, (t << 2) + 0x01);
  2523.     SET_IMG_PAT (macimg, 2, (t << 2) + 0x01);
  2524.     SET_IMG_PAT (macimg, 3, 0x7d);
  2525.     SET_IMG_PAT (macimg, 4, (t << 2) + 0x01);
  2526.     SET_IMG_PAT (macimg, 5, (t << 2) + 0x01);
  2527.     SET_IMG_PAT (macimg, 6, 0x01);
  2528.     SET_IMG_PAT (macimg, 7, 0xff);
  2529.     macimg->patdefined = 1;
  2530.     /* Count this type as having only a default pattern, but only if we can't
  2531.        possibly render the terrain with a solid color. */
  2532.     if (!(hasColorQD && t_color_defined(t)))
  2533.       imf->ersatz = TRUE;
  2534. }
  2535.  
  2536. /* Given a color name, get actual RGB values for it.  As a special case for b/w
  2537.    screens, the five shades of gray also get set here. */
  2538.  
  2539. void
  2540. interp_named_color(char *name, enum grays *grayvar, RGBColor *colorvar)
  2541. {
  2542.     ImageColor tmpcolor;
  2543.  
  2544.     if (strcmp(name, "black") == 0) {
  2545.         *grayvar = blackgray;
  2546.         colorvar->red = colorvar->green = colorvar->blue = 0;
  2547.     } else if (strcmp(name, "dark gray") == 0) {
  2548.         *grayvar = darkgray;
  2549.         colorvar->red = colorvar->green = colorvar->blue = 16384;
  2550.     } else if (strcmp(name, "gray") == 0) {
  2551.         *grayvar = mediumgray;
  2552.         colorvar->red = colorvar->green = colorvar->blue = 32768;
  2553.     } else if (strcmp(name, "light gray") == 0) {
  2554.         *grayvar = lightgray;
  2555.         colorvar->red = colorvar->green = colorvar->blue = 49000;
  2556.     } else if (strcmp(name, "white") == 0) {
  2557.         *grayvar = whitegray;
  2558.         colorvar->red = colorvar->green = colorvar->blue = 65535;
  2559.     } else if (hasColorQD) {
  2560.         tmpcolor.name = name;
  2561.         mac_load_image_color(&tmpcolor);
  2562.         colorvar->red = tmpcolor.r;
  2563.         colorvar->green = tmpcolor.g;
  2564.         colorvar->blue = tmpcolor.b;
  2565.     }
  2566. }
  2567.  
  2568. /* Load/create all the images of each side's emblem. */
  2569.  
  2570. void
  2571. init_all_emblem_images()
  2572. {
  2573.     Side *side2;
  2574.  
  2575.     eimages = (ImageFamily **) xmalloc((g_sides_max() + 1) * sizeof(ImageFamily *));
  2576.  
  2577.     for_all_sides_plus_indep(side2) {
  2578.         init_emblem_images(side2);
  2579.     }
  2580. }
  2581.  
  2582. /* Set up the emblems for a particular side.  This is used both to do all sides
  2583.    initially and whenever a side changes emblems. */
  2584.  
  2585. void
  2586. init_emblem_images(Side *side2)
  2587. {
  2588.     int e;
  2589.  
  2590.     e = side_number(side2);
  2591.     eimages[e] = get_emblem_images(dside, side2, mac_interp_imf, mac_load_imf,
  2592.                                    add_default_emblem_image);
  2593. }
  2594.  
  2595. /* Default image is SICN-sized number.  Use only if no images have been found
  2596.    and the emblem name is not "none". */
  2597.  
  2598. static void
  2599. add_default_emblem_image(ImageFamily *imf, int e, char *name)
  2600. {
  2601.     int i;
  2602.     char tmpbuf[10];
  2603.     Str255 tmpstr;
  2604.     Handle sicnhandle, maskhandle;
  2605.     Rect tmprect;
  2606.     PicHandle pichandle;
  2607.     RgnHandle tmprgn;
  2608.     Image *img;
  2609.     MacImage *macimg;
  2610.  
  2611.     img = get_img(imf, 16, 16);
  2612.     img->minw = img->minh = 8;
  2613.     img->maxw = img->maxh = 64;
  2614.     /* Emblems never tile. */
  2615.     img->istile = FALSE;
  2616.     macimg = get_mac_image(img);
  2617.     sprintf(tmpbuf, "s%d", e);
  2618.     c2p(name, tmpstr);
  2619.     sicnhandle = GetNamedResource('SICN', tmpstr);
  2620.     if (tmpstr != nil) {
  2621.         /* Found a sicn with the side's number in it. */
  2622.         /* Image itself is the first 32 bytes, mask is second 32 if present. */
  2623.         macimg->monosicn = sicnhandle;
  2624.         maskhandle = (Handle) NewHandle(32);
  2625.         if (SizeResource(sicnhandle) >= 64) {
  2626.             for (i = 0; i < 32; ++i) {
  2627.                 (*maskhandle)[i] = (*sicnhandle)[i+32];
  2628.             }
  2629.         } else {
  2630.             /* Set up an all-white background. */
  2631.             for (i = 0; i < 32; ++i) {
  2632.                 (*maskhandle)[i] = 0xff;
  2633.             }
  2634.         }
  2635.         macimg->masksicn = maskhandle;
  2636.     } else {
  2637.         /* If we couldn't even find a number sicn, build a crude substitute pict. */
  2638.         imf->ersatz = TRUE;
  2639.         tmprgn = NewRgn();
  2640.         GetClip(tmprgn);
  2641.         SetRect(&tmprect, 0, 0, 16, 16);
  2642.         ClipRect(&tmprect);
  2643.         pichandle = OpenPicture(&tmprect);
  2644.         FillRect(&tmprect, QDPat(white));
  2645.         MoveTo(2, 12);
  2646.         DrawText(tmpbuf+1, 0, strlen(tmpbuf+1));
  2647.         ClosePicture();
  2648.         SetClip(tmprgn);
  2649.         DisposeRgn(tmprgn);
  2650.         macimg->monopict = pichandle;
  2651.     }
  2652. }
  2653.