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