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