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