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