]> git.pond.sub.org Git - empserver/blob - src/lib/commands/look.c
commands: Rename the command functions
[empserver] / src / lib / commands / look.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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  *     Markus Armbruster, 2004-2016
32  */
33
34 #include <config.h>
35
36 #include "chance.h"
37 #include "commands.h"
38 #include "empobj.h"
39 #include "map.h"
40 #include "optlist.h"
41 #include "path.h"
42
43 static void look_ship(struct shpstr *lookship);
44 static void look_land(struct lndstr *lookland);
45
46 int
47 c_lookout(void)
48 {
49     return do_look(EF_SHIP);
50 }
51
52 int
53 c_llookout(void)
54 {
55     return do_look(EF_LAND);
56 }
57
58 int
59 do_look(int type)
60 {
61     int i;
62     struct nstr_item ni;
63     union empobj_storage unit;
64     struct sctstr sect;
65     int x, y;
66     unsigned char *bitmap;
67     int changed = 0;
68
69     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
70         type = EF_SHIP;
71
72     if (!snxtitem(&ni, type, player->argp[1], NULL))
73         return RET_SYN;
74     bitmap = calloc((WORLD_SZ() + 7) / 8, 1);
75     if (!bitmap) {
76         logerror("malloc failed in do_look\n");
77         pr("Memory error.  Tell the deity.\n");
78         return RET_FAIL;
79     }
80     while (nxtitem(&ni, &unit)) {
81         if (!player->owner)
82             continue;
83         if (type == EF_LAND) {
84             if (unit.land.lnd_ship >= 0)
85                 continue;
86             if (unit.land.lnd_land >= 0)
87                 continue;
88             /* Spies don't need military to do a "llook".  Other
89                units do */
90             if ((unit.land.lnd_item[I_MILIT] <= 0) &&
91                 !(lchr[(int)unit.land.lnd_type].l_flags & L_SPY))
92                 continue;
93             look_land(&unit.land);
94         } else
95             look_ship(&unit.ship);
96         for (i = 0; i <= 6; i++) {
97             x = diroff[i][0] + unit.gen.x;
98             y = diroff[i][1] + unit.gen.y;
99             if (emp_getbit(x, y, bitmap))
100                 continue;
101             emp_setbit(x, y, bitmap);
102             getsect(x, y, &sect);
103             if (sect.sct_type == SCT_WATER)
104                 continue;
105             look_at_sect(&sect, 10);
106             changed += map_set(player->cnum, x, y,
107                                dchr[sect.sct_type].d_mnem, 0);
108             if (opt_HIDDEN) {
109                 setcont(player->cnum, sect.sct_own, FOUND_LOOK);
110             }
111         }
112     }
113     if (changed)
114         writemap(player->cnum);
115     free(bitmap);
116     return RET_OK;
117 }
118
119 void look_at_sect(struct sctstr *sp, int mult)
120 {
121     int civ, mil;
122     int ours = player->god || sp->sct_own == player->cnum;
123
124     pr("%s %s",
125        sp->sct_own == player->cnum ? "Your" : prnatid(sp->sct_own),
126        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 %s @ %s\n",
197            prnatid(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 (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
230                 continue;
231         }
232         vrange = ldround((lnd_vis(lp) * range) / 20.0, 1);
233         dist = mapdist(lp->lnd_x, lp->lnd_y,
234                        lookland->lnd_x, lookland->lnd_y);
235         if (dist > vrange)
236             continue;
237
238         pr("%s %s (approx %d mil) @ %s\n",
239            prnatid(lp->lnd_own), prland(lp),
240            roundintby(lp->lnd_item[I_MILIT], 20),
241            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
242         if (opt_HIDDEN)
243             setcont(player->cnum, lp->lnd_own, FOUND_LOOK);
244     }
245     for (i = 0; NULL != (pp = getplanep(i)); i++) {
246         if (pp->pln_own == player->cnum || pp->pln_own == 0)
247             continue;
248         if (pp->pln_ship >= 0 || pp->pln_land >= 0)
249             continue;
250         if (pp->pln_flags & PLN_LAUNCHED)
251             continue;
252         vrange = ldround((10 * range) / 20.0, 1);
253         dist = mapdist(pp->pln_x, pp->pln_y,
254                        lookland->lnd_x, lookland->lnd_y);
255         if (dist > vrange)
256             continue;
257
258         pr("%s %s @ %s\n",
259            prnatid(pp->pln_own), prplane(pp),
260            xyas(pp->pln_x, pp->pln_y, player->cnum));
261         if (opt_HIDDEN)
262             setcont(player->cnum, pp->pln_own, FOUND_LOOK);
263     }
264 }