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