]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/look.c
Fix bitmap overruns when WORLD_X * WORLD_Y not a multiple of 16
[empserver] / src / lib / commands / look.c
index 9ca463676f3fe9221cad167f1224d6defd1d8ff8..fffd99b5d339af8a400be83d7feb23073f242137 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -26,7 +26,7 @@
  *  ---
  *
  *  look.c: Lookout from a ship or land unit
- * 
+ *
  *  Known contributors to this file:
  *     Ron Koenderink, 2006-2007
  */
@@ -55,29 +55,27 @@ llook(void)
 }
 
 int
-do_look(short type)
+do_look(int type)
 {
     int i;
     struct nstr_item ni;
     union empobj_storage unit;
     struct sctstr sect;
     int x, y;
-    int civ;
-    int mil;
     unsigned char *bitmap;
     int changed = 0;
 
     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
        type = EF_SHIP;
 
-    if (!snxtitem(&ni, type, player->argp[1]))
+    if (!snxtitem(&ni, type, player->argp[1], NULL))
        return RET_SYN;
-    if ((bitmap = malloc((WORLD_X * WORLD_Y) / 8)) == 0) {
+    bitmap = calloc((WORLD_SZ() + 7) / 8, 1);
+    if (!bitmap) {
        logerror("malloc failed in do_look\n");
        pr("Memory error.  Tell the deity.\n");
        return RET_FAIL;
     }
-    memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
     while (nxtitem(&ni, &unit)) {
        if (!player->owner)
            continue;
@@ -103,26 +101,9 @@ do_look(short type)
            getsect(x, y, &sect);
            if (sect.sct_type == SCT_WATER)
                continue;
-           if (player->owner)
-               pr("Your ");
-           else
-               pr("%s (#%d) ", cname(sect.sct_own), sect.sct_own);
-           pr("%s", dchr[sect.sct_type].d_name);
+           look_at_sect(&sect, 10);
            changed += map_set(player->cnum, x, y,
                               dchr[sect.sct_type].d_mnem, 0);
-           pr(" %d%% efficient ", player->owner ? sect.sct_effic :
-              roundintby((int)sect.sct_effic, 10));
-           civ = sect.sct_item[I_CIVIL];
-           mil = sect.sct_item[I_MILIT];
-           if (civ)
-               pr("with %s%d civ ",
-                  player->owner ? "" : "approx ",
-                  player->owner ? civ : roundintby(civ, 10));
-           if (mil)
-               pr("with %s%d mil ",
-                  player->owner ? "" : "approx ",
-                  player->owner ? mil : roundintby(mil, 10));
-           pr("@ %s\n", xyas(x, y, player->cnum));
            if (opt_HIDDEN) {
                setcont(player->cnum, sect.sct_own, FOUND_LOOK);
            }
@@ -134,6 +115,31 @@ do_look(short type)
     return RET_OK;
 }
 
+void look_at_sect(struct sctstr *sp, int mult)
+{
+    int civ, mil;
+    int ours = player->god || sp->sct_own == player->cnum;
+
+    if (sp->sct_own == player->cnum)
+       pr("Your ");
+    else
+       pr("%s (#%d) ", cname(sp->sct_own), sp->sct_own);
+    pr("%s", dchr[sp->sct_type].d_name);
+    pr(" %d%% efficient ",
+       ours ? sp->sct_effic : roundintby(sp->sct_effic, mult));
+    civ = sp->sct_item[I_CIVIL];
+    mil = sp->sct_item[I_MILIT];
+    if (civ)
+       pr("with %s%d civ ",
+          ours ? "" : "approx ",
+          ours ? civ : roundintby(civ, mult));
+    if (mil)
+       pr("with %s%d mil ",
+          ours ? "" : "approx ",
+          ours ? mil : roundintby(mil, mult));
+    pr("@ %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
+}
+
 static void
 look_ship(struct shpstr *lookship)
 {
@@ -207,7 +213,7 @@ look_land(struct lndstr *lookland)
     int i;
     int dist;
 
-    drange = techfact(lookland->lnd_tech, lookland->lnd_spy);
+    drange = techfact(lookland->lnd_tech, lchr[lookland->lnd_type].l_spy);
     drange *= lookland->lnd_effic / 100.0;
     range = ldround(drange, 1);