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