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