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