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