]> 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-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  edit.c: Edit things (sectors, ships, planes, units, nukes, countries)
28  *
29  *  Known contributors to this file:
30  *     David Muir Sharnoff
31  *     Chad Zabel, 1994
32  *     Steve McClure, 1998-2000
33  *     Ron Koenderink, 2003-2009
34  *     Markus Armbruster, 2003-2013
35  */
36
37 #include <config.h>
38
39 #include <ctype.h>
40 #include <limits.h>
41 #include "actofgod.h"
42 #include "commands.h"
43 #include "item.h"
44 #include "land.h"
45 #include "news.h"
46 #include "optlist.h"
47 #include "plague.h"
48 #include "plane.h"
49 #include "ship.h"
50 #include "unit.h"
51
52 static void print_sect(struct sctstr *);
53 static void print_nat(struct natstr *);
54 static void print_plane(struct plnstr *);
55 static void print_land(struct lndstr *);
56 static void print_ship(struct shpstr *);
57 static 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>: %-20s\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         sect->sct_x = newx;
563         sect->sct_y = newy;
564         ef_set_uid(EF_SECTOR, sect, XYOFFSET(newx, newy));
565         break;
566     case 'D':
567         if (!sarg_xy(p, &newx, &newy))
568             return RET_SYN;
569         divine_sct_change_quiet(sect, "Distribution sector",
570                 newx != sect->sct_dist_x || newy != sect->sct_dist_y,
571                 "from %s to %s",
572                 xyas(sect->sct_dist_x, sect->sct_dist_y, player->cnum),
573                 xyas(newx, newy, player->cnum));
574         if (newx == sect->sct_dist_x && newy == sect->sct_dist_y)
575             break;
576         if (sect->sct_own && sect->sct_own != player->cnum)
577             wu(0, sect->sct_own,
578                "Distribution sector of %s changed from %s to %s"
579                " by an act of %s\n",
580                xyas(sect->sct_x, sect->sct_y, player->cnum),
581                xyas(sect->sct_dist_x, sect->sct_dist_y, player->cnum),
582                xyas(newx, newy, player->cnum),
583                cname(player->cnum));
584         sect->sct_dist_x = newx;
585         sect->sct_dist_y = newy;
586         break;
587     case 's':
588         new = sct_typematch(p);
589         if (new < 0)
590             return RET_SYN;
591         divine_sct_change(sect, "Designation",
592                           new != sect->sct_type, 0, "from %c to %c",
593                           dchr[sect->sct_type].d_mnem, dchr[new].d_mnem);
594         set_coastal(sect, sect->sct_type, new);
595         sect->sct_type = new;
596         break;
597     case 'S':
598         new = sct_typematch(p);
599         if (new < 0)
600             return RET_SYN;
601         divine_sct_change(sect, "New designation",
602                           new != sect->sct_newtype, 0, "from %c to %c",
603                           dchr[sect->sct_newtype].d_mnem, dchr[new].d_mnem);
604         sect->sct_newtype = new;
605         break;
606     default:
607         return edit_sect_i(sect, key, atoi(p));
608     }
609     return RET_OK;
610 }
611
612 static void
613 edit_level(struct natstr *np, int lvl, char *name, char *p)
614 {
615     float new = (float)atof(p);
616
617     new = MAX(0.0, new);
618     divine_nat_change(np, name,
619                       new != np->nat_level[lvl],
620                       (new > np->nat_level[lvl]) - (new < np->nat_level[lvl]),
621                       "from %.2f to %.2f", np->nat_level[lvl], new);
622     np->nat_level[lvl] = new;
623 }
624
625 static int
626 edit_nat(struct natstr *np, char *key, char *p)
627 {
628     coord newx, newy;
629     natid nat = np->nat_cnum;
630     int arg = atoi(p);
631
632     switch (*key) {
633     case 'n':
634         if (!check_nat_name(p, nat))
635             return RET_SYN;
636         divine_nat_change(np, "Country name", strcmp(np->nat_cnam, p), 0,
637                           "from %s to %s", np->nat_cnam, p);
638         if (opt_GODNEWS)
639             nreport(player->cnum, N_NAME_CHNG, 0, 1);
640         strcpy(np->nat_cnam, p);
641         break;
642     case 'r':
643         divine_nat_change(np, "Country representative",
644                 strncmp(p, np->nat_pnam, sizeof(np->nat_pnam) - 1), 0,
645                 "from %s to %.*s",
646                 np->nat_pnam, (int)sizeof(np->nat_pnam) - 1, p);
647         strncpy(np->nat_pnam, p, sizeof(np->nat_pnam) - 1);
648         break;
649     case 't':
650         arg = LIMIT_TO(arg, 0, USHRT_MAX);
651         divine_nat_change_quiet(np, "Number of unread telegrams",
652                                 arg != np->nat_tgms,
653                                 "from %d to %d", np->nat_tgms, arg);
654         np->nat_tgms = arg;
655         break;
656     case 'b':
657         arg = LIMIT_TO(arg, 0, max_btus);
658         divine_nat_change(np, "BTUs",
659                           arg != np->nat_btu, arg - np->nat_btu,
660                           "from %d to %d", np->nat_btu, arg);
661         np->nat_btu = arg;
662         break;
663     case 'm':
664         arg = LIMIT_TO(arg, 0, INT_MAX);
665         divine_nat_change(np, "Military reserves",
666                           arg != np->nat_reserve, arg - np->nat_reserve,
667                           "from %d to %d", np->nat_reserve, arg);
668         np->nat_reserve = arg;
669         break;
670     case 'c':
671         if (!sarg_xy(p, &newx, &newy))
672             return RET_SYN;
673         if (newx == np->nat_xcap && newy == np->nat_ycap)
674             pr("Capital unchanged\n");
675         else {
676             pr("Capital moved from %s to %s\n",
677                xyas(np->nat_xcap, np->nat_ycap, player->cnum),
678                xyas(newx, newy, player->cnum));
679             if (nat != player->cnum)
680                 wu(0, nat,
681                    "Capital moved from %s to %s by an act of %s!\n",
682                    xyas(np->nat_xcap, np->nat_ycap, nat),
683                    xyas(newx, newy, nat), cname(player->cnum));
684         }
685         np->nat_xcap = newx;
686         np->nat_ycap = newy;
687         break;
688     case 'o':
689         if (!sarg_xy(p, &newx, &newy))
690             return RET_SYN;
691         if (newx == np->nat_xorg && newy == np->nat_yorg)
692             pr("Origin unchanged\n");
693         else {
694             pr("Origin moved from %s to %s\n",
695                xyas(np->nat_xorg, np->nat_yorg, player->cnum),
696                xyas(newx, newy, player->cnum));
697             if (nat != player->cnum)
698                 wu(0, nat,
699                    "Origin moved from %s to %s by an act of %s!\n",
700                    xyas(np->nat_xorg, np->nat_yorg, nat),
701                    xyas(newx, newy, nat), cname(player->cnum));
702         }
703         np->nat_xorg = newx;
704         np->nat_yorg = newy;
705         break;
706     case 's':
707         arg = LIMIT_TO(arg, STAT_UNUSED, STAT_GOD);
708         divine_nat_change(np, "Status",
709                           (enum nat_status)arg != np->nat_stat,
710                           0, "to %s", nation_status[arg].name);
711         np->nat_stat = arg;
712         break;
713     case 'u':
714         arg = LIMIT_TO(arg, 0, m_m_p_d * 60);
715         divine_nat_change(np, "Number of seconds used",
716                           arg != np->nat_timeused, arg - np->nat_timeused,
717                           "from %d to %d", np->nat_timeused, arg);
718         np->nat_timeused = arg;
719         break;
720     case 'M':
721         divine_nat_change(np, "Money",
722                           arg != np->nat_money, arg - np->nat_money,
723                           "from %d to %d", np->nat_money, arg);
724         np->nat_money = arg;
725         break;
726     case 'T':
727         edit_level(np, NAT_TLEV, "Technology", p);
728         break;
729     case 'R':
730         edit_level(np, NAT_RLEV, "Research", p);
731         break;
732     case 'E':
733         edit_level(np, NAT_ELEV, "Education", p);
734         break;
735     case 'H':
736         edit_level(np, NAT_HLEV, "Happiness", p);
737         break;
738     default:
739         pr("huh? (%s)\n", key);
740         break;
741     }
742     return RET_OK;
743 }
744
745 static int
746 edit_unit(struct empobj *unit, char *key, char *p,
747           int mineff, char *group_name, int on_carrier)
748 {
749     int arg = atoi(p);
750     coord newx, newy;
751     union empobj_storage newunit;
752     char newgroup;
753  
754     switch (toupper(*key)) {
755     case 'U':
756         if (arg < 0)
757             return RET_SYN;
758         if (arg == unit->uid) {
759             pr("%s unchanged\n", unit_nameof(unit));
760             break;
761         }
762         if (!ef_ensure_space(unit->ef_type, arg, 50)) {
763             pr("Can't copy to %s #%d\n", ef_nameof(unit->ef_type), arg);
764             return RET_FAIL;
765         }
766         pr("%s duplicated to (#%d)\n", unit_nameof(unit), arg);
767         ef_set_uid(unit->ef_type, unit, arg);
768         if (get_empobj(unit->ef_type, arg, &newunit) && newunit.gen.own) {
769             pr("Replacing %s of %s\n",
770                unit_nameof(&newunit.gen), prnatid(newunit.gen.own));
771             report_god_takes("", unit_nameof(&newunit.gen),
772                              newunit.gen.own);
773         }
774         report_god_gives("", unit_nameof(unit), unit->own);
775         break;
776     case 'O':
777         if (arg < 0 || arg >= MAXNOC)
778             return RET_SYN;
779         divine_unit_change_quiet(unit, "Owner", arg != unit->own,
780                                  "from %s to %s",
781                                  prnatid(unit->own), prnatid(arg));
782         if (arg != unit->own) {
783             report_god_takes("", unit_nameof(unit), unit->own);
784             report_god_gives("", unit_nameof(unit), arg);
785         }
786         if (arg && unit->effic < mineff) {
787             divine_unit_change_quiet(unit, "Efficiency", 1,
788                                      "from %d to %d", unit->effic, mineff);
789             unit->effic = mineff;
790         }
791         unit->own = arg;
792         break;
793     case 'L':
794         if (!sarg_xy(p, &newx, &newy))
795             return RET_SYN;
796         if (on_carrier && (newx != unit->x || newy != unit->y)) {
797             pr("Can't move %s while it's loaded\n", unit_nameof(unit));
798             return RET_FAIL;
799         }
800         divine_unit_change_quiet(unit, "Location",
801                                  unit->own && unit->own != player->cnum,
802                                  "from %s to %s",
803                                  xyas(unit->x, unit->y, player->cnum),
804                                  xyas(newx, newy, player->cnum));
805         if (newx == unit->x && newy == unit->y)
806             break;
807         if (unit->own && unit->own != player->cnum)
808             wu(0, unit->own,
809                "Location of %s changed from %s to %s by an act of %s!\n",
810                unit_nameof(unit),
811                xyas(unit->x, unit->y, unit->own),
812                xyas(newx, newy, unit->own),
813                cname(player->cnum));
814         unit->x = newx;
815         unit->y = newy;
816         break;
817     case 'E':
818         arg = LIMIT_TO(arg, mineff, 100);
819         divine_unit_change(unit, "Efficiency",
820                            arg != unit->effic, arg - unit->effic,
821                            "from %d to %d", unit->effic, arg);
822         unit->effic = arg;
823         break;
824     case 'M':
825         arg = LIMIT_TO(arg, -127, 127);
826         divine_unit_change(unit, "Mobility",
827                            arg != unit->mobil, arg - unit->mobil,
828                            "from %d to %d", unit->mobil, arg);
829         unit->mobil = arg;
830         break;
831     case 'F':
832     case 'W':
833     case 'A':
834     case 'S':
835         if (p[0] == '~')
836             newgroup = 0;
837         else if (isalpha(p[0]))
838             newgroup = p[0];
839         else {
840             pr("%c: invalid %s\n", p[0], group_name);
841             return RET_FAIL;
842         }
843         divine_unit_change(unit, "Assignment", newgroup != unit->group, 0,
844                            "from %s %c to %c", group_name,
845                            unit->group ? unit->group : '~', p[0]);
846         unit->group = newgroup;
847         break;
848     default:
849         CANT_REACH();
850     }
851     return RET_OK;
852 }
853
854 static void
855 edit_item(struct empobj *unit, short item[], struct ichrstr *ip, int arg,
856           short lim[])
857 {
858     arg = LIMIT_TO(arg, 0, lim[ip->i_uid]);
859     divine_unit_change_quiet(unit, ip->i_name, arg != item[ip->i_uid],
860                              "from %d to %d", item[ip->i_uid], arg);
861     report_divine_gift(unit->own, ip, arg - item[ip->i_uid],
862                        unit_nameof(unit));
863     item[ip->i_uid] = arg;
864 }
865
866 static void
867 limit_item(struct empobj *unit, short item[], short lim[])
868 {
869     i_type it;
870
871     for (it = I_NONE + 1; it <= I_MAX; it++) {
872         if (item[it] > lim[it])
873             edit_item(unit, item, &ichr[it], item[it], lim);
874     }
875 }
876
877 static int
878 edit_ship(struct shpstr *ship, char *key, char *p)
879 {
880     struct mchrstr *mcp = &mchr[ship->shp_type];
881     int arg = atoi(p);
882
883     switch (*key) {
884     case 'U':
885     case 'O':
886     case 'L':
887     case 'E':
888     case 'M':
889     case 'F':
890         return edit_unit((struct empobj *)ship, key, p,
891                          SHIP_MINEFF, "fleet", 0);
892     case 't':
893         arg = ef_elt_byname(EF_SHIP_CHR, p);
894         if (arg < 0) {
895             pr("%s: invalid ship type\n", p);
896             return RET_FAIL;
897         }
898         divine_unit_change((struct empobj *)ship, "Type",
899                            arg != ship->shp_type, 0,
900                            "to %s", mchr[arg].m_name);
901         ship->shp_type = arg;
902         limit_item((struct empobj *)ship, ship->shp_item, mchr[arg].m_item);
903         if (ship->shp_tech >= mchr[arg].m_tech)
904             break;
905         arg = mchr[arg].m_tech;
906         /* fall through */
907     case 'T':
908         arg = LIMIT_TO(arg, mcp->m_tech, SHRT_MAX);
909         divine_unit_change((struct empobj *)ship, "Tech level",
910                            arg != ship->shp_tech, arg - ship->shp_tech,
911                            "from %d to %d", ship->shp_tech, arg);
912         shp_set_tech(ship, arg);
913         break;
914     case 'a':
915         arg = LIMIT_TO(arg, 0, PLG_EXPOSED);
916         divine_unit_change_quiet((struct empobj *)ship, "Plague stage",
917                                  arg != ship->shp_pstage,
918                                  "from %d to %d", ship->shp_pstage, arg);
919         ship->shp_pstage = arg;
920         break;
921     case 'b':
922         arg = LIMIT_TO(arg, 0, 32767);
923         divine_unit_change_quiet((struct empobj *)ship, "Plague time",
924                                  arg != ship->shp_ptime,
925                                  "from %d to %d", ship->shp_ptime, arg);
926         ship->shp_ptime = arg;
927         break;
928     case 'R':
929         divine_unit_change((struct empobj *)ship, "Retreat path",
930                 strncmp(p, ship->shp_rpath, sizeof(ship->shp_rpath) - 1),
931                 0, "from %s to %.*s",
932                 ship->shp_rpath, (int)sizeof(ship->shp_rpath) - 1, p);
933         strncpy(ship->shp_rpath, p, sizeof(ship->shp_rpath) - 1);
934         break;
935     case 'W':
936         divine_flag_change((struct empobj *)ship, "Retreat conditions",
937                            ship->shp_rflags, arg, retreat_flags);
938         ship->shp_rflags = arg;
939         break;
940     case 'c':
941     case 'm':
942     case 'u':
943     case 'f':
944     case 's':
945     case 'g':
946     case 'p':
947     case 'i':
948     case 'd':
949     case 'o':
950     case 'l':
951     case 'h':
952     case 'r':
953         edit_item((struct empobj *)ship, ship->shp_item, item_by_name(key),
954                   arg, mcp->m_item);
955         break;
956     default:
957         pr("huh? (%s)\n", key);
958         return RET_FAIL;
959     }
960     return RET_OK;
961 }
962
963 static int
964 edit_land(struct lndstr *land, char *key, char *p)
965 {
966     struct lchrstr *lcp = &lchr[land->lnd_type];
967     int arg = atoi(p);
968
969     switch (*key) {
970     case 'U':
971     case 'O':
972     case 'L':
973     case 'e':
974     case 'M':
975     case 'a':
976         return edit_unit((struct empobj *)land, key, p,
977                          LAND_MINEFF, "army",
978                          land->lnd_ship >= 0 || land->lnd_land >= 0);
979     case 'T':
980         arg = ef_elt_byname(EF_LAND_CHR, p);
981         if (arg < 0) {
982             pr("%s: invalid land unit type\n", p);
983             return RET_FAIL;
984         }
985         divine_unit_change((struct empobj *)land, "Type",
986                            arg != land->lnd_type, 0,
987                            "to %s", lchr[arg].l_name);
988         land->lnd_type = arg;
989         limit_item((struct empobj *)land, land->lnd_item, lchr[arg].l_item);
990         if (land->lnd_tech >= lchr[arg].l_tech)
991             break;
992         arg = lchr[arg].l_tech;
993         /* fall through */
994     case 't':
995         arg = LIMIT_TO(arg, lcp->l_tech, SHRT_MAX);
996         divine_unit_change((struct empobj *)land, "Tech level",
997                            arg != land->lnd_tech, arg - land->lnd_tech,
998                            "from %d to %d", land->lnd_tech, arg);
999         lnd_set_tech(land, arg);
1000         break;
1001     case 'F':
1002         arg = LIMIT_TO(arg, 0, 127);
1003         divine_unit_change((struct empobj *)land, "Fortification",
1004                            arg != land->lnd_harden, arg - land->lnd_harden,
1005                            "from %d to %d", land->lnd_harden, arg);
1006         land->lnd_harden = arg;
1007         break;
1008     case 'S':
1009         if (arg < -1 || arg >= ef_nelem(EF_SHIP))
1010             return RET_SYN;
1011         if (arg == land->lnd_ship) {
1012             pr("Ship of %s unchanged\n", prland(land));
1013             break;
1014         }
1015         divine_unload((struct empobj *)land, EF_SHIP, land->lnd_ship);
1016         if (arg >= 0) {
1017             divine_unload((struct empobj *)land, EF_LAND, land->lnd_land);
1018             land->lnd_land = -1;
1019         }
1020         divine_load((struct empobj *)land, EF_SHIP, arg);
1021         land->lnd_ship = arg;
1022         break;
1023     case 'Y':
1024         if (arg < -1 || arg >= ef_nelem(EF_LAND))
1025             return RET_SYN;
1026         if (arg == land->lnd_land) {
1027             pr("Land unit of %s unchanged\n", prland(land));
1028             break;
1029         }
1030         divine_unload((struct empobj *)land, EF_LAND, land->lnd_land);
1031         if (arg >= 0) {
1032             divine_unload((struct empobj *)land, EF_SHIP, land->lnd_ship);
1033             land->lnd_ship = -1;
1034         }
1035         divine_load((struct empobj *)land, EF_LAND, arg);
1036         land->lnd_land = arg;
1037         break;
1038     case 'Z':
1039         arg = LIMIT_TO(arg, 0, 100);
1040         divine_unit_change((struct empobj *)land, "Retreat percentage",
1041                            arg != land->lnd_retreat, 0,
1042                            "from %d to %d", land->lnd_retreat, arg);
1043         land->lnd_retreat = arg;
1044         break;
1045     case 'R':
1046         divine_unit_change((struct empobj *)land, "Retreat path",
1047                 strncmp(p, land->lnd_rpath, sizeof(land->lnd_rpath) - 1),
1048                 0, "from %s to %.*s",
1049                 land->lnd_rpath, (int)sizeof(land->lnd_rpath) - 1, p);
1050         strncpy(land->lnd_rpath, p, sizeof(land->lnd_rpath) - 1);
1051         break;
1052     case 'W':
1053         divine_flag_change((struct empobj *)land, "Retreat condition",
1054                            land->lnd_rflags, arg, retreat_flags);
1055         land->lnd_rflags = arg;
1056         break;
1057     case 'c':
1058     case 'm':
1059     case 'u':
1060     case 'f':
1061     case 's':
1062     case 'g':
1063     case 'p':
1064     case 'i':
1065     case 'd':
1066     case 'o':
1067     case 'l':
1068     case 'h':
1069     case 'r':
1070         edit_item((struct empobj *)land, land->lnd_item, item_by_name(key),
1071                   arg, lcp->l_item);
1072         break;
1073     default:
1074         pr("huh? (%s)\n", key);
1075         return RET_FAIL;
1076     }
1077     return RET_OK;
1078 }
1079
1080 static int
1081 edit_plane(struct plnstr *plane, char *key, char *p)
1082 {
1083     struct plchrstr *pcp = &plchr[plane->pln_type];
1084     int arg = atoi(p);
1085
1086     switch (*key) {
1087     case 'U':
1088     case 'O':
1089     case 'l':
1090     case 'e':
1091     case 'm':
1092     case 'w':
1093         return edit_unit((struct empobj *)plane, key, p,
1094                          PLANE_MINEFF, "wing",
1095                          plane->pln_ship >= 0 || plane->pln_land >= 0);
1096     case 'T':
1097         arg = ef_elt_byname(EF_PLANE_CHR, p);
1098         if (arg < 0) {
1099             pr("%s: invalid plane type\n", p);
1100             return RET_FAIL;
1101         }
1102         divine_unit_change((struct empobj *)plane, "Type",
1103                            arg != plane->pln_type, 0,
1104                            "to %s", plchr[arg].pl_name);
1105         plane->pln_type = arg;
1106         if (plane->pln_tech >= plchr[arg].pl_tech)
1107             break;
1108         arg = plchr[arg].pl_tech;
1109         /* fall through */
1110     case 't':
1111         arg = LIMIT_TO(arg, pcp->pl_tech, SHRT_MAX);
1112         divine_unit_change((struct empobj *)plane, "Tech level",
1113                            arg != plane->pln_tech, arg - plane->pln_tech,
1114                            "from %d to %d", plane->pln_tech, arg);
1115         pln_set_tech(plane, arg);
1116         break;
1117     case 'r':
1118         arg = LIMIT_TO(arg, 0, pl_range(pcp, plane->pln_tech));
1119         divine_unit_change((struct empobj *)plane, "Range",
1120                            arg != plane->pln_range, 0,
1121                            "from %d to %d", plane->pln_range, arg);
1122         plane->pln_range = (unsigned char)arg;
1123         break;
1124     case 's':
1125         if (arg < -1 || arg >= ef_nelem(EF_SHIP))
1126             return RET_SYN;
1127         if (arg == plane->pln_ship) {
1128             pr("Ship of %s unchanged\n", prplane(plane));
1129             break;
1130         }
1131         divine_unload((struct empobj *)plane, EF_SHIP, plane->pln_ship);
1132         if (arg >= 0) {
1133             divine_unload((struct empobj *)plane, EF_LAND, plane->pln_land);
1134             plane->pln_land = -1;
1135         }
1136         divine_load((struct empobj *)plane, EF_SHIP, arg);
1137         plane->pln_ship = arg;
1138         break;
1139     case 'y':
1140         if (arg < -1 || arg >= ef_nelem(EF_LAND))
1141             return RET_SYN;
1142         if (arg == plane->pln_land) {
1143             pr("Land unit of %s unchanged\n", prplane(plane));
1144             break;
1145         }
1146         divine_unload((struct empobj *)plane, EF_LAND, plane->pln_land);
1147         if (arg >= 0) {
1148             divine_unload((struct empobj *)plane, EF_SHIP, plane->pln_ship);
1149             plane->pln_ship = -1;
1150         }
1151         divine_load((struct empobj *)plane, EF_LAND, arg);
1152         plane->pln_land = arg;
1153         break;
1154     case 'f':
1155         divine_flag_change((struct empobj *)plane, "Flags",
1156                            plane->pln_flags, arg, plane_flags);
1157         plane->pln_flags = arg;
1158         break;
1159     default:
1160         pr("huh? (%s)\n", key);
1161         return RET_FAIL;
1162     }
1163     return RET_OK;
1164 }
1165
1166 static int
1167 edit_nuke(struct nukstr *nuke, char *key, char *p)
1168 {
1169     struct nchrstr *ncp = &nchr[nuke->nuk_type];
1170     int arg = atoi(p);
1171
1172     switch (*key) {
1173     case 'U':
1174     case 'O':
1175     case 'L':
1176     case 'S':
1177         return edit_unit((struct empobj *)nuke, key, p,
1178                          100, "stockpile", nuke->nuk_plane >= 0);
1179     case 't':
1180         arg = ef_elt_byname(EF_NUKE_CHR, p);
1181         if (arg < 0) {
1182             pr("%s: invalid nuke type\n", p);
1183             return RET_FAIL;
1184         }
1185         divine_unit_change((struct empobj *)nuke, "Type",
1186                            arg != nuke->nuk_type, 0,
1187                            "to %s", nchr[arg].n_name);
1188         nuke->nuk_type = arg;
1189         if (nuke->nuk_tech >= nchr[arg].n_tech)
1190             break;
1191         arg = nchr[arg].n_tech;
1192         /* fall through */
1193     case 'T':
1194         arg = LIMIT_TO(arg, ncp->n_tech, SHRT_MAX);
1195         divine_unit_change((struct empobj *)nuke, "Tech level",
1196                            arg != nuke->nuk_tech, arg - nuke->nuk_tech,
1197                            "from %d to %d", nuke->nuk_tech, arg);
1198         nuke->nuk_tech = arg;
1199         break;
1200     case 'p':
1201         if (arg < -1 || arg >= ef_nelem(EF_PLANE))
1202             return RET_SYN;
1203         if (arg == nuke->nuk_plane) {
1204             pr("Plane of %s unchanged\n", prnuke(nuke));
1205             break;
1206         }
1207         divine_unload((struct empobj *)nuke, EF_PLANE, nuke->nuk_plane);
1208         divine_load((struct empobj *)nuke, EF_PLANE, arg);
1209         nuke->nuk_plane = arg;
1210         break;
1211     default:
1212         pr("huh? (%s)\n", key);
1213         return RET_FAIL;
1214     }
1215     return RET_OK;
1216 }