]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sail.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / sail.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 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.
25  *
26  *  ---
27  *
28  *  sail.c: Set sail path for leaders
29  * 
30  *  Known contributors to this file:
31  *     Robert Forsman
32  */
33
34 #include <ctype.h>
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "ship.h"
39 #include "path.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "nat.h"
44 #include "deity.h"
45 #include "commands.h"
46 #include "optlist.h"
47
48 static int
49 show_sail(struct nstr_item *nstr)
50 {
51     register int count = 0;
52     struct shpstr ship;
53
54     while (nxtitem(nstr, (s_char *)&ship)) {
55         if (!player->owner || ship.shp_own == 0)
56             continue;
57         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
58             pr("bad ship type %d (#%d)\n", ship.shp_type, nstr->cur);
59             continue;
60         }
61         if (count++ == 0) {
62             if (player->god)
63                 pr("own ");
64             pr("shp#     ship type       x,y    ");
65             pr("mobil mobquota follows path\n");
66         }
67         if (player->god)
68             pr("%3d ", ship.shp_own);
69         pr("%4d ", ship.shp_uid);
70         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
71         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
72         pr("%3d  ", ship.shp_mobil);
73         pr("   %3d     ", ship.shp_mobquota);
74         pr("   %3d   ", ship.shp_follow);
75         if (ship.shp_path[0]) {
76             pr(ship.shp_path);
77         } else if ((ship.shp_autonav & AN_AUTONAV)) {
78             pr("Has orders");
79         }
80         pr("\n");
81         if (opt_SHIPNAMES) {
82             if (ship.shp_name[0] != 0) {
83                 if (player->god)
84                     pr("    ");
85                 pr("       %s\n", ship.shp_name);
86             }
87         }
88     }
89     if (count == 0) {
90         if (player->argp[1])
91             pr("%s: No ship(s)\n", player->argp[1]);
92         else
93             pr("%s: No ship(s)\n", "");
94         return RET_FAIL;
95     } else
96         pr("%d ship%s\n", count, splur(count));
97     return RET_OK;
98 }
99
100 static int
101 cmd_unsail_ship(struct nstr_item *nstr)
102 {
103     struct shpstr ship;
104     int count = 0;
105
106     while (nxtitem(nstr, (s_char *)&ship)) {
107         if (!player->owner || ship.shp_own == 0)
108             continue;
109         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
110             pr("bad ship type %d (#%d)\n", ship.shp_type, nstr->cur);
111             continue;
112         }
113         if (ship.shp_path[0]) {
114             pr("Ship #%d unsailed\n", ship.shp_uid);
115             count++;
116             ship.shp_path[0] = 0;
117             putship(ship.shp_uid, &ship);
118         }
119     }
120     return RET_OK;
121 }
122
123 static int
124 cmd_sail_ship(struct nstr_item *nstr)
125 {
126     s_char *cp;
127     struct shpstr ship;
128     char navpath[MAX_PATH_LEN];
129
130     while (!player->aborted && nxtitem(nstr, (s_char *)&ship)) {
131         if (!player->owner || ship.shp_own == 0)
132             continue;
133         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
134             pr("bad ship type %d (#%d)\n", ship.shp_type, nstr->cur);
135             continue;
136         }
137         if ((ship.shp_autonav & AN_AUTONAV) &&
138             !(ship.shp_autonav & AN_STANDBY)) {
139             pr("Ship #%d has other orders!\n", ship.shp_uid);
140             continue;
141         }
142
143         pr("Ship #%d at %s\n", ship.shp_uid,
144            xyas(ship.shp_x, ship.shp_y, ship.shp_own));
145         cp = getpath(navpath, player->argp[2],
146                      ship.shp_x, ship.shp_y, 0, 0, 0, P_SAILING);
147         if (!check_ship_ok(&ship))
148             continue;
149         if (!player->aborted) {
150             bzero(ship.shp_path, sizeof(ship.shp_path));
151             strncpy(ship.shp_path, cp, sizeof(ship.shp_path) - 2);
152             ship.shp_mission = 0;
153             putship(ship.shp_uid, &ship);
154         }
155     }
156     return RET_OK;
157 }
158
159 int
160 sail(void)
161 {
162     s_char *cp;
163     struct nstr_item nstr;
164
165     if (!opt_SAIL) {
166         pr("The SAIL option is not enabled, so this command is not valid.\n");
167         return RET_FAIL;
168     }
169     if (!snxtitem(&nstr, EF_SHIP, player->argp[1]))
170         return RET_SYN;
171     cp = player->argp[2];
172     if ((*player->argp[0] == 'q') /*qsail command */ ||(cp && *cp == 'q')) {
173         return (show_sail(&nstr));
174     } else if (*player->argp[0] == 'u' /*unsail command */ 
175                || (cp && *cp == '-')) {
176         return (cmd_unsail_ship(&nstr));
177     } else
178         return (cmd_sail_ship(&nstr));
179 }