]> git.pond.sub.org Git - empserver/blob - src/lib/commands/look.c
Fix bitmap overruns when WORLD_X * WORLD_Y not a multiple of 16
[empserver] / src / lib / commands / look.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  look.c: Lookout from a ship or land unit
29  *
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2006-2007
32  */
33
34 #include <config.h>
35
36 #include "commands.h"
37 #include "empobj.h"
38 #include "map.h"
39 #include "optlist.h"
40 #include "path.h"
41
42 static void look_ship(struct shpstr *lookship);
43 static void look_land(struct lndstr *lookland);
44
45 int
46 look(void)
47 {
48     return do_look(EF_SHIP);
49 }
50
51 int
52 llook(void)
53 {
54     return do_look(EF_LAND);
55 }
56
57 int
58 do_look(int type)
59 {
60     int i;
61     struct nstr_item ni;
62     union empobj_storage unit;
63     struct sctstr sect;
64     int x, y;
65     unsigned char *bitmap;
66     int changed = 0;
67
68     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
69         type = EF_SHIP;
70
71     if (!snxtitem(&ni, type, player->argp[1], NULL))
72         return RET_SYN;
73     bitmap = calloc((WORLD_SZ() + 7) / 8, 1);
74     if (!bitmap) {
75         logerror("malloc failed in do_look\n");
76         pr("Memory error.  Tell the deity.\n");
77         return RET_FAIL;
78     }
79     while (nxtitem(&ni, &unit)) {
80         if (!player->owner)
81             continue;
82         if (type == EF_LAND) {
83             if (unit.land.lnd_ship >= 0)
84                 continue;
85             if (unit.land.lnd_land >= 0)
86                 continue;
87             /* Spies don't need military to do a "llook".  Other
88                units do */
89             if ((unit.land.lnd_item[I_MILIT] <= 0) &&
90                 !(lchr[(int)unit.land.lnd_type].l_flags & L_SPY))
91                 continue;
92             look_land(&unit.land);
93         } else
94             look_ship(&unit.ship);
95         for (i = 0; i <= 6; i++) {
96             x = diroff[i][0] + unit.gen.x;
97             y = diroff[i][1] + unit.gen.y;
98             if (emp_getbit(x, y, bitmap))
99                 continue;
100             emp_setbit(x, y, bitmap);
101             getsect(x, y, &sect);
102             if (sect.sct_type == SCT_WATER)
103                 continue;
104             look_at_sect(&sect, 10);
105             changed += map_set(player->cnum, x, y,
106                                dchr[sect.sct_type].d_mnem, 0);
107             if (opt_HIDDEN) {
108                 setcont(player->cnum, sect.sct_own, FOUND_LOOK);
109             }
110         }
111     }
112     if (changed)
113         writemap(player->cnum);
114     free(bitmap);
115     return RET_OK;
116 }
117
118 void look_at_sect(struct sctstr *sp, int mult)
119 {
120     int civ, mil;
121     int ours = player->god || sp->sct_own == player->cnum;
122
123     if (sp->sct_own == player->cnum)
124         pr("Your ");
125     else
126         pr("%s (#%d) ", cname(sp->sct_own), sp->sct_own);
127     pr("%s", dchr[sp->sct_type].d_name);
128     pr(" %d%% efficient ",
129        ours ? sp->sct_effic : roundintby(sp->sct_effic, mult));
130     civ = sp->sct_item[I_CIVIL];
131     mil = sp->sct_item[I_MILIT];
132     if (civ)
133         pr("with %s%d civ ",
134            ours ? "" : "approx ",
135            ours ? civ : roundintby(civ, mult));
136     if (mil)
137         pr("with %s%d mil ",
138            ours ? "" : "approx ",
139            ours ? mil : roundintby(mil, mult));
140     pr("@ %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
141 }
142
143 static void
144 look_ship(struct shpstr *lookship)
145 {
146     struct shpstr *sp;
147     struct mchrstr *smcp;
148     struct mchrstr *tmcp;
149     struct sctstr sect;
150     int range;
151     int vrange;
152     int i;
153     int dist;
154
155     range = (int)techfact(lookship->shp_tech,
156                           mchr[(int)lookship->shp_type].m_vrnge);
157     range = range * (lookship->shp_effic / 100.0);
158     smcp = &mchr[(int)lookship->shp_type];
159     if (smcp->m_flags & M_SUB)
160         range = MIN(range, 1);
161     for (i = 0; NULL != (sp = getshipp(i)); i++) {
162         if (sp->shp_own == player->cnum || sp->shp_own == 0)
163             continue;
164         dist = mapdist(sp->shp_x, sp->shp_y,
165                        lookship->shp_x, lookship->shp_y);
166         if (dist > ship_max_interdiction_range)
167             continue;
168         tmcp = &mchr[(int)sp->shp_type];
169         if (smcp->m_flags & M_SUB)
170             vrange = (int)(shp_visib(sp) * range / 30.0);
171         else
172             vrange = (int)(shp_visib(sp) * range / 20.0);
173         getsect(sp->shp_x, sp->shp_y, &sect);
174         if (sect.sct_type != SCT_WATER)
175             vrange = MAX(1, vrange);
176         if (dist > vrange)
177             continue;
178         if (smcp->m_flags & M_SUB) {
179             if (tmcp->m_flags & M_SONAR && dist < 2) {
180                 if (sp->shp_own != 0)
181                     wu(0, sp->shp_own,
182                        "%s detected surfacing noises in %s.\n",
183                        prship(sp),
184                        xyas(lookship->shp_x, lookship->shp_y,
185                             sp->shp_own));
186             }
187             if (dist == 0 && (tmcp->m_flags & M_SUB) == 0)
188                 if (sp->shp_own != 0)
189                     wu(0, sp->shp_own,
190                        "Periscope spotted in %s by %s\n",
191                        xyas(lookship->shp_x, lookship->shp_y,
192                             sp->shp_own), prship(sp));
193         }
194         /* subs at sea only seen by sonar */
195         if (tmcp->m_flags & M_SUB && sect.sct_type == SCT_WATER)
196             continue;
197         pr("%s (#%d) %s @ %s\n",
198            cname(sp->shp_own), sp->shp_own, prship(sp),
199            xyas(sp->shp_x, sp->shp_y, player->cnum));
200         if (opt_HIDDEN)
201             setcont(player->cnum, sp->shp_own, FOUND_LOOK);
202     }
203 }
204
205 static void
206 look_land(struct lndstr *lookland)
207 {
208     struct plnstr *pp;
209     struct lndstr *lp;
210     double drange;
211     int range;
212     int vrange;
213     int i;
214     int dist;
215
216     drange = techfact(lookland->lnd_tech, lchr[lookland->lnd_type].l_spy);
217     drange *= lookland->lnd_effic / 100.0;
218     range = ldround(drange, 1);
219
220     if (range == 0)
221         return;
222
223     for (i = 0; NULL != (lp = getlandp(i)); i++) {
224         if (lp->lnd_own == player->cnum || lp->lnd_own == 0)
225             continue;
226         if (lp->lnd_ship >= 0)
227             continue;
228         /* Don't always see spies */
229         if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
230             /* If it's on a ship or unit, assume it's hidden
231                enough not to be seen */
232             if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
233                 continue;
234             if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
235                 continue;
236         }
237         vrange = ldround((lnd_vis(lp) * range) / 20.0, 1);
238         dist = mapdist(lp->lnd_x, lp->lnd_y,
239                        lookland->lnd_x, lookland->lnd_y);
240         if (dist > vrange)
241             continue;
242
243         pr("%s (#%d) %s (approx %d mil) @ %s\n",
244            cname(lp->lnd_own), lp->lnd_own,
245            prland(lp), roundintby(lp->lnd_item[I_MILIT], 20),
246            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
247         if (opt_HIDDEN)
248             setcont(player->cnum, lp->lnd_own, FOUND_LOOK);
249     }
250     for (i = 0; NULL != (pp = getplanep(i)); i++) {
251         if (pp->pln_own == player->cnum || pp->pln_own == 0)
252             continue;
253         if (pp->pln_ship >= 0)
254             continue;
255         if (pp->pln_flags & PLN_LAUNCHED)
256             continue;
257         vrange = ldround((10 * range) / 20.0, 1);
258         dist = mapdist(pp->pln_x, pp->pln_y,
259                        lookland->lnd_x, lookland->lnd_y);
260         if (dist > vrange)
261             continue;
262
263         pr("%s (#%d) %s @ %s\n",
264            cname(pp->pln_own), pp->pln_own,
265            prplane(pp), xyas(pp->pln_x, pp->pln_y, player->cnum));
266         if (opt_HIDDEN)
267             setcont(player->cnum, pp->pln_own, FOUND_LOOK);
268     }
269 }