]> git.pond.sub.org Git - empserver/blob - src/lib/commands/edit.c
edit: Parse int values in edit_FOO() helpers rather than edit()
[empserver] / src / lib / commands / edit.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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  *  edit.c: Edit things (sectors, ships, planes, units, nukes, countries)
28  *
29  *  Known contributors to this file:
30  *     David Muir Sharnoff
31  *     Chad Zabel, 1994
32  *     Steve McClure, 1998-2000
33  *     Ron Koenderink, 2003-2009
34  *     Markus Armbruster, 2003-2013
35  */
36
37 #include <config.h>
38
39 #include <ctype.h>
40 #include <limits.h>
41 #include "actofgod.h"
42 #include "commands.h"
43 #include "item.h"
44 #include "land.h"
45 #include "news.h"
46 #include "optlist.h"
47 #include "plague.h"
48 #include "plane.h"
49 #include "ship.h"
50
51 static void print_sect(struct sctstr *);
52 static void print_nat(struct natstr *);
53 static void print_plane(struct plnstr *);
54 static void print_land(struct lndstr *);
55 static void print_ship(struct shpstr *);
56 static char *getin(char *, char **);
57 static int edit_sect(struct sctstr *, char *, char *);
58 static int edit_sect_i(struct sctstr *, char *, int);
59 static int edit_nat(struct natstr *, char *, char *);
60 static int edit_ship(struct shpstr *, char *, char *);
61 static int edit_land(struct lndstr *, char *, char *);
62 static int edit_plane(struct plnstr *, char *, char *);
63
64 int
65 edit(void)
66 {
67     struct sctstr sect;
68     struct plnstr plane;
69     struct shpstr ship;
70     struct lndstr land;
71     char *what;
72     char *key, *ptr;
73     int num;
74     int err;
75     int arg_index = 3;
76     coord x, y;
77     struct natstr *np;
78     char buf[1024];
79     char ewhat;
80
81     what = getstarg(player->argp[1],
82                     "Edit what (country, land, ship, plane, nuke, unit)? ",
83                     buf);
84     if (!what)
85         return RET_SYN;
86     ewhat = what[0];
87     switch (ewhat) {
88     case 'l':
89         if (!(ptr = getstarg(player->argp[2], "Sector : ", buf)))
90             return RET_FAIL;
91         if (!sarg_xy(ptr, &x, &y))
92             return RET_FAIL;
93         if (!getsect(x, y, &sect))
94             return RET_FAIL;
95         break;
96     case 'c':
97         np = natargp(player->argp[2], "Country? ");
98         if (!np)
99             return RET_SYN;
100         break;
101     case 'p':
102         if ((num = onearg(player->argp[2], "Plane number? ")) < 0)
103             return RET_SYN;
104         if (!getplane(num, &plane))
105             return RET_SYN;
106         break;
107     case 's':
108         if ((num = onearg(player->argp[2], "Ship number? ")) < 0)
109             return RET_SYN;
110         if (!getship(num, &ship))
111             return RET_SYN;
112         break;
113     case 'u':
114         if ((num = onearg(player->argp[2], "Unit number? ")) < 0)
115             return RET_SYN;
116         if (!getland(num, &land))
117             return RET_SYN;
118         break;
119     case 'n':
120         pr("Not implemented yet.\n");
121         break;
122     default:
123         pr("huh?\n");
124         return RET_SYN;
125     }
126     if (!player->argp[3]) {
127         switch (ewhat) {
128         case 'l':
129             print_sect(&sect);
130             break;
131         case 'c':
132             print_nat(np);
133             break;
134         case 'p':
135             print_plane(&plane);
136             break;
137         case 's':
138             print_ship(&ship);
139             break;
140         case 'u':
141             print_land(&land);
142             break;
143         }
144     }
145     for (;;) {
146         if (player->argp[arg_index]) {
147             if (player->argp[arg_index+1]) {
148                 key = player->argp[arg_index++];
149                 ptr = player->argp[arg_index++];
150             } else
151                 return RET_SYN;
152         } else if (arg_index == 3) {
153             key = getin(buf, &ptr);
154             if (!key)
155                 return RET_SYN;
156             if (!*key) {
157                 switch (ewhat) {
158                 case 'c':
159                     print_nat(np);
160                     break;
161                 case 'l':
162                     print_sect(&sect);
163                     break;
164                 case 's':
165                     print_ship(&ship);
166                     break;
167                 case 'u':
168                     print_land(&land);
169                     break;
170                 case 'p':
171                     print_plane(&plane);
172                     break;
173                 }
174                 return RET_OK;
175             }
176         } else
177             return RET_OK;
178
179         switch (ewhat) {
180         case 'c':
181             if ((err = edit_nat(np, key, ptr)) != RET_OK)
182                 return err;
183             break;
184         case 'l':
185             if (!check_sect_ok(&sect))
186                 return RET_FAIL;
187             if ((err = edit_sect(&sect, key, ptr)) != RET_OK)
188                 return err;
189             if (!putsect(&sect))
190                 return RET_FAIL;
191             break;
192         case 's':
193             if (!check_ship_ok(&ship))
194                 return RET_FAIL;
195             if ((err = edit_ship(&ship, key, ptr)) != RET_OK)
196                 return err;
197             if (!ef_ensure_space(EF_SHIP, ship.shp_uid, 50))
198                 return RET_FAIL;
199             if (!putship(ship.shp_uid, &ship))
200                 return RET_FAIL;
201             break;
202         case 'u':
203             if (!check_land_ok(&land))
204                 return RET_FAIL;
205             if ((err = edit_land(&land, key, ptr)) != RET_OK)
206                 return err;
207             if (!ef_ensure_space(EF_LAND, land.lnd_uid, 50))
208                 return RET_FAIL;
209             if (!putland(land.lnd_uid, &land))
210                 return RET_FAIL;
211             break;
212         case 'p':
213             if (!check_plane_ok(&plane))
214                 return RET_FAIL;
215             if ((err = edit_plane(&plane, key, ptr)) != RET_OK)
216                 return err;
217             if (!ef_ensure_space(EF_PLANE, plane.pln_uid, 50))
218                 return RET_FAIL;
219             if (!putplane(plane.pln_uid, &plane))
220                 return RET_FAIL;
221             break;
222         }
223     }
224 }
225
226 static void
227 benefit(natid who, int goodness)
228 {
229     if (opt_GODNEWS && getnatp(who)->nat_stat != STAT_GOD && goodness)
230         nreport(player->cnum, goodness > 0 ? N_AIDS : N_HURTS, who, 1);
231 }
232
233 static void
234 noise(struct sctstr *sptr, char *name, int old, int new)
235 {
236     pr("%s of %s changed from %d to %d\n",
237        name, xyas(sptr->sct_x, sptr->sct_y, player->cnum), old, new);
238     if (sptr->sct_own && sptr->sct_own != player->cnum && new != old)
239         wu(0, sptr->sct_own,
240            "%s in %s was changed from %d to %d by an act of %s\n",
241            name, xyas(sptr->sct_x, sptr->sct_y, sptr->sct_own),
242            old, new, cname(player->cnum));
243     benefit(sptr->sct_own, new - old);
244 }
245
246 static void
247 print_sect(struct sctstr *sect)
248 {
249     pr("Location <L>: %s\t", xyas(sect->sct_x, sect->sct_y, player->cnum));
250     pr("Distribution sector <D>: %s\n",
251        xyas(sect->sct_dist_x, sect->sct_dist_y, player->cnum));
252     pr("Designation <s>: %c\tNew designation <S>: %c\n",
253        dchr[sect->sct_type].d_mnem, dchr[sect->sct_newtype].d_mnem);
254     pr("own  oo eff mob min gld frt oil urn wrk lty che ctg plg ptime fall avail\n");
255     pr("  o   O   e   m   i   g   f   c   u   w   l   x   X   p     t    F     a\n");
256     pr("%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %5d %4d %5d\n",
257        sect->sct_own, sect->sct_oldown, sect->sct_effic, sect->sct_mobil,
258        sect->sct_min, sect->sct_gmin, sect->sct_fertil, sect->sct_oil,
259        sect->sct_uran, sect->sct_work, sect->sct_loyal,
260        sect->sct_che, sect->sct_che_target,
261        sect->sct_pstage, sect->sct_ptime,
262        sect->sct_fallout, sect->sct_avail);
263
264     pr("Mines <M>: %d\n", sect->sct_mines);
265     pr("Road %% <R>: %d\t", sect->sct_road);
266     pr("Rail %% <r>: %d\t", sect->sct_rail);
267     pr("Defense %% <d>: %d\n", sect->sct_defense);
268 }
269
270 static void
271 print_nat(struct natstr *np)
272 {
273     int i;
274
275     pr("Country #: %2d\n", np->nat_cnum);
276     pr("Name <n>: %-20s\t", np->nat_cnam);
277     pr("Representative <r>: %-20s\n", np->nat_pnam);
278     pr("BTUs <b>: %3d\t\t\t", np->nat_btu);
279     pr("Reserves <m>: %5d\n", np->nat_reserve);
280     pr("Capital <c>: %s\t\t",
281        xyas(np->nat_xcap, np->nat_ycap, player->cnum));
282     pr("Origin <o>: %3s\n",
283        xyas(np->nat_xorg, np->nat_yorg, player->cnum));
284     pr("Status <s>: 0x%x\t\t\t", np->nat_stat);
285     pr("Seconds Used <u>: %3d\n", np->nat_timeused);
286     pr("Technology <T>: %.2f\t\t", np->nat_level[NAT_TLEV]);
287     pr("Research <R>: %.2f\n", np->nat_level[NAT_RLEV]);
288     pr("Education <E>: %.2f\t\t", np->nat_level[NAT_ELEV]);
289     pr("Happiness <H>: %.2f\n", np->nat_level[NAT_HLEV]);
290     pr("Money <M>: $%6d\n", np->nat_money);
291     pr("Telegrams <t>: %6d\n", np->nat_tgms);
292     if (opt_HIDDEN) {
293         pr("Countries contacted: ");
294         for (i = 0; i < MAXNOC; i++) {
295             if (getcontact(np, i))
296                 pr("%d(%d) ", i, getcontact(np, i));
297         }
298         pr("\n");
299     }
300 }
301
302 static void
303 print_plane(struct plnstr *plane)
304 {
305     pr("%s %s\n", prnatid(plane->pln_own), prplane(plane));
306     pr("UID <U>: %d\t\t", plane->pln_uid);
307     pr("Owner <O>: %d\t\t", plane->pln_own);
308     pr("Location <l>: %s\n",
309        xyas(plane->pln_x, plane->pln_y, player->cnum));
310     pr("Efficiency <e>: %d\t", plane->pln_effic);
311     pr("Mobility <m>: %d\n", plane->pln_mobil);
312     pr("Tech <t>: %d\t\t", plane->pln_tech);
313     pr("Wing <w>: %.1s\n", &plane->pln_wing);
314     pr("Range <r>: %d\t\t", plane->pln_range);
315     pr("Flags <f>: %d\n", plane->pln_flags);
316     pr("Ship <s>: %d\t\t", plane->pln_ship);
317     pr("Land Unit <y>: %d\t", plane->pln_land);
318 }
319
320 static void
321 print_land(struct lndstr *land)
322 {
323     pr("%s %s\n", prnatid(land->lnd_own), prland(land));
324     pr("UID <U>: %d\n", land->lnd_uid);
325     pr("Owner <O>: %d\n", land->lnd_own);
326     pr("Location <L>: %s\n", xyas(land->lnd_x, land->lnd_y, player->cnum));
327     pr("Efficiency <e>: %d\t", land->lnd_effic);
328     pr("Mobility <M>: %d\n", land->lnd_mobil);
329     pr("Tech <t>: %d\t\t", land->lnd_tech);
330     pr("Army <a>: %.1s\n", &land->lnd_army);
331     pr("Fortification <F>: %d\t", land->lnd_harden);
332     pr("Land unit <Y>: %d\n", land->lnd_land);
333     pr("Ship <S>: %d\t\t", land->lnd_ship);
334     pr("Retreat percentage <Z>: %d\n", land->lnd_retreat);
335     pr("Retreat path <R>: '%s'\t\tRetreat Flags <W>: %d\n",
336        land->lnd_rpath, land->lnd_rflags);
337     pr("civ mil  uw food shl gun  pet  irn  dst  oil  lcm  hcm rad\n");
338     pr("  c   m   u    f   s   g    p    i    d    o    l    h   r\n");
339     pr("%3d", land->lnd_item[I_CIVIL]);
340     pr("%4d", land->lnd_item[I_MILIT]);
341     pr("%4d", land->lnd_item[I_UW]);
342     pr("%5d", land->lnd_item[I_FOOD]);
343     pr("%4d", land->lnd_item[I_SHELL]);
344     pr("%4d", land->lnd_item[I_GUN]);
345     pr("%5d", land->lnd_item[I_PETROL]);
346     pr("%5d", land->lnd_item[I_IRON]);
347     pr("%5d", land->lnd_item[I_DUST]);
348     pr("%5d", land->lnd_item[I_OIL]);
349     pr("%5d", land->lnd_item[I_LCM]);
350     pr("%5d", land->lnd_item[I_HCM]);
351     pr("%4d", land->lnd_item[I_RAD]);
352     pr("\n");
353 }
354
355 static void
356 print_ship(struct shpstr *ship)
357 {
358     pr("%s %s\n", prnatid(ship->shp_own), prship(ship));
359     pr("UID <U>: %d\n", ship->shp_uid);
360     pr("Owner <O>: %d\t\t\t", ship->shp_own);
361     pr("Location <L>: %s\n", xyas(ship->shp_x, ship->shp_y, player->cnum));
362     pr("Tech <T>: %d\t\t\t", ship->shp_tech);
363     pr("Efficiency <E>: %d\n", ship->shp_effic);
364     pr("Mobility <M>: %d\t\t", ship->shp_mobil);
365     pr("Fleet <F>: %.1s\n", &ship->shp_fleet);
366     pr("Retreat path <R>: '%s'\t\tRetreat Flags <W>: %d\n",
367        ship->shp_rpath, ship->shp_rflags);
368     pr("Plague Stage <a>: %d\t\t", ship->shp_pstage);
369     pr("Plague Time <b>: %d\n", ship->shp_ptime);
370     pr("civ mil  uw food shl gun  pet  irn  dst  oil  lcm  hcm rad\n");
371     pr("  c   m   u    f   s   g    p    i    d    o    l    h   r\n");
372     pr("%3d", ship->shp_item[I_CIVIL]);
373     pr("%4d", ship->shp_item[I_MILIT]);
374     pr("%4d", ship->shp_item[I_UW]);
375     pr("%5d", ship->shp_item[I_FOOD]);
376     pr("%4d", ship->shp_item[I_SHELL]);
377     pr("%4d", ship->shp_item[I_GUN]);
378     pr("%5d", ship->shp_item[I_PETROL]);
379     pr("%5d", ship->shp_item[I_IRON]);
380     pr("%5d", ship->shp_item[I_DUST]);
381     pr("%5d", ship->shp_item[I_OIL]);
382     pr("%5d", ship->shp_item[I_LCM]);
383     pr("%5d", ship->shp_item[I_HCM]);
384     pr("%4d", ship->shp_item[I_RAD]);
385     pr("\n");
386 }
387
388 static char *
389 getin(char *buf, char **valp)
390 {
391     char line[1024];
392     char *argp[128];
393     char *p;
394
395     *valp = NULL;
396     p = getstarg(NULL, "%c xxxxx -- thing value : ", line);
397     if (!p)
398         return NULL;
399     switch (parse(p, buf, argp, NULL, NULL, NULL)) {
400     case 0:
401         return "";
402     case 1:
403         return NULL;
404     default:
405         *valp = argp[1];
406         return argp[0];
407     }
408 }
409
410 #if 0   /* not needed right now */
411 static void
412 warn_deprecated(char key)
413 {
414     pr("Key <%c> is deprecated and will go away in a future release\n", key);
415 }
416 #endif
417
418 static int
419 edit_sect_i(struct sctstr *sect, char *key, int arg)
420 {
421     int new;
422
423     switch (*key) {
424     case 'o':
425         if (arg < 0 || arg >= MAXNOC)
426             return RET_SYN;
427         pr("Owner of %s changed from %s to %s.\n",
428            xyas(sect->sct_x, sect->sct_y, player->cnum),
429            prnatid(sect->sct_own), prnatid(arg));
430         if (arg == sect->sct_own)
431             break;
432         report_god_takes("Sector ",
433                          xyas(sect->sct_x, sect->sct_y, sect->sct_own),
434                          sect->sct_own);
435         report_god_gives("Sector ",
436                          xyas(sect->sct_x, sect->sct_y, arg),
437                          arg);
438         sect->sct_own = arg;
439         break;
440     case 'O':
441         if (arg < 0 || arg >= MAXNOC)
442             return RET_SYN;
443         pr("Old owner of %s changed from %s to %s.\n",
444            xyas(sect->sct_x, sect->sct_y, player->cnum),
445            prnatid(sect->sct_oldown), prnatid(arg));
446         if (arg == sect->sct_oldown)
447             break;
448         if (sect->sct_own && sect->sct_own != player->cnum)
449             wu(0, sect->sct_own,
450                "Old owner of %s changed from %s to %s by an act of %s\n",
451                xyas(sect->sct_x, sect->sct_y, player->cnum),
452                prnatid(sect->sct_oldown), prnatid(arg),
453                cname(player->cnum));
454         sect->sct_oldown = arg;
455         break;
456     case 'e':
457         new = LIMIT_TO(arg, 0, 100);
458         noise(sect, "Efficiency", sect->sct_effic, new);
459         sect->sct_effic = new;
460         break;
461     case 'm':
462         new = LIMIT_TO(arg, -127, 127);
463         noise(sect, "Mobility", sect->sct_mobil, new);
464         sect->sct_mobil = new;
465         break;
466     case 'i':
467         new = LIMIT_TO(arg, 0, 100);
468         noise(sect, "Iron ore content", sect->sct_min, new);
469         sect->sct_min = (unsigned char)new;
470         break;
471     case 'g':
472         new = LIMIT_TO(arg, 0, 100);
473         noise(sect, "Gold content", sect->sct_gmin, new);
474         sect->sct_gmin = (unsigned char)new;
475         break;
476     case 'f':
477         new = LIMIT_TO(arg, 0, 100);
478         noise(sect, "Fertility", sect->sct_fertil, new);
479         sect->sct_fertil = (unsigned char)new;
480         break;
481     case 'c':
482         new = LIMIT_TO(arg, 0, 100);
483         noise(sect, "Oil content", sect->sct_oil, new);
484         sect->sct_oil = (unsigned char)new;
485         break;
486     case 'u':
487         new = LIMIT_TO(arg, 0, 100);
488         noise(sect, "Uranium content", sect->sct_uran, new);
489         sect->sct_uran = (unsigned char)new;
490         break;
491     case 'w':
492         new = LIMIT_TO(arg, 0, 100);
493         noise(sect, "Workforce percentage", sect->sct_work, new);
494         sect->sct_work = (unsigned char)new;
495         break;
496     case 'l':
497         new = LIMIT_TO(arg, 0, 127);
498         pr("Loyalty of %s changed from %d to %d\n",
499            xyas(sect->sct_x, sect->sct_y, player->cnum),
500            sect->sct_loyal, new);
501         sect->sct_loyal = (unsigned char)new;
502         break;
503     case 'x':
504         new = LIMIT_TO(arg, 0, CHE_MAX);
505         pr("Guerillas in %s changed from %d to %d\n",
506            xyas(sect->sct_x, sect->sct_y, player->cnum),
507            sect->sct_che, new);
508         sect->sct_che = new;
509         break;
510     case 'X':
511         if (arg < 0 || arg >= MAXNOC)
512             return RET_SYN;
513         pr("Che target of %s changed from %s to %s.\n",
514            xyas(sect->sct_x, sect->sct_y, player->cnum),
515            prnatid(sect->sct_che_target), prnatid(arg));
516         sect->sct_che_target = arg;
517         if (arg == 0)
518             sect->sct_che = 0;
519         break;
520     case 'p':
521         new = LIMIT_TO(arg, 0, PLG_EXPOSED);
522         pr("Plague stage of %s changed from %d to %d\n",
523            xyas(sect->sct_x, sect->sct_y, player->cnum),
524            sect->sct_pstage, new);
525         sect->sct_pstage = new;
526         break;
527     case 't':
528         new = LIMIT_TO(arg, 0, 32767);
529         pr("Plague time of %s changed from %d to %d\n",
530            xyas(sect->sct_x, sect->sct_y, player->cnum),
531            sect->sct_ptime, new);
532         sect->sct_ptime = new;
533         break;
534     case 'F':
535         new = LIMIT_TO(arg, 0, FALLOUT_MAX);
536         noise(sect, "Fallout", sect->sct_fallout, new);
537         sect->sct_fallout = new;
538         break;
539     case 'a':
540         new = LIMIT_TO(arg, 0, 9999);
541         noise(sect, "Available workforce", sect->sct_avail, new);
542         sect->sct_avail = new;
543         break;
544     case 'M':
545         new = LIMIT_TO(arg, 0, MINES_MAX);
546         if (sect->sct_own == sect->sct_oldown)
547             noise(sect, "Mines", sect->sct_mines, new);
548         else
549             pr("Mines of %s changed from %d to %d\n",
550                xyas(sect->sct_x, sect->sct_y, player->cnum),
551                sect->sct_mines, new);
552         sect->sct_mines = new;
553         break;
554     case 'R':
555         new = LIMIT_TO(arg, 0, 100);
556         noise(sect, "Road percentage", sect->sct_road, new);
557         sect->sct_road = new;
558         break;
559     case 'r':
560         new = LIMIT_TO(arg, 0, 100);
561         noise(sect, "Rail percentage", sect->sct_rail, new);
562         sect->sct_rail = new;
563         break;
564     case 'd':
565         new = LIMIT_TO(arg, 0, 100);
566         noise(sect, "Defense percentage", sect->sct_defense, new);
567         sect->sct_defense = new;
568         break;
569     default:
570         pr("huh? (%s)\n", key);
571         return RET_SYN;
572     }
573     return RET_OK;
574 }
575
576 static int
577 edit_sect(struct sctstr *sect, char *key, char *p)
578 {
579     coord newx, newy;
580     int new;
581     struct sctstr newsect;
582
583     switch (*key) {
584     case 'L':
585         if (!sarg_xy(p, &newx, &newy))
586             return RET_SYN;
587         getsect(newx, newy, &newsect);
588         pr("Sector %s duplicated to %s\n",
589            xyas(sect->sct_x, sect->sct_y, player->cnum),
590            xyas(newx, newy, player->cnum));
591         report_god_takes("Sector ", xyas(newx, newy, newsect.sct_own),
592                          newsect.sct_own);
593         report_god_gives("Sector ", xyas(newx, newy, sect->sct_own),
594                          sect->sct_own);
595         sect->sct_x = newx;
596         sect->sct_y = newy;
597         ef_set_uid(EF_SECTOR, &sect, XYOFFSET(newx, newy));
598         break;
599     case 'D':
600         if (!sarg_xy(p, &newx, &newy))
601             return RET_SYN;
602         pr("Distribution sector of sector %s changed from %s to %s\n",
603            xyas(sect->sct_x, sect->sct_y, player->cnum),
604            xyas(sect->sct_dist_x, sect->sct_dist_y, player->cnum),
605            xyas(newx, newy, player->cnum));
606         if (newx == sect->sct_dist_x && newy == sect->sct_dist_y)
607             break;
608         if (sect->sct_own && sect->sct_own != player->cnum)
609             wu(0, sect->sct_own,
610                "Distribution sector of sector %s changed from %s to %s"
611                " by an act of %s\n",
612                xyas(sect->sct_x, sect->sct_y, player->cnum),
613                xyas(sect->sct_dist_x, sect->sct_dist_y, player->cnum),
614                xyas(newx, newy, player->cnum),
615                cname(player->cnum));
616         sect->sct_dist_x = newx;
617         sect->sct_dist_y = newy;
618         break;
619     case 's':
620         new = sct_typematch(p);
621         if (new < 0)
622             return RET_SYN;
623         pr("Designation of sector %s changed from %c to %c\n",
624            xyas(sect->sct_x, sect->sct_y, player->cnum),
625            dchr[sect->sct_type].d_mnem, dchr[new].d_mnem);
626         if (new == sect->sct_type)
627             break;
628         if (sect->sct_own && sect->sct_own != player->cnum)
629             wu(0, sect->sct_own,
630                "Designation of sector %s changed from %c to %c"
631                " by an act of %s\n",
632                xyas(sect->sct_x, sect->sct_y, player->cnum),
633                dchr[sect->sct_type].d_mnem, dchr[new].d_mnem,
634                cname(player->cnum));
635         set_coastal(sect, sect->sct_type, new);
636         sect->sct_type = new;
637         break;
638     case 'S':
639         new = sct_typematch(p);
640         if (new < 0)
641             return RET_SYN;
642         pr("New designation of sector %s changed from %c to %c\n",
643            xyas(sect->sct_x, sect->sct_y, player->cnum),
644            dchr[sect->sct_newtype].d_mnem, dchr[new].d_mnem);
645         if (new == sect->sct_newtype)
646             break;
647         if (sect->sct_own && sect->sct_own != player->cnum)
648             wu(0, sect->sct_own,
649                "New designation of sector %s changed from %c to %c"
650                " by an act of %s\n",
651                xyas(sect->sct_x, sect->sct_y, player->cnum),
652                dchr[sect->sct_newtype].d_mnem, dchr[new].d_mnem,
653                cname(player->cnum));
654         sect->sct_newtype = new;
655         break;
656     default:
657         return edit_sect_i(sect, key, atoi(p));
658     }
659     return RET_OK;
660 }
661
662 static int
663 edit_nat(struct natstr *np, char *key, char *p)
664 {
665     coord newx, newy;
666     natid nat = np->nat_cnum;
667     int arg = atoi(p);
668     float farg = (float)atof(p);
669
670     switch (*key) {
671     case 'n':
672         if (!check_nat_name(p, nat))
673             return RET_SYN;
674         pr("Country name changed from %s to %s\n", np->nat_cnam, p);
675         strcpy(np->nat_cnam, p);
676         break;
677     case 'r':
678         pr("Country representative changed from %s to %s\n",
679            np->nat_pnam, p);
680         strncpy(np->nat_pnam, p, sizeof(np->nat_pnam) - 1);
681         break;
682     case 't':
683         arg = LIMIT_TO(arg, 0, USHRT_MAX);
684         np->nat_tgms = arg;
685         break;
686     case 'b':
687         arg = LIMIT_TO(arg, 0, max_btus);
688         pr("BTU's changed from %d to %d\n", np->nat_btu, arg);
689         np->nat_btu = arg;
690         break;
691     case 'm':
692         arg = LIMIT_TO(arg, 0, INT_MAX);
693         benefit(nat, arg - np->nat_reserve);
694         pr("Military reserves changed from %d to %d\n",
695            np->nat_reserve, arg);
696         if (arg == np->nat_reserve)
697             break;
698         if (nat != player->cnum)
699             wu(0, nat,
700                "Military reserves changed from %d to %d by an act of %s\n",
701                np->nat_reserve, arg, cname(player->cnum));
702         np->nat_reserve = arg;
703         break;
704     case 'c':
705         if (!sarg_xy(p, &newx, &newy))
706             return RET_SYN;
707         pr("Capital coordinates changed from %s to %s\n",
708            xyas(np->nat_xcap, np->nat_ycap, player->cnum),
709            xyas(newx, newy, player->cnum));
710         np->nat_xcap = newx;
711         np->nat_ycap = newy;
712         break;
713     case 'o':
714         if (!sarg_xy(p, &newx, &newy))
715             return RET_SYN;
716         pr("Origin coordinates changed from %s to %s\n",
717            xyas(np->nat_xorg, np->nat_yorg, player->cnum),
718            xyas(newx, newy, player->cnum));
719         np->nat_xorg = newx;
720         np->nat_yorg = newy;
721         break;
722     case 's':
723         np->nat_stat = LIMIT_TO(arg, STAT_UNUSED, STAT_GOD);
724         break;
725     case 'u':
726         arg = LIMIT_TO(arg, 0, m_m_p_d * 60);
727         pr("Number of seconds used changed from %d to %d.\n",
728            np->nat_timeused, arg);
729         np->nat_timeused = arg;
730         break;
731     case 'M':
732         pr("Money changed from %d to %d\n", np->nat_money, arg);
733         if (arg == np->nat_money)
734             break;
735         if (nat != player->cnum)
736             wu(0, nat, "Money changed from %d to %d by an act of %s\n",
737                np->nat_money, arg, cname(player->cnum));
738         np->nat_money = arg;
739         break;
740     case 'T':
741         farg = MAX(0.0, farg);
742         pr("Tech changed from %.2f to %.2f.\n",
743            np->nat_level[NAT_TLEV], farg);
744         np->nat_level[NAT_TLEV] = farg;
745         break;
746     case 'R':
747         farg = MAX(0.0, farg);
748         pr("Research changed from %.2f to %.2f.\n",
749            np->nat_level[NAT_RLEV], farg);
750         np->nat_level[NAT_RLEV] = farg;
751         break;
752     case 'E':
753         farg = MAX(0.0, farg);
754         pr("Education changed from %.2f to %.2f.\n",
755            np->nat_level[NAT_ELEV], farg);
756         np->nat_level[NAT_ELEV] = farg;
757         break;
758     case 'H':
759         farg = MAX(0.0, farg);
760         pr("Happiness changed from %.2f to %.2f.\n",
761            np->nat_level[NAT_HLEV], farg);
762         np->nat_level[NAT_HLEV] = farg;
763         break;
764     default:
765         pr("huh? (%s)\n", key);
766         break;
767     }
768     putnat(np);
769     return RET_OK;
770 }
771
772 static int
773 edit_ship(struct shpstr *ship, char *key, char *p)
774 {
775     struct mchrstr *mcp = &mchr[ship->shp_type];
776     int arg = atoi(p);
777     coord newx, newy;
778     struct ichrstr *ip;
779
780     newx = newy = 0;
781     switch (*key) {
782     case 'a':
783         arg = LIMIT_TO(arg, 0, PLG_EXPOSED);
784         ship->shp_pstage = arg;
785         break;
786     case 'b':
787         arg = LIMIT_TO(arg, 0, 32767);
788         ship->shp_ptime = arg;
789         break;
790     case 'R':
791         strncpy(ship->shp_rpath, p, sizeof(ship->shp_rpath) - 1);
792         break;
793     case 'W':
794         ship->shp_rflags = arg;
795         break;
796     case 'U':
797         ef_set_uid(EF_SHIP, ship, arg);
798         break;
799     case 'O':
800         if (arg < 0 || arg >= MAXNOC)
801             return RET_SYN;
802         if (arg == ship->shp_own)
803             break;
804         if (ship->shp_own && ship->shp_own != player->cnum)
805             wu(0, ship->shp_own, "%s taken from you by an act of %s!\n",
806                prship(ship), cname(player->cnum));
807         if (arg && arg != player->cnum)
808             wu(0, arg, "%s given to you by an act of %s!\n",
809                prship(ship), cname(player->cnum));
810         ship->shp_own = arg;
811         break;
812     case 'L':
813         if (!sarg_xy(p, &newx, &newy))
814             return RET_SYN;
815         ship->shp_x = newx;
816         ship->shp_y = newy;
817         break;
818     case 'T':
819         arg = LIMIT_TO(arg, mcp->m_tech, SHRT_MAX);
820         shp_set_tech(ship, arg);
821         break;
822     case 'E':
823         ship->shp_effic = LIMIT_TO(arg, SHIP_MINEFF, 100);
824         break;
825     case 'M':
826         arg = LIMIT_TO(arg, -127, 127);
827         ship->shp_mobil = arg;
828         break;
829     case 'F':
830         if (p[0] == '~')
831             ship->shp_fleet = 0;
832         else if (isalpha(p[0]))
833             ship->shp_fleet = p[0];
834         else {
835             pr("%c: invalid fleet\n", p[0]);
836             return RET_FAIL;
837         }
838         break;
839     case 'c':
840     case 'm':
841     case 'u':
842     case 'f':
843     case 's':
844     case 'g':
845     case 'p':
846     case 'i':
847     case 'd':
848     case 'o':
849     case 'l':
850     case 'h':
851     case 'r':
852         ip = item_by_name(key);
853         arg = LIMIT_TO(arg, 0, mchr[ship->shp_type].m_item[ip->i_uid]);
854         ship->shp_item[ip->i_uid] = arg;
855         break;
856     default:
857         pr("huh? (%s)\n", key);
858         return RET_FAIL;
859     }
860     return RET_OK;
861 }
862
863 static int
864 edit_land(struct lndstr *land, char *key, char *p)
865 {
866     struct lchrstr *lcp = &lchr[land->lnd_type];
867     int arg = atoi(p);
868     coord newx, newy;
869     struct ichrstr *ip;
870
871     newx = newy = 0;
872     switch (*key) {
873     case 'Y':
874         if (arg < -1 || arg >= ef_nelem(EF_LAND))
875             return RET_SYN;
876         if (arg >= 0 && arg != land->lnd_land)
877             land->lnd_ship = -1;
878         land->lnd_land = arg;
879         break;
880     case 'U':
881         ef_set_uid(EF_LAND, land, arg);
882         break;
883     case 'O':
884         if (arg < 0 || arg >= MAXNOC)
885             return RET_SYN;
886         if (arg == land->lnd_own)
887             break;
888         if (land->lnd_own && land->lnd_own != player->cnum)
889             wu(0, land->lnd_own, "%s taken from you by an act of %s!\n",
890                prland(land), cname(player->cnum));
891         if (arg && arg != player->cnum)
892             wu(0, arg, "%s given to you by an act of %s!\n",
893                prland(land), cname(player->cnum));
894         land->lnd_own = arg;
895         break;
896     case 'L':
897         if (!sarg_xy(p, &newx, &newy))
898             return RET_SYN;
899         land->lnd_x = newx;
900         land->lnd_y = newy;
901         break;
902     case 'e':
903         land->lnd_effic = LIMIT_TO(arg, LAND_MINEFF, 100);
904         break;
905     case 'M':
906         arg = LIMIT_TO(arg, -127, 127);
907         land->lnd_mobil = arg;
908         break;
909     case 't':
910         arg = LIMIT_TO(arg, lcp->l_tech, SHRT_MAX);
911         lnd_set_tech(land, arg);
912         break;
913     case 'a':
914         if (p[0] == '~')
915             land->lnd_army = 0;
916         else if (isalpha(p[0]))
917             land->lnd_army = p[0];
918         else {
919             pr("%c: invalid army\n", p[0]);
920             return RET_FAIL;
921         }
922         break;
923     case 'F':
924         land->lnd_harden = LIMIT_TO(arg, 0, 127);
925         break;
926     case 'S':
927         if (arg < -1 || arg >= ef_nelem(EF_SHIP))
928             return RET_SYN;
929         if (arg >= 0 && arg != land->lnd_ship)
930             land->lnd_land = -1;
931         land->lnd_ship = arg;
932         break;
933     case 'Z':
934         arg = LIMIT_TO(arg, 0, 100);
935         land->lnd_retreat = arg;
936         break;
937     case 'R':
938         strncpy(land->lnd_rpath, p, sizeof(land->lnd_rpath) - 1);
939         break;
940     case 'W':
941         land->lnd_rflags = arg;
942         break;
943     case 'c':
944     case 'm':
945     case 'u':
946     case 'f':
947     case 's':
948     case 'g':
949     case 'p':
950     case 'i':
951     case 'd':
952     case 'o':
953     case 'l':
954     case 'h':
955     case 'r':
956         ip = item_by_name(key);
957         arg = LIMIT_TO(arg, 0, lchr[land->lnd_type].l_item[ip->i_uid]);
958         land->lnd_item[ip->i_uid] = arg;
959         break;
960     default:
961         pr("huh? (%s)\n", key);
962         return RET_FAIL;
963     }
964     return RET_OK;
965 }
966
967 static int
968 edit_plane(struct plnstr *plane, char *key, char *p)
969 {
970     struct plchrstr *pcp = &plchr[plane->pln_type];
971     int arg = atoi(p);
972     coord newx, newy;
973
974     switch (*key) {
975     case 'U':
976         ef_set_uid(EF_PLANE, plane, arg);
977         break;
978     case 'l':
979         if (!sarg_xy(p, &newx, &newy))
980             return RET_SYN;
981         plane->pln_x = newx;
982         plane->pln_y = newy;
983         break;
984     case 'O':
985         if (arg < 0 || arg >= MAXNOC)
986             return RET_SYN;
987         if (arg == plane->pln_own)
988             break;
989         if (plane->pln_own && plane->pln_own != player->cnum)
990             wu(0, plane->pln_own, "%s taken from you by an act of %s!\n",
991                prplane(plane), cname(player->cnum));
992         if (arg && arg != player->cnum)
993             wu(0, arg, "%s given to you by an act of %s!\n",
994                prplane(plane), cname(player->cnum));
995         plane->pln_own = arg;
996         break;
997     case 'e':
998         plane->pln_effic = LIMIT_TO(arg, PLANE_MINEFF, 100);
999         break;
1000     case 'm':
1001         plane->pln_mobil = LIMIT_TO(arg, -127, 127);
1002         break;
1003     case 't':
1004         arg = LIMIT_TO(arg, pcp->pl_tech, SHRT_MAX);
1005         pln_set_tech(plane, arg);
1006         break;
1007     case 'w':
1008         if (p[0] == '~')
1009             plane->pln_wing = 0;
1010         else if (isalpha(p[0]))
1011             plane->pln_wing = p[0];
1012         else {
1013             pr("%c: invalid wing\n", p[0]);
1014             return RET_FAIL;
1015         }
1016         break;
1017     case 'r':
1018         arg = LIMIT_TO(arg, 0, pl_range(pcp, plane->pln_tech));
1019         plane->pln_range = (unsigned char)arg;
1020         break;
1021     case 's':
1022         if (arg < -1 || arg >= ef_nelem(EF_SHIP))
1023             return RET_SYN;
1024         if (arg >= 0 && arg != plane->pln_ship)
1025             plane->pln_land = -1;
1026         plane->pln_ship = arg;
1027         break;
1028     case 'y':
1029         if (arg < -1 || arg >= ef_nelem(EF_LAND))
1030             return RET_SYN;
1031         if (arg >= 0 && arg != plane->pln_land)
1032             plane->pln_ship = -1;
1033         plane->pln_land = arg;
1034         break;
1035     case 'f':
1036         plane->pln_flags = arg;
1037         break;
1038     default:
1039         pr("huh? (%s)\n", key);
1040         return RET_FAIL;
1041     }
1042     return RET_OK;
1043 }