]> git.pond.sub.org Git - empserver/blob - src/lib/commands/stop.c
Fix trailing whitespace
[empserver] / src / lib / commands / stop.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  stop.c: Stop a sector or unit from producing
29  *
30  *  Known contributors to this file:
31  *     Thomas Ruschak, 1992
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2006-2008
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include "commands.h"
40 #include "empobj.h"
41 #include "path.h"
42
43 static int start_stop(int);
44 static int start_stop_sector(char *, int);
45 static void start_stop_hdr(int);
46 static void proff(int);
47 static int start_stop_unit(int, char *, int);
48 static void start_stop_unit_hdr(int);
49
50 int
51 start(void)
52 {
53     return start_stop(0);
54 }
55
56 int
57 stop(void)
58 {
59     return start_stop(1);
60 }
61
62 static int
63 start_stop(int off)
64 {
65     static int sct_or_unit[] = {
66         EF_SECTOR, EF_SHIP, EF_PLANE, EF_LAND, EF_NUKE, EF_BAD
67     };
68     int type;
69     char *arg, *p;
70     char buf[1024];
71
72     if (player->argp[1] && !isalpha(*player->argp[1])) {
73         /* accept legacy syntax */
74         type = EF_SECTOR;
75         arg = player->argp[1];
76     } else {
77         p = getstarg(player->argp[1],
78                      "Sector, ship, plane, land unit or nuke? ", buf);
79         if (p == 0)
80             return RET_SYN;
81         type = ef_byname_from(p, sct_or_unit);
82         if (type < 0) {
83             pr("Sectors, ships, planes, land units or nukes only!\n");
84             return RET_SYN;
85         }
86         arg = player->argp[2];
87     }
88
89     if (type == EF_SECTOR)
90         return start_stop_sector(arg, off);
91     return start_stop_unit(type, arg, off);
92 }
93
94 static int
95 start_stop_sector(char *arg, int off)
96 {
97     struct sctstr sect;
98     int nsect;
99     struct nstr_sect nstr;
100
101     if (!snxtsct(&nstr, arg))
102         return RET_SYN;
103     prdate();
104     nsect = 0;
105     while (nxtsct(&nstr, &sect)) {
106         if (!player->owner)
107             continue;
108         if (!sect.sct_off == !off)
109             continue;
110         if (nsect++ == 0)
111             start_stop_hdr(off);
112         if (player->god)
113             pr("%3d ", sect.sct_own);
114         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
115         pr(" %c", dchr[sect.sct_type].d_mnem);
116         if (sect.sct_newtype != sect.sct_type)
117             pr("%c", dchr[sect.sct_newtype].d_mnem);
118         else
119             pr(" ");
120         pr("%4d%%", sect.sct_effic);
121         proff(off);
122         sect.sct_off = off;
123         putsect(&sect);
124     }
125     if (nsect == 0) {
126         pr("%s: No sector(s)\n", arg ? arg : "");
127         return RET_FAIL;
128     } else
129         pr("%d sector%s\n", nsect, splur(nsect));
130     return 0;
131 }
132
133 static void
134 start_stop_hdr(int off)
135 {
136     if (player->god)
137         pr("    ");
138     pr("PRODUCTION %s\n", off ? "STOPPAGE" : "STARTING");
139     if (player->god)
140         pr("own ");
141     pr("  sect        eff\n");
142 }
143
144 static void
145 proff(int off)
146 {
147     if (off)
148         pr("  will not produce or gain efficiency.\n");
149     else
150         pr("  will be updated normally.\n");
151 }
152
153 static int
154 start_stop_unit(int type, char *arg, int off)
155 {
156     union empobj_storage unit;
157     int nunit;
158     struct nstr_item nstr;
159
160     if (!snxtitem(&nstr, type, arg, NULL))
161         return RET_SYN;
162     prdate();
163     nunit = 0;
164     while (nxtitem(&nstr, &unit)) {
165         if (!player->owner)
166             continue;
167         if (!unit.gen.off == !off)
168             continue;
169         if (nunit++ == 0)
170             start_stop_unit_hdr(off);
171         if (player->god)
172             pr("%3d ", unit.gen.own);
173         pr("%4d %-4.4s ", nstr.cur, empobj_chr_name(&unit.gen));
174         prxy("%4d,%-4d", unit.gen.x, unit.gen.y, player->cnum);
175         pr("%4d%%", unit.gen.effic);
176         proff(off);
177         unit.gen.off = off;
178         ef_write(type, nstr.cur, &unit);
179     }
180     if (nunit == 0) {
181         pr("%s: No %s(s)\n", arg ? arg : "", ef_nameof(type));
182         return RET_FAIL;
183     } else
184         pr("%d %s%s\n", nunit, ef_nameof(type), splur(nunit));
185     return 0;
186 }
187
188 static void
189 start_stop_unit_hdr(int off)
190 {
191     if (player->god)
192         pr("    ");
193     pr("PRODUCTION %s\n", off ? "STOPPAGE" : "STARTING");
194     if (player->god)
195         pr("own ");
196     pr("   #         x,y     eff\n");
197 }