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
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.
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.
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/>.
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.
27 * look.c: Lookout from a ship or land unit
29 * Known contributors to this file:
30 * Ron Koenderink, 2006-2007
41 static void look_ship(struct shpstr *lookship);
42 static void look_land(struct lndstr *lookland);
47 return do_look(EF_SHIP);
53 return do_look(EF_LAND);
61 union empobj_storage unit;
64 unsigned char *bitmap;
67 if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
70 if (!snxtitem(&ni, type, player->argp[1], NULL))
72 bitmap = calloc((WORLD_SZ() + 7) / 8, 1);
74 logerror("malloc failed in do_look\n");
75 pr("Memory error. Tell the deity.\n");
78 while (nxtitem(&ni, &unit)) {
81 if (type == EF_LAND) {
82 if (unit.land.lnd_ship >= 0)
84 if (unit.land.lnd_land >= 0)
86 /* Spies don't need military to do a "llook". Other
88 if ((unit.land.lnd_item[I_MILIT] <= 0) &&
89 !(lchr[(int)unit.land.lnd_type].l_flags & L_SPY))
91 look_land(&unit.land);
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))
99 emp_setbit(x, y, bitmap);
100 getsect(x, y, §);
101 if (sect.sct_type == SCT_WATER)
103 look_at_sect(§, 10);
104 changed += map_set(player->cnum, x, y,
105 dchr[sect.sct_type].d_mnem, 0);
107 setcont(player->cnum, sect.sct_own, FOUND_LOOK);
112 writemap(player->cnum);
117 void look_at_sect(struct sctstr *sp, int mult)
120 int ours = player->god || sp->sct_own == player->cnum;
122 if (sp->sct_own == player->cnum)
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];
133 ours ? "" : "approx ",
134 ours ? civ : roundintby(civ, mult));
137 ours ? "" : "approx ",
138 ours ? mil : roundintby(mil, mult));
139 pr("@ %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum));
143 look_ship(struct shpstr *lookship)
146 struct mchrstr *smcp;
147 struct mchrstr *tmcp;
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)
163 dist = mapdist(sp->shp_x, sp->shp_y,
164 lookship->shp_x, lookship->shp_y);
165 if (dist > ship_max_interdiction_range)
167 tmcp = &mchr[(int)sp->shp_type];
168 if (smcp->m_flags & M_SUB)
169 vrange = (int)(shp_visib(sp) * range / 30.0);
171 vrange = (int)(shp_visib(sp) * range / 20.0);
172 getsect(sp->shp_x, sp->shp_y, §);
173 if (sect.sct_type != SCT_WATER)
174 vrange = MAX(1, vrange);
177 if (smcp->m_flags & M_SUB) {
178 if (tmcp->m_flags & M_SONAR && dist < 2) {
179 if (sp->shp_own != 0)
181 "%s detected surfacing noises in %s.\n",
183 xyas(lookship->shp_x, lookship->shp_y,
186 if (dist == 0 && (tmcp->m_flags & M_SUB) == 0)
187 if (sp->shp_own != 0)
189 "Periscope spotted in %s by %s\n",
190 xyas(lookship->shp_x, lookship->shp_y,
191 sp->shp_own), prship(sp));
193 /* subs at sea only seen by sonar */
194 if (tmcp->m_flags & M_SUB && sect.sct_type == SCT_WATER)
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));
200 setcont(player->cnum, sp->shp_own, FOUND_LOOK);
205 look_land(struct lndstr *lookland)
215 drange = techfact(lookland->lnd_tech, lchr[lookland->lnd_type].l_spy);
216 drange *= lookland->lnd_effic / 100.0;
217 range = ldround(drange, 1);
222 for (i = 0; NULL != (lp = getlandp(i)); i++) {
223 if (lp->lnd_own == player->cnum || lp->lnd_own == 0)
225 if (lp->lnd_ship >= 0)
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)
233 if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
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);
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));
247 setcont(player->cnum, lp->lnd_own, FOUND_LOOK);
249 for (i = 0; NULL != (pp = getplanep(i)); i++) {
250 if (pp->pln_own == player->cnum || pp->pln_own == 0)
252 if (pp->pln_ship >= 0)
254 if (pp->pln_flags & PLN_LAUNCHED)
256 vrange = ldround((10 * range) / 20.0, 1);
257 dist = mapdist(pp->pln_x, pp->pln_y,
258 lookland->lnd_x, lookland->lnd_y);
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));
266 setcont(player->cnum, pp->pln_own, FOUND_LOOK);