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