]> git.pond.sub.org Git - empserver/blob - src/lib/commands/stop.c
Fix the previous revision.
[empserver] / src / lib / commands / stop.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 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
34  */
35
36 #include <config.h>
37
38 #include "misc.h"
39 #include "player.h"
40 #include "xy.h"
41 #include "sect.h"
42 #include "nsc.h"
43 #include "nat.h"
44 #include "path.h"
45 #include "file.h"
46 #include "commands.h"
47
48 union item_u {
49     struct genitem gen;
50     struct shpstr ship;
51     struct plnstr plane;
52     struct lndstr land;
53     struct nukstr nuke;
54 };
55
56 static int start_stop(int);
57 static int start_stop_sector(char *, int);
58 static void start_stop_hdr(int);
59 static void proff(int);
60 static int start_stop_unit(int, char *, int);
61 static void start_stop_unit_hdr(int);
62 static char *unit_type_name(union item_u *);
63
64 int
65 start(void)
66 {
67     return start_stop(0);
68 }
69
70 int
71 stop(void)
72 {
73     return start_stop(1);
74 }
75
76 static int
77 start_stop(int off)
78 {
79     static int sct_or_unit[] = {
80         EF_SECTOR, EF_SHIP, EF_PLANE, EF_LAND, EF_NUKE, EF_BAD
81     };
82     int type;
83     char *arg, *p;
84     char buf[1024];
85
86     if (player->argp[1] && !isalpha(*player->argp[1])) {
87         /* accept legacy syntax */
88         type = EF_SECTOR;
89         arg = player->argp[1];
90     } else {
91         p = getstarg(player->argp[1],
92                      "Sector, ship, plane, land unit or nuke? ", buf);
93         if (p == 0)
94             return RET_SYN;
95         type = ef_byname_from(p, sct_or_unit);
96         if (type < 0) {
97             pr("Sectors, ships, planes, land units or nukes only!\n");
98             return RET_SYN;
99         }
100         arg = player->argp[2];
101     }
102
103     if (type == EF_SECTOR)
104         return start_stop_sector(arg, off);
105     return start_stop_unit(type, arg, off);
106 }
107
108 static int
109 start_stop_sector(char *arg, int off)
110 {
111     struct sctstr sect;
112     int nsect;
113     struct nstr_sect nstr;
114
115     if (!snxtsct(&nstr, arg))
116         return RET_SYN;
117     prdate();
118     nsect = 0;
119     while (nxtsct(&nstr, &sect)) {
120         if (!player->owner)
121             continue;
122         if (!sect.sct_off == !off)
123             continue;
124         if (nsect++ == 0)
125             start_stop_hdr(off);
126         if (player->god)
127             pr("%3d ", sect.sct_own);
128         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
129         pr(" %c", dchr[sect.sct_type].d_mnem);
130         if (sect.sct_newtype != sect.sct_type)
131             pr("%c", dchr[sect.sct_newtype].d_mnem);
132         else
133             pr(" ");
134         pr("%4d%%", sect.sct_effic);
135         proff(off);
136         sect.sct_off = off;
137         putsect(&sect);
138     }
139     if (nsect == 0) {
140         pr("%s: No sector(s)\n", arg ? arg : "");
141         return RET_FAIL;
142     } else
143         pr("%d sector%s\n", nsect, splur(nsect));
144     return 0;
145 }
146
147 static void
148 start_stop_hdr(int off)
149 {
150     if (player->god)
151         pr("    ");
152     pr("PRODUCTION %s\n", off ? "STOPPAGE" : "STARTING");
153     if (player->god)
154         pr("own ");
155     pr("  sect        eff\n");
156 }
157
158 static void
159 proff(int off)
160 {
161     if (off)
162         pr("  will not produce or gain efficiency.\n");
163     else
164         pr("  will be updated normally.\n");
165 }
166
167 static int
168 start_stop_unit(int type, char *arg, int off)
169 {
170     union item_u unit;
171     int nunit;
172     struct nstr_item nstr;
173
174     if (!snxtitem(&nstr, type, arg))
175         return RET_SYN;
176     prdate();
177     nunit = 0;
178     while (nxtitem(&nstr, &unit)) {
179         if (!player->owner)
180             continue;
181         if (!unit.gen.off == !off)
182             continue;
183         if (nunit++ == 0)
184             start_stop_unit_hdr(off);
185         if (player->god)
186             pr("%3d ", unit.gen.own);
187         pr("%4d %-4.4s ", nstr.cur, unit_type_name(&unit));
188         prxy("%4d,%-4d", unit.gen.x, unit.gen.y, player->cnum);
189         pr("%4d%%", unit.gen.effic);
190         proff(off);
191         unit.gen.off = off;
192         ef_write(type, nstr.cur, &unit);
193     }
194     if (nunit == 0) {
195         pr("%s: No %s(s)\n", arg ? arg : "", ef_nameof(type));
196         return RET_FAIL;
197     } else
198         pr("%d %s%s\n", nunit, ef_nameof(type), splur(nunit));
199     return 0;
200 }
201
202 static void
203 start_stop_unit_hdr(int off)
204 {
205     if (player->god)
206         pr("    ");
207     pr("PRODUCTION %s\n", off ? "STOPPAGE" : "STARTING");
208     if (player->god)
209         pr("own ");
210     pr("   #         x,y     eff\n");
211 }
212
213 static char *
214 unit_type_name(union item_u *unit)
215 {
216     int type = unit->gen.type;
217
218     switch (unit->gen.ef_type) {
219     case EF_SHIP:
220         return mchr[type].m_name;
221     case EF_PLANE:
222         return plchr[type].pl_name;
223     case EF_LAND:
224         return lchr[type].l_name;
225     case EF_NUKE:
226         return nchr[type].n_name;
227     }
228     CANT_REACH();
229     return "?";
230 }