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