]> git.pond.sub.org Git - empserver/blob - src/lib/commands/foll.c
5ce66e3bd485534b3ecffda1f9ec81f1d32fecbf
[empserver] / src / lib / commands / foll.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 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  *  foll.c: Set leader of a set of ships
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 "xy.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "nat.h"
44 #include "commands.h"
45 #include "optlist.h"
46
47 int
48 foll(void)
49 {
50     struct shpstr ship;
51     s_char *cp;
52     int good, leader, count = 0;
53     coord x, y;
54     struct nstr_item nstr;
55     s_char buf[1024];
56
57     if (!opt_SAIL) {
58         pr("The SAIL option is not enabled, so this command is not valid.\n");
59         return RET_FAIL;
60     }
61     if (!snxtitem(&nstr, EF_SHIP, player->argp[1]))
62         return RET_SYN;
63     cp = getstarg(player->argp[2], "leader?", buf);
64     if (cp == 0)
65         cp = "";
66     good = sscanf(cp, "%d", &leader);
67     if (!good)
68         return RET_SYN;
69     getship(leader, &ship);
70     if (ship.shp_own != player->cnum &&
71         getrel(getnatp(ship.shp_own), player->cnum) < FRIENDLY) {
72         pr("That ship won't let you follow.\n");
73         return RET_FAIL;
74     }
75     x = ship.shp_x;
76     y = ship.shp_y;
77     while (nxtitem(&nstr, &ship)) {
78         if (!player->owner)
79             continue;
80         if (ship.shp_x != x || ship.shp_y != y) {
81             pr("Ship #%d not in same sector as #%d\n", ship.shp_uid,
82                leader);
83             continue;
84         }
85         if (ship.shp_uid == leader) {
86             pr("Ship #%d can't follow itself!\n", leader);
87             continue;
88         }
89         if ((ship.shp_autonav & AN_AUTONAV)
90             && !(ship.shp_autonav & AN_STANDBY)) {
91             pr("Ship #%d has other orders!\n", ship.shp_uid);
92             continue;
93         }
94         count++;
95         ship.shp_mission = 0;
96         *ship.shp_path = 'f';
97         ship.shp_path[1] = 0;
98         ship.shp_follow = leader;
99         pr("Ship #%d follows #%d.\n", ship.shp_uid, leader);
100         putship(ship.shp_uid, &ship);
101     }
102     if (count == 0) {
103         if (player->argp[1])
104             pr("%s: No ship(s)\n", player->argp[1]);
105         else
106             pr("%s: No ship(s)\n", "");
107         return RET_FAIL;
108     } else
109         pr("%d ship%s\n", count, splur(count));
110     return RET_OK;
111 }