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