]> git.pond.sub.org Git - empserver/blob - src/lib/commands/spy.c
Use relations_with() in player_relstr()
[empserver] / src / lib / commands / spy.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  *  spy.c: Spy on your neighbors
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1998-2000
33  *     Markus Armbruster, 2005-2008
34  */
35
36 #include <config.h>
37
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 spy(void)
58 {
59     natid own;
60     int 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 BTU's for spying on that scale!\n");
87         return RET_FAIL;
88     }
89     bitmap = calloc(WORLD_SZ() / 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 (#%d) spy deported from %s\n",
142                            cname(player->cnum), player->cnum,
143                            xyas(nx, ny, own));
144                 } else {
145                     /* execute spy */
146                     pr("BANG!! A spy was shot in %s\n",
147                        xyas(nx, ny, player->cnum));
148                     military--;
149                     if (own != 0)
150                         wu(0, own, "%s (#%d) spy caught in %s\n",
151                            cname(player->cnum), player->cnum,
152                            xyas(nx, ny, own));
153                     nreport(player->cnum, N_SPY_SHOT, own, 1);
154                 }
155                 if (opt_HIDDEN)
156                     setcont(own, player->cnum, FOUND_SPY);
157                 if (!nrecon)    /* unless you have a recon unit */
158                     continue;   /* no report from caught spy */
159             }
160
161             /* spy report */
162             emp_setbit(nx, ny, bitmap);
163             spy_report(&dsect);
164             changed += map_set(player->cnum, dsect.sct_x, dsect.sct_y,
165                                dchr[dsect.sct_type].d_mnem, 0);
166             if (opt_HIDDEN)
167                 setcont(player->cnum, own, FOUND_SPY);
168         }
169         /* subtract any military if necessary */
170         if (from.sct_item[I_MILIT] != military) {
171             from.sct_item[I_MILIT] = military;
172             putsect(&from);
173         }
174     }
175     if (changed)
176         writemap(player->cnum);
177     player->btused += btucost;
178     free(bitmap);
179     return RET_OK;
180 }
181
182 static void
183 spy_report(struct sctstr *sp)
184 {
185     prxy("%4d,%-4d", sp->sct_x, sp->sct_y);
186     pr(" %c%c %3d %3d %3d %3d %3d %3d %4d %4d %4d %3d %4d %4d %4d\n",
187        dchr[sp->sct_type].d_mnem,
188        sp->sct_newtype == sp->sct_type ? ' ' : dchr[sp->sct_newtype].d_mnem,
189        sp->sct_own,
190        sp->sct_oldown,
191        roundintby((int)sp->sct_effic, 10),
192        roundintby((int)sp->sct_road, 10),
193        opt_RAILWAYS ? !!sct_rail_track(sp) : roundintby(sp->sct_rail, 10),
194        roundintby((int)sp->sct_defense, 10),
195        roundintby(sp->sct_item[I_CIVIL], 10),
196        roundintby(sp->sct_item[I_MILIT], 10),
197        roundintby(sp->sct_item[I_SHELL], 10),
198        roundintby(sp->sct_item[I_GUN], 10),
199        roundintby(sp->sct_item[I_PETROL], 10),
200        roundintby(sp->sct_item[I_FOOD], 10),
201        roundintby(sp->sct_item[I_BAR], 10));
202     prunits(sp->sct_x, sp->sct_y);
203     prplanes(sp->sct_x, sp->sct_y);
204 }
205
206 static void
207 prunits(int x, int y)
208 {
209     struct lndstr land;
210     struct nstr_item ni;
211     char report[128];
212
213     snxtitem_xy(&ni, EF_LAND, x, y);
214     while (nxtitem(&ni, &land)) {
215         if (land.lnd_own == player->cnum || land.lnd_own == 0)
216             continue;
217         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
218             continue;
219         /* Don't always see spies */
220         if (lchr[(int)land.lnd_type].l_flags & L_SPY) {
221             if (!(chance(LND_SPY_DETECT_CHANCE(land.lnd_effic))))
222                 continue;
223         }
224         sprintf(report, "%s (%s) unit in %s: ",
225                 player_relstr(land.lnd_own),
226                 cname(land.lnd_own),
227                 xyas(land.lnd_x, land.lnd_y, player->cnum));
228         intelligence_report(player->cnum, &land, 3, report);
229     }
230 }
231
232 static void
233 prplanes(int x, int y)
234 {
235     struct plnstr plane;
236     struct nstr_item ni;
237
238     snxtitem_xy(&ni, EF_PLANE, x, y);
239     while (nxtitem(&ni, &plane)) {
240         if (plane.pln_own == player->cnum || plane.pln_own == 0)
241             continue;
242         if (plane.pln_ship >= 0 || plane.pln_land >= 0)
243             continue;
244         if (plane.pln_flags & PLN_LAUNCHED)
245             continue;
246         pr("%s (%s) plane in %s: %s\n",
247            player_relstr(plane.pln_own),
248            cname(plane.pln_own),
249            xyas(plane.pln_x, plane.pln_y, player->cnum),
250            prplane(&plane));
251     }
252 }
253
254 static char *
255 player_relstr(natid them)
256 {
257     int rel = relations_with(player->cnum, them);
258
259     if (rel == ALLIED)
260         return "Allied";
261     if (rel >= NEUTRAL)
262         return "Neutral";
263     return "Enemy";
264 }