2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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 = 0; /* flagship */
68 s_char pathtaken[1024]; /* Doubtful we'll have a path longer than this */
69 s_char *pt = pathtaken;
71 if (!snxtitem(&ni_ship, EF_SHIP, player->argp[1]))
73 shp_sel(&ni_ship, &ship_list);
74 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
75 player->condarg = 0; /* conditions don't apply to nav_map() */
76 if (QEMPTY(&ship_list)) {
80 set_flagship(&ship_list, &shp);
81 if (player->argp[2]) {
82 strcpy(buf, player->argp[2]);
83 if (!(cp = shp_path(together, shp, buf)))
88 while (!QEMPTY(&ship_list)) {
91 if (cp == 0 || *cp == '\0' || stopping) {
93 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
94 if (QEMPTY(&ship_list)) {
95 pr("No ships left\n");
96 if (strlen(pathtaken) > 0) {
97 pathtaken[strlen(pathtaken) - 1] = '\0';
98 if (strlen(pathtaken) > 0)
99 pr("Path taken: %s\n", pathtaken);
103 if (set_flagship(&ship_list, &shp)) {
108 nav_map(shp->shp_x, shp->shp_y,
109 !(mchr[(int)shp->shp_type].m_flags & M_SUB));
112 sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
113 minmob, xyas(shp->shp_x, shp->shp_y, player->cnum));
114 cp = getstring(prompt, buf);
115 /* Just in case any of our ships were shelled while we were at the
116 * prompt, we call shp_nav() again.
118 shp_nav(&ship_list, &minmob, &maxmob, &together, player->cnum);
119 if (QEMPTY(&ship_list)) {
120 pr("No ships left\n");
121 if (strlen(pathtaken) > 0) {
122 pathtaken[strlen(pathtaken) - 1] = '\0';
123 if (strlen(pathtaken) > 0)
124 pr("Path taken: %s\n", pathtaken);
128 if (set_flagship(&ship_list, &shp)) {
133 radmapnopr(shp->shp_x, shp->shp_y, (int)shp->shp_effic,
134 (int)techfact(shp->shp_tech,
135 (double)mchr[(int)shp->shp_type].m_vrnge),
136 (double)((mchr[(int)shp->shp_type].m_flags & M_SONAR)
137 ? techfact(shp->shp_tech, 1.0) : 0.0));
138 if (cp == 0 || *cp == '\0')
139 cp = &dirch[DIR_STOP];
141 *cp == 'B' || *cp == 'f' || *cp == 'i' || *cp == 'm') {
144 unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
145 draw_map(0, origin, MAP_SHIP, &ns, player->cnum);
147 } else if (cp[-1] == 'B') {
148 unit_map(EF_SHIP, shp->shp_uid, &ns, &origin);
149 draw_map(EF_BMAP, origin, MAP_SHIP, &ns, player->cnum);
151 } else if (cp[-1] == 'f') {
152 struct emp_qelem *qp;
153 qp = ship_list.q_back;
154 emp_remque(ship_list.q_back);
155 emp_insque(qp, &ship_list);
156 set_flagship(&ship_list, &shp);
157 } else if (cp[-1] == 'i') {
158 shp_list(&ship_list);
160 stopping |= shp_sweep(&ship_list, 1, player->cnum);
163 } else if (*cp == 'r' || *cp == 'l' || *cp == 's') {
165 while ((*bp != ' ') && (*bp))
167 while ((*bp == ' ') && (*bp))
169 if ((bp != (s_char *)0) && (*bp))
170 player->argp[1] = bp;
172 sprintf(dp, "%d", shp->shp_uid);
173 player->argp[1] = dp;
178 } else if (cp[-1] == 'l')
189 dir = chkdir(*cp++, DIR_STOP, DIR_VIEW);
191 if (NULL != (cp = shp_path(together, shp, buf)))
193 direrr("`%c' to stop", ", `%c' to view, ", 0);
194 pr("`i' to list ships, `f' to change flagship,\n");
195 pr("`r' to radar, `s' to sonar, `l' to look, `M' to map, `B' to bmap,\n");
196 pr("and `m' to minesweep\n");
199 } else if (dir == DIR_VIEW) {
200 shp_view(&ship_list);
205 shp_nav_one_sector(&ship_list, dir, player->cnum, together);
211 if (strlen(pathtaken) > 0) {
212 pathtaken[strlen(pathtaken) - 1] = '\0';
213 if (strlen(pathtaken) > 0)
214 pr("Path taken: %s\n", pathtaken);
220 nav_map(int x, int y, int show_designations)
222 register s_char *ptr;
228 /* Note this is not re-entrant anyway, so we keep the buffers
230 static u_char *bitmap = (u_char *)0;
231 static s_char *wmapbuf = (s_char *)0;
232 static s_char **wmap = (s_char **)0;
236 np = getnatp(player->cnum);
237 sprintf(what, "%d:%d,%d:%d", xrel(np, x - 2), xrel(np, x + 2),
238 yrel(np, y - 1), yrel(np, y + 1));
239 if (!snxtsct(&ns, what))
243 (s_char *)malloc((WORLD_Y * MAPWIDTH(1)) * sizeof(s_char));
245 wmap = (s_char **)malloc(WORLD_Y * sizeof(s_char *));
246 if (wmap && wmapbuf) {
247 for (i = 0; i < WORLD_Y; i++)
248 wmap[i] = &wmapbuf[MAPWIDTH(1) * i];
250 free((s_char *)wmap);
255 bitmap = (u_char *)malloc((WORLD_X * WORLD_Y) / 8);
256 if (!wmapbuf || !wmap || !bitmap) {
257 pr("Memory error, tell the deity.\n");
258 logerror("malloc failed in navi\n");
261 memset(bitmap, 0, (WORLD_X * WORLD_Y) / 8);
262 /* zap any conditionals */
264 xyrelrange(np, &ns.range, &range);
265 blankfill((s_char *)wmapbuf, &ns.range, 1);
266 while (nxtsct(&ns, §)) {
267 ptr = &wmap[ns.dy][ns.dx];
268 *ptr = dchr[sect.sct_type].d_mnem;
269 if (!show_designations &&
270 sect.sct_own != player->cnum &&
271 sect.sct_type != SCT_WATER &&
272 sect.sct_type != SCT_BSPAN && sect.sct_type != SCT_HARBR)
274 changed += map_set(player->cnum, sect.sct_x, sect.sct_y, *ptr, 0);
276 * We do it this way so that 'x' and 'X'
277 * bdesignations will show up. This can
278 * be used to mark mined sectors. So, the
279 * player will see the current des, UNLESS
280 * they've marked the sector 'x' or 'X',
281 * in which case they'll see that.
284 *ptr = player->bmap[sctoff(sect.sct_x, sect.sct_y)];
287 writemap(player->cnum);
288 for (i = 0; i < ns.range.height; i++)
294 set_flagship(struct emp_qelem *list, struct shpstr **flagshipp)
296 struct mlist *mlp = (struct mlist *)(list->q_back);
300 else if ((*flagshipp)->shp_uid != mlp->ship.shp_uid)
301 pr("Changing flagship to ");
304 *flagshipp = &mlp->ship;
305 pr("%s\n", prship(&mlp->ship));