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