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