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