2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * navi.c: Navigate ships and such
30 * Known contributors to this file:
31 * Ken Stevens, 1995 (rewritten)
50 static int set_flagship(struct emp_qelem *list, struct shpstr **flagshipp);
55 struct nstr_item ni_ship;
56 struct emp_qelem ship_list;
57 double minmob, maxmob;
60 struct shpstr *shp = NULL; /* flagship */
69 char pathtaken[1024]; /* Doubtful we'll have a path longer than this */
74 if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1]))
76 shp_sel(&ni_ship, &ship_list);
77 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
78 if (QEMPTY(&ship_list)) {
82 set_flagship(&ship_list, &shp);
83 if (player->argp[2]) {
84 strcpy(buf, player->argp[2]);
85 if (!(cp = shp_path(together, shp, buf)))
90 while (!QEMPTY(&ship_list)) {
93 if (cp == NULL || *cp == '\0' || stopping) {
95 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
96 if (QEMPTY(&ship_list)) {
97 pr("No ships left\n");
98 if (strlen(pathtaken) > 0) {
99 pathtaken[strlen(pathtaken) - 1] = '\0';
100 if (strlen(pathtaken) > 0)
101 pr("Path taken: %s\n", pathtaken);
105 if (set_flagship(&ship_list, &shp)) {
110 nav_map(shp->shp_x, shp->shp_y,
111 !(mchr[(int)shp->shp_type].m_flags & M_SUB));
114 sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
115 minmob, xyas(shp->shp_x, shp->shp_y, player->cnum));
116 cp = getstring(prompt, buf);
117 /* Just in case any of our ships were shelled while we were
118 * at the prompt, we call shp_nav() again.
120 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
121 if (QEMPTY(&ship_list)) {
122 pr("No ships left\n");
123 if (strlen(pathtaken) > 0) {
124 pathtaken[strlen(pathtaken) - 1] = '\0';
125 if (strlen(pathtaken) > 0)
126 pr("Path taken: %s\n", pathtaken);
130 if (set_flagship(&ship_list, &shp)) {
134 if (cp && !(cp = shp_path(together, shp, buf)))
137 radmapnopr(shp->shp_x, shp->shp_y, (int)shp->shp_effic,
138 (int)techfact(shp->shp_tech,
139 mchr[(int)shp->shp_type].m_vrnge),
140 (mchr[(int)shp->shp_type].m_flags & M_SONAR)
141 ? techfact(shp->shp_tech, 1.0) : 0.0);
142 if (cp == NULL || *cp == '\0')
143 cp = &dirch[DIR_STOP];
144 dir = chkdir(*cp, DIR_STOP, DIR_VIEW);
147 shp_view(&ship_list);
149 stopping |= shp_nav_one_sector(&ship_list, dir, player->cnum, together);
158 ac = parse(cp, player->argp, NULL, scanspace, NULL);
160 sprintf(dp, "%d", shp->shp_uid);
161 player->argp[1] = dp;
166 switch (*player->argp[0]) {
173 unit_map(EF_SHIP, atoi(player->argp[1]), &ns, &origin);
174 draw_map(bmap_flag, origin, MAP_SHIP, &ns);
179 struct emp_qelem *qp;
181 qp = ship_list.q_back;
182 emp_remque(ship_list.q_back);
183 emp_insque(qp, &ship_list);
184 set_flagship(&ship_list, &shp);
188 shp_list(&ship_list);
191 stopping |= shp_sweep(&ship_list, 1, 0, player->cnum);
209 player->argp[2] = player->argp[1];
210 sprintf(dp, "%d", shp->shp_uid);
211 player->argp[1] = dp;
218 direrr("`%c' to stop", ", `%c' to view, ", 0);
219 pr("`i' to list ships, `f' to change flagship,\n");
220 pr("`r' to radar, `s' to sonar, `l' to look, `M' to map, `B' to bmap,\n");
221 pr("`d' to drop mines, and `m' to minesweep\n");
225 if (strlen(pathtaken) > 0) {
226 pathtaken[strlen(pathtaken) - 1] = '\0';
227 if (strlen(pathtaken) > 0)
228 pr("Path taken: %s\n", pathtaken);
234 nav_map(int x, int y, int show_designations)
242 /* Note this is not re-entrant anyway, so we keep the buffers
244 static unsigned char *bitmap = NULL;
245 static char *wmapbuf = NULL;
246 static char **wmap = NULL;
250 wmapbuf = malloc(WORLD_Y * MAPWIDTH(1));
252 wmap = malloc(WORLD_Y * sizeof(*wmap));
253 if (wmap && wmapbuf) {
254 for (i = 0; i < WORLD_Y; i++)
255 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
262 bitmap = malloc((WORLD_X * WORLD_Y) / 8);
263 if (!wmapbuf || !wmap || !bitmap) {
264 pr("Memory error, tell the deity.\n");
265 logerror("malloc failed in navi\n");
268 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
269 snxtsct_dist(&ns, x, y, 1);
270 np = getnatp(player->cnum);
271 xyrelrange(np, &ns.range, &range);
272 blankfill(wmapbuf, &ns.range, 1);
273 while (nxtsct(&ns, §)) {
274 ptr = &wmap[ns.dy][ns.dx];
275 *ptr = dchr[sect.sct_type].d_mnem;
276 if (!show_designations &&
277 sect.sct_own != player->cnum &&
278 sect.sct_type != SCT_WATER &&
279 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
281 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
283 * We do it this way so that 'x' and 'X'
284 * bdesignations will show up. This can
285 * be used to mark mined sectors. So, the
286 * player will see the current des, UNLESS
287 * they've marked the sector 'x' or 'X',
288 * in which case they'll see that.
291 *ptr = player->bmap[sctoff(sect.sct_x, sect.sct_y)];
294 writemap(player->cnum);
295 for (i = 0; i < ns.range.height; i++)
301 set_flagship(struct emp_qelem *list, struct shpstr **flagshipp)
303 struct mlist *mlp = (struct mlist *)(list->q_back);
307 else if ((*flagshipp)->shp_uid != mlp->ship.shp_uid)
308 pr("Changing flagship to ");
311 *flagshipp = &mlp->ship;
312 pr("%s\n", prship(&mlp->ship));