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