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