]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/edit.c
commands: Rename the command functions
[empserver] / src / lib / commands / edit.c
index ee38a1543d94db615766a4443b270560d16e3bb6..80683694a160c04c45452c4005624a6792a78d49 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
@@ -31,7 +31,7 @@
  *     Chad Zabel, 1994
  *     Steve McClure, 1998-2000
  *     Ron Koenderink, 2003-2009
- *     Markus Armbruster, 2003-2013
+ *     Markus Armbruster, 2003-2018
  */
 
 #include <config.h>
@@ -54,14 +54,16 @@ static void print_nat(struct natstr *);
 static void print_plane(struct plnstr *);
 static void print_land(struct lndstr *);
 static void print_ship(struct shpstr *);
+static void print_nuke(struct nukstr *);
 static char *getin(char *, char **);
 static int edit_nat(struct natstr *, char *, char *);
 static int edit_ship(struct shpstr *, char *, char *);
 static int edit_land(struct lndstr *, char *, char *);
 static int edit_plane(struct plnstr *, char *, char *);
+static int edit_nuke(struct nukstr *, char *, char *);
 
 int
-edit(void)
+c_edit(void)
 {
     union empobj_storage item;
     char *what;
@@ -90,8 +92,8 @@ edit(void)
        type = EF_LAND;
        break;
     case 'n':
-       pr("Not implemented yet.\n");
-       return RET_FAIL;
+       type = EF_NUKE;
+       break;
     case 'c':
        type = EF_NATION;
        break;
@@ -117,6 +119,9 @@ edit(void)
            case EF_LAND:
                print_land(&item.land);
                break;
+           case EF_NUKE:
+               print_nuke(&item.nuke);
+               break;
            case EF_NATION:
                print_nat(&item.nat);
                break;
@@ -171,6 +176,9 @@ edit(void)
            case EF_PLANE:
                ret = edit_plane(&item.plane, key, ptr);
                break;
+           case EF_NUKE:
+               ret = edit_nuke(&item.nuke, key, ptr);
+               break;
            default:
                CANT_REACH();
            }
@@ -218,11 +226,9 @@ print_sect(struct sctstr *sect)
 static void
 print_nat(struct natstr *np)
 {
-    int i;
-
     pr("Country #: %2d\n", np->nat_cnum);
     pr("Name <n>: %-20s\t", np->nat_cnam);
-    pr("Representative <r>: %-20s\n", np->nat_pnam);
+    pr("Representative <r>: %s\n", np->nat_pnam);
     pr("BTUs <b>: %3d\t\t\t", np->nat_btu);
     pr("Reserves <m>: %5d\n", np->nat_reserve);
     pr("Capital <c>: %s\t\t",
@@ -237,14 +243,6 @@ print_nat(struct natstr *np)
     pr("Happiness <H>: %.2f\n", np->nat_level[NAT_HLEV]);
     pr("Money <M>: $%6d\n", np->nat_money);
     pr("Telegrams <t>: %6d\n", np->nat_tgms);
-    if (opt_HIDDEN) {
-       pr("Countries contacted: ");
-       for (i = 0; i < MAXNOC; i++) {
-           if (getcontact(np, i))
-               pr("%d(%d) ", i, getcontact(np, i));
-       }
-       pr("\n");
-    }
 }
 
 static void
@@ -266,6 +264,28 @@ print_plane(struct plnstr *plane)
     pr("Land Unit <y>: %d\n", plane->pln_land);
 }
 
+static void
+print_items(short item[])
+{
+    pr("  civ  mil   uw food   sh  gun  pet iron dust  bar  oil  lcm  hcm  rad\n"
+       "    c    m    u    f    s    g    p    i    d    B    o    l    h    r\n"
+       "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d\n",
+       item[I_CIVIL],
+       item[I_MILIT],
+       item[I_UW],
+       item[I_FOOD],
+       item[I_SHELL],
+       item[I_GUN],
+       item[I_PETROL],
+       item[I_IRON],
+       item[I_DUST],
+       item[I_BAR],
+       item[I_OIL],
+       item[I_LCM],
+       item[I_HCM],
+       item[I_RAD]);
+}
+
 static void
 print_land(struct lndstr *land)
 {
@@ -284,22 +304,7 @@ print_land(struct lndstr *land)
     pr("Retreat percentage <Z>: %d\n", land->lnd_retreat);
     pr("Retreat path <R>: '%s'\t\tRetreat Flags <W>: %d\n",
        land->lnd_rpath, land->lnd_rflags);
-    pr("civ mil  uw food shl gun  pet  irn  dst  oil  lcm  hcm rad\n");
-    pr("  c   m   u    f   s   g    p    i    d    o    l    h   r\n");
-    pr("%3d", land->lnd_item[I_CIVIL]);
-    pr("%4d", land->lnd_item[I_MILIT]);
-    pr("%4d", land->lnd_item[I_UW]);
-    pr("%5d", land->lnd_item[I_FOOD]);
-    pr("%4d", land->lnd_item[I_SHELL]);
-    pr("%4d", land->lnd_item[I_GUN]);
-    pr("%5d", land->lnd_item[I_PETROL]);
-    pr("%5d", land->lnd_item[I_IRON]);
-    pr("%5d", land->lnd_item[I_DUST]);
-    pr("%5d", land->lnd_item[I_OIL]);
-    pr("%5d", land->lnd_item[I_LCM]);
-    pr("%5d", land->lnd_item[I_HCM]);
-    pr("%4d", land->lnd_item[I_RAD]);
-    pr("\n");
+    print_items(land->lnd_item);
 }
 
 static void
@@ -318,22 +323,20 @@ print_ship(struct shpstr *ship)
        ship->shp_rpath, ship->shp_rflags);
     pr("Plague Stage <a>: %d\t\t", ship->shp_pstage);
     pr("Plague Time <b>: %d\n", ship->shp_ptime);
-    pr("civ mil  uw food shl gun  pet  irn  dst  oil  lcm  hcm rad\n");
-    pr("  c   m   u    f   s   g    p    i    d    o    l    h   r\n");
-    pr("%3d", ship->shp_item[I_CIVIL]);
-    pr("%4d", ship->shp_item[I_MILIT]);
-    pr("%4d", ship->shp_item[I_UW]);
-    pr("%5d", ship->shp_item[I_FOOD]);
-    pr("%4d", ship->shp_item[I_SHELL]);
-    pr("%4d", ship->shp_item[I_GUN]);
-    pr("%5d", ship->shp_item[I_PETROL]);
-    pr("%5d", ship->shp_item[I_IRON]);
-    pr("%5d", ship->shp_item[I_DUST]);
-    pr("%5d", ship->shp_item[I_OIL]);
-    pr("%5d", ship->shp_item[I_LCM]);
-    pr("%5d", ship->shp_item[I_HCM]);
-    pr("%4d", ship->shp_item[I_RAD]);
-    pr("\n");
+    print_items(ship->shp_item);
+}
+
+static void
+print_nuke(struct nukstr *nuke)
+{
+    pr("%s %s\n", prnatid(nuke->nuk_own), prnuke(nuke));
+    pr("UID <U>: %d\t\t\t", nuke->nuk_uid);
+    pr("Type <t>: %d\n", nuke->nuk_type);
+    pr("Owner <O>: %d\t\t\t", nuke->nuk_own);
+    pr("Location <L>: %s\n", xyas(nuke->nuk_x, nuke->nuk_y, player->cnum));
+    pr("Tech <T>: %d\t\t\t", nuke->nuk_tech);
+    pr("Stockpile <S>: %.1s\n", &nuke->nuk_stockpile);
+    pr("Plane <p>: %d\n", nuke->nuk_plane);
 }
 
 static char *
@@ -444,7 +447,7 @@ edit_sect_i(struct sctstr *sect, char *key, int arg)
        break;
     case 'x':
        new = LIMIT_TO(arg, 0, CHE_MAX);
-       divine_sct_change_quiet(sect, "Guerillas", new != sect->sct_che,
+       divine_sct_change_quiet(sect, "Guerrillas", new != sect->sct_che,
                                "from %d to %d", sect->sct_che, new);
        sect->sct_che = new;
        break;
@@ -538,8 +541,14 @@ edit_sect(struct sctstr *sect, char *key, char *p)
                         newsect.sct_own);
        report_god_gives("Sector ", xyas(newx, newy, sect->sct_own),
                         sect->sct_own);
+       if (sect->sct_x == sect->sct_dist_x
+           && sect->sct_y == sect->sct_dist_y) {
+           sect->sct_dist_x = newx;
+           sect->sct_dist_y = newy;
+       }
        sect->sct_x = newx;
        sect->sct_y = newy;
+       sect->sct_coastal = newsect.sct_coastal;
        ef_set_uid(EF_SECTOR, sect, XYOFFSET(newx, newy));
        break;
     case 'D':
@@ -729,7 +738,7 @@ edit_unit(struct empobj *unit, char *key, char *p,
     coord newx, newy;
     union empobj_storage newunit;
     char newgroup;
+
     switch (toupper(*key)) {
     case 'U':
        if (arg < 0)
@@ -777,10 +786,12 @@ edit_unit(struct empobj *unit, char *key, char *p,
            return RET_FAIL;
        }
        divine_unit_change_quiet(unit, "Location",
-                                unit->own && unit->own != player->cnum,
+                                newx != unit->x || newy != unit->y,
                                 "from %s to %s",
                                 xyas(unit->x, unit->y, player->cnum),
                                 xyas(newx, newy, player->cnum));
+       if (newx == unit->x && newy == unit->y)
+           break;
        if (unit->own && unit->own != player->cnum)
            wu(0, unit->own,
               "Location of %s changed from %s to %s by an act of %s!\n",
@@ -788,8 +799,7 @@ edit_unit(struct empobj *unit, char *key, char *p,
               xyas(unit->x, unit->y, unit->own),
               xyas(newx, newy, unit->own),
               cname(player->cnum));
-       unit->x = newx;
-       unit->y = newy;
+       unit_teleport(unit, newx, newy);
        break;
     case 'E':
        arg = LIMIT_TO(arg, mineff, 100);
@@ -808,6 +818,7 @@ edit_unit(struct empobj *unit, char *key, char *p,
     case 'F':
     case 'W':
     case 'A':
+    case 'S':
        if (p[0] == '~')
            newgroup = 0;
        else if (isalpha(p[0]))
@@ -839,6 +850,17 @@ edit_item(struct empobj *unit, short item[], struct ichrstr *ip, int arg,
     item[ip->i_uid] = arg;
 }
 
+static void
+edit_unit_item(struct empobj *unit, short item[], char *key, int arg,
+              short lim[])
+{
+    char lowkey[2];
+
+    lowkey[0] = tolower(*key);
+    lowkey[1] = 0;
+    edit_item(unit, item, item_by_name(lowkey), arg, lim);
+}
+
 static void
 limit_item(struct empobj *unit, short item[], short lim[])
 {
@@ -855,6 +877,7 @@ edit_ship(struct shpstr *ship, char *key, char *p)
 {
     struct mchrstr *mcp = &mchr[ship->shp_type];
     int arg = atoi(p);
+    int ret;
 
     switch (*key) {
     case 'U':
@@ -863,8 +886,9 @@ edit_ship(struct shpstr *ship, char *key, char *p)
     case 'E':
     case 'M':
     case 'F':
-       return edit_unit((struct empobj *)ship, key, p,
-                        SHIP_MINEFF, "fleet", 0);
+       ret = edit_unit((struct empobj *)ship, key, p,
+                       SHIP_MINEFF, "fleet", 0);
+       return ret;
     case 't':
        arg = ef_elt_byname(EF_SHIP_CHR, p);
        if (arg < 0) {
@@ -874,12 +898,12 @@ edit_ship(struct shpstr *ship, char *key, char *p)
        divine_unit_change((struct empobj *)ship, "Type",
                           arg != ship->shp_type, 0,
                           "to %s", mchr[arg].m_name);
+       if (ship->shp_tech < mchr[arg].m_tech)
+           shp_set_tech(ship, mchr[arg].m_tech);
        ship->shp_type = arg;
+       shp_set_tech(ship, ship->shp_tech);
        limit_item((struct empobj *)ship, ship->shp_item, mchr[arg].m_item);
-       if (ship->shp_tech >= mchr[arg].m_tech)
-           break;
-       arg = mchr[arg].m_tech;
-       /* fall through */
+       break;
     case 'T':
        arg = LIMIT_TO(arg, mcp->m_tech, SHRT_MAX);
        divine_unit_change((struct empobj *)ship, "Tech level",
@@ -922,12 +946,13 @@ edit_ship(struct shpstr *ship, char *key, char *p)
     case 'p':
     case 'i':
     case 'd':
+    case 'B':
     case 'o':
     case 'l':
     case 'h':
     case 'r':
-       edit_item((struct empobj *)ship, ship->shp_item, item_by_name(key),
-                 arg, mcp->m_item);
+       edit_unit_item((struct empobj *)ship, ship->shp_item, key, arg,
+                      mcp->m_item);
        break;
     default:
        pr("huh? (%s)\n", key);
@@ -961,12 +986,12 @@ edit_land(struct lndstr *land, char *key, char *p)
        divine_unit_change((struct empobj *)land, "Type",
                           arg != land->lnd_type, 0,
                           "to %s", lchr[arg].l_name);
+       if (land->lnd_tech < lchr[arg].l_tech)
+           lnd_set_tech(land, lchr[arg].l_tech);
        land->lnd_type = arg;
+       lnd_set_tech(land, land->lnd_tech);
        limit_item((struct empobj *)land, land->lnd_item, lchr[arg].l_item);
-       if (land->lnd_tech >= lchr[arg].l_tech)
-           break;
-       arg = lchr[arg].l_tech;
-       /* fall through */
+       break;
     case 't':
        arg = LIMIT_TO(arg, lcp->l_tech, SHRT_MAX);
        divine_unit_change((struct empobj *)land, "Tech level",
@@ -976,6 +1001,8 @@ edit_land(struct lndstr *land, char *key, char *p)
        break;
     case 'F':
        arg = LIMIT_TO(arg, 0, 127);
+       if (land->lnd_ship >= 0 || land->lnd_land >= 0)
+           arg = 0;
        divine_unit_change((struct empobj *)land, "Fortification",
                           arg != land->lnd_harden, arg - land->lnd_harden,
                           "from %d to %d", land->lnd_harden, arg);
@@ -1011,6 +1038,20 @@ edit_land(struct lndstr *land, char *key, char *p)
        divine_load((struct empobj *)land, EF_LAND, arg);
        land->lnd_land = arg;
        break;
+    case 'A':
+       arg = LIMIT_TO(arg, 0, PLG_EXPOSED);
+       divine_unit_change_quiet((struct empobj *)land, "Plague stage",
+                                arg != land->lnd_pstage,
+                                "from %d to %d", land->lnd_pstage, arg);
+       land->lnd_pstage = arg;
+       break;
+    case 'b':
+       arg = LIMIT_TO(arg, 0, 32767);
+       divine_unit_change_quiet((struct empobj *)land, "Plague time",
+                                arg != land->lnd_ptime,
+                                "from %d to %d", land->lnd_ptime, arg);
+       land->lnd_ptime = arg;
+       break;
     case 'Z':
        arg = LIMIT_TO(arg, 0, 100);
        divine_unit_change((struct empobj *)land, "Retreat percentage",
@@ -1039,12 +1080,13 @@ edit_land(struct lndstr *land, char *key, char *p)
     case 'p':
     case 'i':
     case 'd':
+    case 'B':
     case 'o':
     case 'l':
     case 'h':
     case 'r':
-       edit_item((struct empobj *)land, land->lnd_item, item_by_name(key),
-                 arg, lcp->l_item);
+       edit_unit_item((struct empobj *)land, land->lnd_item, key, arg,
+                      lcp->l_item);
        break;
     default:
        pr("huh? (%s)\n", key);
@@ -1078,11 +1120,14 @@ edit_plane(struct plnstr *plane, char *key, char *p)
        divine_unit_change((struct empobj *)plane, "Type",
                           arg != plane->pln_type, 0,
                           "to %s", plchr[arg].pl_name);
+       if (plane->pln_tech < plchr[arg].pl_tech)
+           pln_set_tech(plane, plchr[arg].pl_tech);
+       if (plane->pln_range >= pln_range_max(plane))
+           /* preserve unlimited range, pln_set_tech() will adjust */
+           plane->pln_range = UCHAR_MAX;
        plane->pln_type = arg;
-       if (plane->pln_tech >= plchr[arg].pl_tech)
-           break;
-       arg = plchr[arg].pl_tech;
-       /* fall through */
+       pln_set_tech(plane, plane->pln_tech);
+       break;
     case 't':
        arg = LIMIT_TO(arg, pcp->pl_tech, SHRT_MAX);
        divine_unit_change((struct empobj *)plane, "Tech level",
@@ -1090,6 +1135,16 @@ edit_plane(struct plnstr *plane, char *key, char *p)
                           "from %d to %d", plane->pln_tech, arg);
        pln_set_tech(plane, arg);
        break;
+    case 'F':
+       arg = LIMIT_TO(arg, 0, 127);
+       if (!(pcp->pl_flags & P_M)
+           || plane->pln_ship >= 0 || plane->pln_land >= 0)
+           arg = 0;
+       divine_unit_change((struct empobj *)plane, "Fortification",
+                          arg != plane->pln_harden, arg - plane->pln_harden,
+                          "from %d to %d", plane->pln_harden, arg);
+       plane->pln_harden = arg;
+       break;
     case 'r':
        arg = LIMIT_TO(arg, 0, pl_range(pcp, plane->pln_tech));
        divine_unit_change((struct empobj *)plane, "Range",
@@ -1131,6 +1186,59 @@ edit_plane(struct plnstr *plane, char *key, char *p)
        divine_flag_change((struct empobj *)plane, "Flags",
                           plane->pln_flags, arg, plane_flags);
        plane->pln_flags = arg;
+       if (pln_is_in_orbit(plane))
+           plane->pln_ship = plane->pln_land = -1;
+       break;
+    default:
+       pr("huh? (%s)\n", key);
+       return RET_FAIL;
+    }
+    return RET_OK;
+}
+
+static int
+edit_nuke(struct nukstr *nuke, char *key, char *p)
+{
+    struct nchrstr *ncp = &nchr[nuke->nuk_type];
+    int arg = atoi(p);
+
+    switch (*key) {
+    case 'U':
+    case 'O':
+    case 'L':
+    case 'S':
+       return edit_unit((struct empobj *)nuke, key, p,
+                        100, "stockpile", nuke->nuk_plane >= 0);
+    case 't':
+       arg = ef_elt_byname(EF_NUKE_CHR, p);
+       if (arg < 0) {
+           pr("%s: invalid nuke type\n", p);
+           return RET_FAIL;
+       }
+       divine_unit_change((struct empobj *)nuke, "Type",
+                          arg != nuke->nuk_type, 0,
+                          "to %s", nchr[arg].n_name);
+       if (nuke->nuk_tech < nchr[arg].n_tech)
+           nuke->nuk_tech = nchr[arg].n_tech;
+       nuke->nuk_type = arg;
+       break;
+    case 'T':
+       arg = LIMIT_TO(arg, ncp->n_tech, SHRT_MAX);
+       divine_unit_change((struct empobj *)nuke, "Tech level",
+                          arg != nuke->nuk_tech, arg - nuke->nuk_tech,
+                          "from %d to %d", nuke->nuk_tech, arg);
+       nuke->nuk_tech = arg;
+       break;
+    case 'p':
+       if (arg < -1 || arg >= ef_nelem(EF_PLANE))
+           return RET_SYN;
+       if (arg == nuke->nuk_plane) {
+           pr("Plane of %s unchanged\n", prnuke(nuke));
+           break;
+       }
+       divine_unload((struct empobj *)nuke, EF_PLANE, nuke->nuk_plane);
+       divine_load((struct empobj *)nuke, EF_PLANE, arg);
+       nuke->nuk_plane = arg;
        break;
     default:
        pr("huh? (%s)\n", key);