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