]> git.pond.sub.org Git - empserver/blob - src/lib/commands/look.c
96a3bf4d71496d60516aac0dedf77ab0ffde1b76
[empserver] / src / lib / commands / look.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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(short type)
59 {
60     int i;
61     struct nstr_item ni;
62     union empobj_storage unit;
63     struct sctstr sect;
64     int x, y;
65     int civ;
66     int mil;
67     unsigned char *bitmap;
68     int changed = 0;
69
70     if (!snxtitem(&ni, type, player->argp[1]))
71         return RET_SYN;
72     if ((bitmap = malloc((WORLD_X * WORLD_Y) / 8)) == 0) {
73         logerror("malloc failed in do_look\n");
74         pr("Memory error.  Tell the deity.\n");
75         return RET_FAIL;
76     }
77     memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
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             if (player->owner)
104                 pr("Your ");
105             else
106                 pr("%s (#%d) ", cname(sect.sct_own), sect.sct_own);
107             pr("%s", dchr[sect.sct_type].d_name);
108             changed += map_set(player->cnum, x, y,
109                                dchr[sect.sct_type].d_mnem, 0);
110             pr(" %d%% efficient ", player->owner ? sect.sct_effic :
111                roundintby((int)sect.sct_effic, 10));
112             civ = sect.sct_item[I_CIVIL];
113             mil = sect.sct_item[I_MILIT];
114             if (civ)
115                 pr("with %s%d civ ",
116                    player->owner ? "" : "approx ",
117                    player->owner ? civ : roundintby(civ, 10));
118             if (mil)
119                 pr("with %s%d mil ",
120                    player->owner ? "" : "approx ",
121                    player->owner ? mil : roundintby(mil, 10));
122             pr("@ %s\n", xyas(x, y, player->cnum));
123             if (opt_HIDDEN) {
124                 setcont(player->cnum, sect.sct_own, FOUND_LOOK);
125             }
126         }
127     }
128     if (changed)
129         writemap(player->cnum);
130     free(bitmap);
131     return RET_OK;
132 }
133
134 static void
135 look_ship(struct shpstr *lookship)
136 {
137     struct shpstr *sp;
138     struct mchrstr *smcp;
139     struct mchrstr *tmcp;
140     struct sctstr sect;
141     int range;
142     int vrange;
143     int i;
144     int dist;
145
146     range = (int)techfact(lookship->shp_tech,
147                           mchr[(int)lookship->shp_type].m_vrnge);
148     range = range * (lookship->shp_effic / 100.0);
149     smcp = &mchr[(int)lookship->shp_type];
150     if (smcp->m_flags & M_SUB)
151         range = MIN(range, 1);
152     for (i = 0; NULL != (sp = getshipp(i)); i++) {
153         if (sp->shp_own == player->cnum || sp->shp_own == 0)
154             continue;
155         dist = mapdist(sp->shp_x, sp->shp_y,
156                        lookship->shp_x, lookship->shp_y);
157         if (dist > ship_max_interdiction_range)
158             continue;
159         tmcp = &mchr[(int)sp->shp_type];
160         if (smcp->m_flags & M_SUB)
161             vrange = (int)(sp->shp_visib * range / 30.0);
162         else
163             vrange = (int)(sp->shp_visib * range / 20.0);
164         getsect(sp->shp_x, sp->shp_y, &sect);
165         if (sect.sct_type != SCT_WATER)
166             vrange = MAX(1, vrange);
167         if (dist > vrange)
168             continue;
169         if (smcp->m_flags & M_SUB) {
170             if (tmcp->m_flags & M_SONAR && dist < 2) {
171                 if (sp->shp_own != 0)
172                     wu(0, sp->shp_own,
173                        "%s detected surfacing noises in %s.\n",
174                        prship(sp),
175                        xyas(lookship->shp_x, lookship->shp_y,
176                             sp->shp_own));
177             }
178             if (dist == 0 && (tmcp->m_flags & M_SUB) == 0)
179                 if (sp->shp_own != 0)
180                     wu(0, sp->shp_own,
181                        "Periscope spotted in %s by %s\n",
182                        xyas(lookship->shp_x, lookship->shp_y,
183                             sp->shp_own), prship(sp));
184         }
185         /* subs at sea only seen by sonar */
186         if (tmcp->m_flags & M_SUB && sect.sct_type == SCT_WATER)
187             continue;
188         pr("%s (#%d) %s @ %s\n",
189            cname(sp->shp_own), sp->shp_own, prship(sp),
190            xyas(sp->shp_x, sp->shp_y, player->cnum));
191         if (opt_HIDDEN)
192             setcont(player->cnum, sp->shp_own, FOUND_LOOK);
193     }
194 }
195
196 static void
197 look_land(struct lndstr *lookland)
198 {
199     struct plnstr *pp;
200     struct lndstr *lp;
201     double drange;
202     int range;
203     int vrange;
204     int i;
205     int dist;
206
207     drange = techfact(lookland->lnd_tech, lookland->lnd_spy);
208     drange *= lookland->lnd_effic / 100.0;
209     range = ldround(drange, 1);
210
211     if (range == 0)
212         return;
213
214     for (i = 0; NULL != (lp = getlandp(i)); i++) {
215         if (lp->lnd_own == player->cnum || lp->lnd_own == 0)
216             continue;
217         if (lp->lnd_ship >= 0)
218             continue;
219         /* Don't always see spies */
220         if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
221             /* If it's on a ship or unit, assume it's hidden
222                enough not to be seen */
223             if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
224                 continue;
225             if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
226                 continue;
227         }
228         vrange = ldround((lp->lnd_vis * range) / 20.0, 1);
229         dist = mapdist(lp->lnd_x, lp->lnd_y,
230                        lookland->lnd_x, lookland->lnd_y);
231         if (dist > vrange)
232             continue;
233
234         pr("%s (#%d) %s (approx %d mil) @ %s\n",
235            cname(lp->lnd_own), lp->lnd_own,
236            prland(lp), roundintby(lp->lnd_item[I_MILIT], 20),
237            xyas(lp->lnd_x, lp->lnd_y, player->cnum));
238         if (opt_HIDDEN)
239             setcont(player->cnum, lp->lnd_own, FOUND_LOOK);
240     }
241     for (i = 0; NULL != (pp = getplanep(i)); i++) {
242         if (pp->pln_own == player->cnum || pp->pln_own == 0)
243             continue;
244         if (pp->pln_ship >= 0)
245             continue;
246         if (pp->pln_flags & PLN_LAUNCHED)
247             continue;
248         vrange = ldround((10 * range) / 20.0, 1);
249         dist = mapdist(pp->pln_x, pp->pln_y,
250                        lookland->lnd_x, lookland->lnd_y);
251         if (dist > vrange)
252             continue;
253
254         pr("%s (#%d) %s @ %s\n",
255            cname(pp->pln_own), pp->pln_own,
256            prplane(pp), xyas(pp->pln_x, pp->pln_y, player->cnum));
257         if (opt_HIDDEN)
258             setcont(player->cnum, pp->pln_own, FOUND_LOOK);
259     }
260 }