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