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