home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume4 / xconq5 / part15 / ginit.c < prev    next >
C/C++ Source or Header  |  1988-07-01  |  9KB  |  266 lines

  1. /* Copyright (c) 1987, 1988  Stanley T. Shebs, University of Utah. */
  2. /* This program may be used, copied, modified, and redistributed freely */
  3. /* for noncommercial purposes, so long as this notice remains intact. */
  4.  
  5. /* RCS $Header: ginit.c,v 1.1 88/06/21 12:30:10 shebs Exp $ */
  6.  
  7. /* Initialization and random routines for the display part of xconq. */
  8.  
  9. #include "config.h"
  10. #include "misc.h"
  11. #include "dir.h"
  12. #include "period.h"
  13. #include "side.h"
  14. #include "unit.h"
  15. #include "map.h"
  16. #include "global.h"
  17.  
  18. /* Always use four lines for the unit info display, even on small screens. */
  19.  
  20. #define INFOLINES 4
  21.  
  22. int maxnamelen = 0;        /* length of longest side name+host */
  23.  
  24. /* Find a unit and put it at center (or close to it) of first view.  I guess */
  25. /* can't use put_on_screen because not enough stuff is inited yet. */
  26.  
  27. init_curxy(side)
  28. Side *side;
  29. {
  30.     Unit *unit;
  31.  
  32.     for_all_units(unit) {
  33.     if (unit->side == side) {
  34.         side->cx = unit->x;  side->cy = unit->y;
  35.         side->vcx = unit->x;
  36.         side->vcy = max(unit->y, side->vw2+1);
  37.         side->vcy = min(side->vcy, (world.height-1) - side->vh2);
  38.         return;
  39.     }
  40.     }
  41.     side->vcx = side->vw2;  side->vcy = side->vh2;
  42.     side->cx = side->vcx;  side->cy = side->vcy;
  43. }
  44.  
  45. /* The very first step in using X is to open up all the desired displays. */
  46. /* In our case, there are many displays each with many windows.  If the */
  47. /* display opens successfully, do an initial "redraw" to set the screen up. */
  48.  
  49. init_displays()
  50. {
  51.     int len;
  52.     Side *side;
  53.  
  54.     for_all_sides(side) {
  55.     len = 8 + strlen(side->name) + 1;
  56.     maxnamelen = max(maxnamelen,
  57.              len + (side->host ? strlen(side->host) + 3 : 0));
  58.     }
  59.     for_all_sides(side) {
  60.     if (side->host != NULL) {
  61.         init_display(side);
  62.         if (active_display(side)) {
  63.         init_curxy(side);
  64.         redraw(side);
  65.         }
  66.     }
  67.     }
  68. }
  69.  
  70. /* Open display, create all the windows we'll need, do misc setup things, */
  71. /* and initialize some globals to out-of-range values for recognition later. */
  72.  
  73. init_display(side)
  74. Side *side;
  75. {
  76.     int i;
  77.  
  78.     if (Debug) printf("Will try to open display \"%s\" ...\n", side->host);
  79.  
  80.     if (!open_display(side)) {
  81.     fprintf(stderr, "Display \"%s\" could not be opened!\n", side->host);
  82.     exit_xconq();
  83.     }
  84.     active_display(side);   /* done for side effect */
  85.     init_colors(side);
  86.     init_misc(side);
  87.     init_sizes(side);
  88.     create_main_window(side);
  89.     side->msg =
  90.     create_window(side, 0, 0,
  91.               side->lw, side->nh * side->fh);
  92.     side->info =
  93.     create_window(side, 0, side->nh * side->fh + side->bd,
  94.               side->lw, INFOLINES * side->fh);
  95.     side->prompt =
  96.     create_window(side, 0, (side->nh + INFOLINES) * side->fh + 2*side->bd,
  97.               side->lw, side->fh);
  98.     side->map =
  99.     create_window(side, 0, side->th,
  100.               side->vw * side->hw,
  101.               side->vh * side->hch + (side->hh - side->hch));
  102.     side->sides =
  103.     create_window(side, side->lw + side->bd + 1, 0,
  104.               side->sw * side->fw, numsides * side->fh);
  105.     side->timemode =
  106.     create_window(side, side->lw + side->fw, side->th - 4 * side->fh,
  107.               2 * side->margin + 8 * side->fw, 2 * side->fh);
  108.     side->clock =
  109.     create_window(side, side->lw + 13 * side->fw, side->th - 2 * side->fh,
  110.               8 * side->fw, side->fh);
  111.     side->state =
  112.     create_window(side, side->lw + 1, side->th,
  113.               20 * side->fw, period.numutypes*max(side->hh, side->fh));
  114.     if (world_display(side)) {
  115.     side->world =
  116.         create_window(side,
  117.               side->lw+side->bd, side->mh-side->mm*world.height,
  118.               world.width * side->mm, world.height * side->mm);
  119.     }
  120.     create_help_window(side);
  121.     fixup_windows(side);
  122.     enable_input(side);
  123.     for (i = 0; i < MAXNOTES; ++i) strcpy(side->noticebuf[i], " ");
  124.     for (i = 0; i < MAXUTYPES; ++i) side->bvec[i] = 0;
  125.     /* Flag some values as uninitialized */
  126.     side->vcx = side->vcy = -1;
  127.     side->lastvcx = side->lastvcy = -1;
  128.     side->lastx = side->lasty = -1;
  129.     if (Debug) printf("Successfully opened \"%s\"!\n", side->host);
  130. }
  131.  
  132. /* Decide/compute all the sizes of things.  Our main problem here is that */
  133. /* the display might be smaller than we really desire, so things have to */
  134. /* be adjusted to fit. */
  135.  
  136. init_sizes(side)
  137. Side *side;
  138. {
  139.     int alw, abh;
  140.  
  141.     alw = (3 * display_width(side)) / 4;
  142.     side->vw = min(world.width, alw / side->hw);
  143.     side->nw = min(BUFSIZE, alw / side->fw);
  144.     side->sw = maxnamelen;
  145.     abh = (2 * display_height(side)) / 3;
  146.     side->vh = min(world.height, abh / side->hch);
  147.     side->nh = max(1, min(abh/2 - 5, MAXNOTES));
  148.     side->mm = min(5, (max(world.width, side->fw*side->sw) / world.width));
  149.     set_sizes(side);
  150.     if (side->vw < MINWIDTH || side->vh < MINHEIGHT) {
  151.     fprintf(stderr, "Display \"%s\" is too small!\n", side->host);
  152.     exit_xconq();
  153.     }
  154. }
  155.  
  156. /* This fn is a "ten-line horror"; effectively a mini tiled window manager */
  157. /* that keeps the subwindows from overlapping no matter what the display and */
  158. /* requested sizes are.  Siemens eat your heart out... */
  159.  
  160. set_sizes(side)
  161. Side *side;
  162. {
  163.     int ulhgt, llhgt, urhgt, lrhgt;
  164.  
  165.     /* Make sure map window dimensions are OK */
  166.     side->vw = min(world.width, side->vw);
  167.     side->vh = min(world.height, side->vh);
  168.     side->vw2 = side->vw / 2;  side->vh2 = side->vh / 2;
  169.     /* Compute subregion sizes (left/right upper/lower width/height) */
  170.     side->lw = max(side->nw * side->fw, side->hw * side->vw);
  171.     side->rw = max((world_display(side) ? world.width * side->mm : 0),
  172.            side->sw * side->fw);
  173.     urhgt = (numsides + 4) * side->fh + side->bd;
  174.     ulhgt = side->fh * (side->nh + INFOLINES + 1) + 3 * side->bd;
  175.     side->th = max(ulhgt, urhgt);
  176.     lrhgt = period.numutypes * max(side->hh, side->fh);
  177.     if (world_display(side)) lrhgt += side->mm * world.height + side->bd;
  178.     llhgt = side->hch * side->vh + (side->hh - side->hch);
  179.     side->bh = max(llhgt, lrhgt);
  180.     side->mw = side->lw + side->bd + side->rw;
  181.     side->mh = side->th + side->bd + side->bh;
  182.     /* Only vcy needs adjustment, since vcx can never be close to an edge */
  183.     side->vcy = min(max(side->vh2, side->vcy), (world.height-1)-side->vh2);
  184. }
  185.  
  186. /* Acquire a set of colors.  There are too many to make it feasible to */
  187. /* customize them via .Xdefaults, so we don't even try.  If there aren't */
  188. /* enough colors, drop into monochrome mode.  This doesn't take the window */
  189. /* manager into account - it may have grabbed some color space. */
  190.  
  191. init_colors(side)
  192. Side *side;
  193. {
  194.     if (Debug) printf("%d colors available ...\n", display_colors(side));
  195.     side->monochrome = (display_colors(side) == 2);
  196.     side->bonw = (side->monochrome ? BLACKONWHITE : FALSE);
  197.     set_colors(side);
  198. }
  199.  
  200. /* This will set up the correct set of colors at any point in the game. */
  201. /* Note that terrain colors get NO value if in monochrome mode. */
  202. /* If the colors requested are not available, it is up to the graphics */
  203. /* interface to supply a substitute. */
  204.  
  205. set_colors(side)
  206. Side *side;
  207. {
  208.     int    t;
  209.     long fg, bg;
  210.  
  211.     fg = (side->bonw ? black_color(side) : white_color(side));
  212.     bg = (side->bonw ? white_color(side) : black_color(side));
  213.     side->bgcolor = side->owncolor = side->altcolor = side->diffcolor = bg;
  214.     side->fgcolor = side->bdcolor = side->graycolor = side->enemycolor = fg;
  215.     side->neutcolor = side->goodcolor = side->badcolor = fg;
  216.     if (!side->monochrome) {
  217.     for_all_terrain_types(t) {
  218.         side->hexcolor[t] = request_color(side, ttypes[t].color);
  219.     }
  220.     side->owncolor = request_color(side, "black");
  221.     side->altcolor = request_color(side, "blue");
  222.     side->diffcolor = request_color(side, "maroon");
  223.     side->bdcolor = request_color(side, "blue");
  224.     side->graycolor = request_color(side, "light gray");
  225.     side->enemycolor = request_color(side, "red");
  226.     side->neutcolor = request_color(side, "light gray");
  227.     side->goodcolor = request_color(side, "green");
  228.     side->badcolor = request_color(side, "red");
  229.     }
  230. }
  231.  
  232. /* Move windows and change their sizes to correspond with the new sizes of */
  233. /* viewports, etc. */
  234.  
  235. reconfigure_display(side)
  236. Side *side;
  237. {
  238.     int sy, sdy;
  239.  
  240.     if (active_display(side)) {
  241.     set_sizes(side);
  242.     set_colors(side);
  243.     reset_misc(side);
  244.     sy = 0;  sdy = side->nh * side->fh;
  245.     change_window(side, side->msg, 0, sy, side->lw, sdy);
  246.     sy += sdy + side->bd;  sdy = INFOLINES * side->fh;
  247.     change_window(side, side->info, 0, sy, side->lw, sdy);
  248.     sy += sdy + side->bd;  sdy = 1 * side->fh;
  249.     change_window(side, side->prompt, 0, sy, side->lw, sdy);
  250.     change_window(side, side->map, 0, side->th,
  251.               side->vw * side->hw,
  252.               side->vh * side->hch + (side->hh - side->hch));
  253.     change_window(side, side->timemode,
  254.               side->lw + side->fw, side->th - 4 * side->fh, -1, -1);
  255.     change_window(side, side->sides, side->lw+1, 0, -1, -1);
  256.     if (world_display(side)) {
  257.         change_window(side, side->world,
  258.               side->lw + 1, side->mh - world.height * side->mm,
  259.               world.width * side->mm, world.height * side->mm);
  260.     }
  261.     change_window(side, side->main, -1, -1, side->mw, side->mh);
  262.     redraw(side);
  263.     }
  264. }
  265.  
  266.