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