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