]> 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-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  sail.c: Set sail path for leaders
28  *
29  *  Known contributors to this file:
30  *     Robert Forsman
31  */
32
33 #include <config.h>
34
35 #include "commands.h"
36 #include "optlist.h"
37 #include "path.h"
38 #include "ship.h"
39
40 static int
41 show_sail(struct nstr_item *nstr)
42 {
43     int count = 0;
44     struct shpstr ship;
45
46     while (nxtitem(nstr, &ship)) {
47         if (!player->owner || ship.shp_own == 0)
48             continue;
49         if (count++ == 0) {
50             if (player->god)
51                 pr("own ");
52             pr("shp#     ship type       x,y    ");
53             pr("mobil mobquota follows path\n");
54         }
55         if (player->god)
56             pr("%3d ", ship.shp_own);
57         pr("%4d ", ship.shp_uid);
58         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
59         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y);
60         pr("%3d  ", ship.shp_mobil);
61         pr("   %3d     ", ship.shp_mobquota);
62         pr("   %3d   ", ship.shp_follow);
63         if (ship.shp_path[0]) {
64             pr("%s", ship.shp_path);
65         } else if ((ship.shp_autonav & AN_AUTONAV)) {
66             pr("Has orders");
67         }
68         pr("\n");
69         if (ship.shp_name[0] != 0) {
70             if (player->god)
71                 pr("    ");
72             pr("       %s\n", ship.shp_name);
73         }
74     }
75     if (count == 0) {
76         if (player->argp[1])
77             pr("%s: No ship(s)\n", player->argp[1]);
78         else
79             pr("%s: No ship(s)\n", "");
80         return RET_FAIL;
81     } else
82         pr("%d ship%s\n", count, splur(count));
83     return RET_OK;
84 }
85
86 static int
87 cmd_unsail_ship(struct nstr_item *nstr)
88 {
89     struct shpstr ship;
90     int count = 0;
91
92     while (nxtitem(nstr, &ship)) {
93         if (!player->owner || ship.shp_own == 0)
94             continue;
95         if (ship.shp_path[0]) {
96             pr("Ship #%d unsailed\n", ship.shp_uid);
97             count++;
98             ship.shp_path[0] = 0;
99             putship(ship.shp_uid, &ship);
100         }
101     }
102     return RET_OK;
103 }
104
105 static int
106 cmd_sail_ship(struct nstr_item *nstr)
107 {
108     char *cp;
109     struct shpstr ship;
110     char navpath[MAX_PATH_LEN];
111
112     while (nxtitem(nstr, &ship)) {
113         if (!player->owner || ship.shp_own == 0)
114             continue;
115         if ((ship.shp_autonav & AN_AUTONAV) &&
116             !(ship.shp_autonav & AN_STANDBY)) {
117             pr("Ship #%d has other orders!\n", ship.shp_uid);
118             continue;
119         }
120
121         pr("Ship #%d at %s\n", ship.shp_uid,
122            xyas(ship.shp_x, ship.shp_y, player->cnum));
123         cp = getpath(navpath, player->argp[2],
124                      ship.shp_x, ship.shp_y, 0, 0, MOB_SAIL);
125         if (!cp)
126             return RET_FAIL;
127         if (!check_ship_ok(&ship))
128             continue;
129         strncpy(ship.shp_path, cp, sizeof(ship.shp_path) - 1);
130         ship.shp_mission = 0;
131         putship(ship.shp_uid, &ship);
132     }
133     return RET_OK;
134 }
135
136 int
137 sail(void)
138 {
139     char *cp;
140     struct nstr_item nstr;
141
142     if (!opt_SAIL) {
143         pr("The SAIL option is not enabled, so this command is not valid.\n");
144         return RET_FAIL;
145     }
146     if (!snxtitem(&nstr, EF_SHIP, player->argp[1], NULL))
147         return RET_SYN;
148     cp = player->argp[2];
149     if (*player->argp[0] == 'u' || (cp && !strcmp(cp, "-")))
150         return cmd_unsail_ship(&nstr);
151     if (cp && *cp == 'q')
152         return show_sail(&nstr);
153     return cmd_sail_ship(&nstr);
154 }