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