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