]> git.pond.sub.org Git - empserver/blob - src/lib/commands/spy.c
client: Unbreak standalone build
[empserver] / src / lib / commands / spy.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  *  spy.c: Spy on your neighbors
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1998-2000
32  *     Markus Armbruster, 2005-2013
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "commands.h"
39 #include "item.h"
40 #include "land.h"
41 #include "map.h"
42 #include "news.h"
43 #include "optlist.h"
44 #include "path.h"
45 #include "plane.h"
46
47 /*
48  * format:  spy <SECTS>
49  */
50
51 static void spy_report(struct sctstr *sp);
52 static void prplanes(int, int);
53 static void prunits(int, int);
54 static char *player_relstr(natid);
55
56 int
57 c_spy(void)
58 {
59     natid own;
60     enum relations relat;
61     coord x, y;
62     coord nx, ny;
63     int military;
64     int btucost;
65     int i;
66     unsigned char *bitmap;
67     int nrecon;
68     struct nstr_sect nstr;
69     struct nstr_item ni;
70     struct natstr *natp;
71     struct sctstr from;
72     struct sctstr dsect;
73     struct lndstr land;
74     int changed = 0;
75     int nsects;
76
77     /*
78      * first arg should be the range of sectors
79      */
80     if (!snxtsct(&nstr, player->argp[1]))
81         return RET_SYN;
82     nsects = (nstr.range.width + 1) * nstr.range.height / 2;
83     btucost = (nsects / 40) + 1;
84     natp = getnatp(player->cnum);
85     if (natp->nat_btu < btucost) {
86         pr("You don't have the BTUs for spying on that scale!\n");
87         return RET_FAIL;
88     }
89     bitmap = calloc((WORLD_SZ() + 7) / 8, 1);
90     if (!bitmap) {
91         logerror("malloc failed in do_look\n");
92         pr("Memory error.  Tell the deity.\n");
93         return RET_FAIL;
94     }
95
96     pr("SPY report\n");
97     prdate();
98     pr("                 old sct rd  rl  def\n");
99     pr("   sect   de own own eff eff eff eff  civ  mil  shl gun  pet food bars\n");
100     while (nxtsct(&nstr, &from)) {
101         if (!player->owner && !player->god)
102             continue;
103         military = from.sct_item[I_MILIT];
104         if (military == 0)
105             continue;
106         x = from.sct_x;
107         y = from.sct_y;
108         nrecon = 0;
109         snxtitem_xy(&ni, EF_LAND, x, y);
110         while (nxtitem(&ni, &land)) {
111             if (lchr[(int)land.lnd_type].l_flags & L_RECON)
112                 nrecon++;
113         }
114         /* Print out the units/planes in this sector */
115         prunits(x, y);
116         prplanes(x, y);
117         /*
118          * check the neighboring sectors.
119          */
120         for (i = 1; i <= 6; i++) {
121             if (military == 0)
122                 break;
123             nx = x + diroff[i][0];
124             ny = y + diroff[i][1];
125             if (emp_getbit(nx, ny, bitmap))
126                 continue;       /* spied already */
127             getsect(nx, ny, &dsect);
128             if (player->owner || dsect.sct_type == SCT_WATER)
129                 continue;
130
131             own = dsect.sct_own;
132             relat = relations_with(own, player->cnum);
133             if (relat <= NEUTRAL
134                 && chance(dsect.sct_item[I_MILIT] / 200.0)) {
135                 /* spy caught */
136                 if (relat == NEUTRAL) {
137                     /* deport spy */
138                     pr("Spy deported from %s\n",
139                        xyas(nx, ny, player->cnum));
140                     if (own != 0)
141                         wu(0, own, "%s spy deported from %s\n",
142                            prnat(natp), xyas(nx, ny, own));
143                 } else {
144                     /* execute spy */
145                     pr("BANG!! A spy was shot in %s\n",
146                        xyas(nx, ny, player->cnum));
147                     military--;
148                     if (own != 0)
149                         wu(0, own, "%s spy caught in %s\n",
150                            prnat(natp), xyas(nx, ny, own));
151                     nreport(player->cnum, N_SPY_SHOT, own, 1);
152                 }
153                 if (opt_HIDDEN)
154                     setcont(own, player->cnum, FOUND_SPY);
155                 if (!nrecon)    /* unless you have a recon unit */
156                     continue;   /* no report from caught spy */
157             }
158
159             /* spy report */
160             emp_setbit(nx, ny, bitmap);
161             spy_report(&dsect);
162             changed += map_set(player->cnum, dsect.sct_x, dsect.sct_y,
163                                dchr[dsect.sct_type].d_mnem, 0);
164             if (opt_HIDDEN)
165                 setcont(player->cnum, own, FOUND_SPY);
166         }
167         /* subtract any military if necessary */
168         if (from.sct_item[I_MILIT] != military) {
169             from.sct_item[I_MILIT] = military;
170             putsect(&from);
171         }
172     }
173     if (changed)
174         writemap(player->cnum);
175     player->btused += btucost;
176     free(bitmap);
177     return RET_OK;
178 }
179
180 static void
181 spy_report(struct sctstr *sp)
182 {
183     prxy("%4d,%-4d", sp->sct_x, sp->sct_y);
184     pr(" %c%c %3d %3d %3d %3d %3d %3d %4d %4d %4d %3d %4d %4d %4d\n",
185        dchr[sp->sct_type].d_mnem,
186        sp->sct_newtype == sp->sct_type ? ' ' : dchr[sp->sct_newtype].d_mnem,
187        sp->sct_own,
188        sp->sct_oldown,
189        roundintby((int)sp->sct_effic, 10),
190        roundintby((int)sp->sct_road, 10),
191        opt_RAILWAYS ? !!sct_rail_track(sp) : roundintby(sp->sct_rail, 10),
192        roundintby((int)sp->sct_defense, 10),
193        roundintby(sp->sct_item[I_CIVIL], 10),
194        roundintby(sp->sct_item[I_MILIT], 10),
195        roundintby(sp->sct_item[I_SHELL], 10),
196        roundintby(sp->sct_item[I_GUN], 10),
197        roundintby(sp->sct_item[I_PETROL], 10),
198        roundintby(sp->sct_item[I_FOOD], 10),
199        roundintby(sp->sct_item[I_BAR], 10));
200     prunits(sp->sct_x, sp->sct_y);
201     prplanes(sp->sct_x, sp->sct_y);
202 }
203
204 static void
205 prunits(int x, int y)
206 {
207     struct lndstr land;
208     struct nstr_item ni;
209     char report[128];
210
211     snxtitem_xy(&ni, EF_LAND, x, y);
212     while (nxtitem(&ni, &land)) {
213         if (land.lnd_own == player->cnum || land.lnd_own == 0)
214             continue;
215         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
216             continue;
217         /* Don't always see spies */
218         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
219             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
220                 continue;
221         }
222         sprintf(report, "%s (%s) unit in %s: ",
223                 player_relstr(land.lnd_own),
224                 cname(land.lnd_own),
225                 xyas(land.lnd_x, land.lnd_y, player->cnum));
226         intelligence_report(player->cnum, &land, 3, report);
227     }
228 }
229
230 static void
231 prplanes(int x, int y)
232 {
233     struct plnstr plane;
234     struct nstr_item ni;
235
236     snxtitem_xy(&ni, EF_PLANE, x, y);
237     while (nxtitem(&ni, &plane)) {
238         if (plane.pln_own == player->cnum || plane.pln_own == 0)
239             continue;
240         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
241             continue;
242         if (plane.pln_flags & PLN_LAUNCHED)
243             continue;
244         pr("%s (%s) plane in %s: %s\n",
245            player_relstr(plane.pln_own),
246            cname(plane.pln_own),
247            xyas(plane.pln_x, plane.pln_y, player->cnum),
248            prplane(&plane));
249     }
250 }
251
252 static char *
253 player_relstr(natid them)
254 {
255     enum relations rel = relations_with(player->cnum, them);
256
257     if (rel == ALLIED)
258         return "Allied";
259     if (rel >= NEUTRAL)
260         return "Neutral";
261     return "Enemy";
262 }