]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/takeover.c
Clean up dead stores
[empserver] / src / lib / subs / takeover.c
index 99c3f19fd24797f0e9cee132599e71cf88fb0638..bce16cc62e03d6396605d3cae883ebe471c705ca 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  takeover.c: Take over from another country
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Steve McClure, 1996-2000
+ *     Markus Armbruster, 2007-2008
  */
 
+#include <config.h>
+
+#include "file.h"
+#include "empobj.h"
+#include "game.h"
+#include "land.h"
+#include "lost.h"
 #include "misc.h"
-#include "sect.h"
-#include "xy.h"
-#include "nsc.h"
 #include "nat.h"
+#include "nsc.h"
+#include "optlist.h"
 #include "plane.h"
-#include "land.h"
-#include "ship.h"
-#include "file.h"
 #include "prototypes.h"
-#include "optlist.h"
+#include "sect.h"
+#include "ship.h"
+#include "unit.h"
+#include "xy.h"
+
+static void takeover_unit(struct empobj *, natid);
 
 void
 takeover(struct sctstr *sp, natid newown)
@@ -69,7 +78,7 @@ takeover(struct sctstr *sp, natid newown)
 
     pp = &p;
     /* Take over planes */
-    snxtitem_dist(&ni, EF_PLANE, sp->sct_x, sp->sct_y, 0);
+    snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
     while (nxtitem(&ni, pp)) {
        if (pp->pln_own != sp->sct_own)
            continue;
@@ -78,7 +87,7 @@ takeover(struct sctstr *sp, natid newown)
 
     /* Take over land units */
     lp = &land;
-    snxtitem_dist(&ni, EF_LAND, sp->sct_x, sp->sct_y, 0);
+    snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
     while (nxtitem(&ni, lp)) {
        if ((lp->lnd_own == newown) || (lp->lnd_own == 0))
            continue;
@@ -109,7 +118,7 @@ takeover(struct sctstr *sp, natid newown)
               prland(lp),
               cname(newown), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
        }
-       takeover_land(lp, newown, 1);
+       takeover_land(lp, newown);
     }
 
     sp->sct_avail = 0;
@@ -121,7 +130,6 @@ takeover(struct sctstr *sp, natid newown)
      * n: random number from -25:75 + (50 - loyalty)
      */
     n = (50 - sp->sct_loyal) + ((random() % 100) - 25);
-    che_count = 0;
     if (n > 0 && sp->sct_own == sp->sct_oldown) {
        che_count = (civ * n / 3000) + 5;
        if (che_count * 2 > civ)
@@ -139,6 +147,8 @@ takeover(struct sctstr *sp, natid newown)
     sp->sct_che = che_count;
     if (newown != sp->sct_oldown)
        sp->sct_che_target = newown;
+    if (sp->sct_che_target == 0)
+       sp->sct_che = 0;
     sp->sct_item[I_CIVIL] = civ;
     if (sp->sct_oldown == newown || civ == 0) {
        /*
@@ -152,11 +162,9 @@ takeover(struct sctstr *sp, natid newown)
         */
        sp->sct_loyal = 50;
     }
-    makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
-    makenotlost(EF_SECTOR, newown, 0, sp->sct_x, sp->sct_y);
     sp->sct_own = newown;
     if (opt_MOB_ACCESS) {
-       time(&sp->sct_access);
+       game_tick_to_now(&sp->sct_access);
        sp->sct_mobil = -(etu_per_update / sect_mob_neg_factor);
     } else {
        sp->sct_mobil = 0;
@@ -182,7 +190,7 @@ takeover_plane(struct plnstr *pp, natid newown)
     if (n < 0)
        n = 0;
     pp->pln_effic = n;
-    if (pp->pln_effic < PLANE_MINEFF || pp->pln_harden > (s_char)0) {
+    if (pp->pln_effic < PLANE_MINEFF || pp->pln_harden > 0) {
        pp->pln_effic = 0;
        mpr(newown, "%s blown up by the crew!\n", prplane(pp));
        wu(0, pp->pln_own,
@@ -196,133 +204,73 @@ takeover_plane(struct plnstr *pp, natid newown)
           prplane(pp),
           cname(newown), xyas(pp->pln_x, pp->pln_y, pp->pln_own));
     }
-    if (opt_MARKET)
-       trdswitchown(EF_PLANE, (int *)pp, newown);
-    if (pp->pln_mobil > (s_char)0)
-       pp->pln_mobil = 0;
-    makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
-    pp->pln_own = newown;
-    makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
-    pp->pln_mission = 0;
-    putplane(pp->pln_uid, pp);
+    takeover_unit((struct empobj *)pp, newown);
 }
 
 void
-takeover_ship(struct shpstr *sp, natid newown, int hostile)
+takeover_ship(struct shpstr *sp, natid newown)
 {
-    struct plnstr *pp;
-    struct lndstr *lp;
-    struct nstr_item ni;
-    struct plnstr p;
-    struct lndstr llp;
-
-    if (opt_MARKET)
-       trdswitchown(EF_SHIP, (int *)sp, newown);
-    makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
-    sp->shp_own = newown;
-    makenotlost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
-    sp->shp_mission = 0;
-    sp->shp_fleet = ' ';
-    sp->shp_rflags = 0;
-    /* Keep track of when this was taken over */
-    time(&sp->shp_access);
-    memset(sp->shp_rpath, 0, sizeof(sp->shp_rpath));
-    pp = &p;
-    lp = &llp;
-    /* Take over planes */
-    snxtitem_all(&ni, EF_PLANE);
-    while (nxtitem(&ni, pp)) {
-       if (pp->pln_ship != sp->shp_uid)
-           continue;
-       if (pp->pln_own == 0)
-           continue;
-       if (hostile) {
-           if (pp->pln_effic > PLANE_MINEFF)
-               pp->pln_effic = PLANE_MINEFF;
-       }
-       pp->pln_mobil = 0;
-       /* Keep track of when this was taken over */
-       time(&pp->pln_access);
-       if (opt_MARKET)
-           trdswitchown(EF_PLANE, (int *)pp, newown);
-       pp->pln_mission = 0;
-       makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
-       pp->pln_own = newown;
-       makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
-                   pp->pln_y);
-       putplane(pp->pln_uid, pp);
-    }
-    /* Take over land units */
-    snxtitem_all(&ni, EF_LAND);
-    while (nxtitem(&ni, lp)) {
-       if (lp->lnd_ship != sp->shp_uid)
-           continue;
-       if (lp->lnd_own == 0)
-           continue;
-       takeover_land(lp, newown, hostile);
-    }
-    putship(sp->shp_uid, sp);
+    takeover_unit((struct empobj *)sp, newown);
 }
 
 void
-takeover_land(struct lndstr *landp, natid newown, int hostile)
+takeover_land(struct lndstr *landp, natid newown)
 {
+    takeover_unit((struct empobj *)landp, newown);
+}
+
+static void
+takeover_unit(struct empobj *unit, natid newown)
+{
+    struct shpstr *sp;
     struct plnstr *pp;
     struct lndstr *lp;
+    struct nukstr *np;
+    int type;
     struct nstr_item ni;
-    struct plnstr p;
-    struct lndstr llp;
+    union empobj_storage cargo;
 
-    if (landp->lnd_effic < LAND_MINEFF) {
-       putland(landp->lnd_uid, landp);
-       return;
-    }
-    landp->lnd_army = ' ';
-    landp->lnd_mobil = 0;
-    landp->lnd_harden = 0;
-    /* Keep track of when this was taken over */
-    time(&landp->lnd_access);
+    unit->own = newown;
     if (opt_MARKET)
-       trdswitchown(EF_LAND, (int *)landp, newown);
-    landp->lnd_mission = 0;
-    makelost(EF_LAND, landp->lnd_own, landp->lnd_uid, landp->lnd_x,
-            landp->lnd_y);
-    landp->lnd_own = newown;
-    makenotlost(EF_LAND, landp->lnd_own, landp->lnd_uid, landp->lnd_x,
-               landp->lnd_y);
-    pp = &p;
-    lp = &llp;
-    /* Take over planes */
-    snxtitem_all(&ni, EF_PLANE);
-    while (nxtitem(&ni, pp)) {
-       if (pp->pln_land != landp->lnd_uid)
-           continue;
-       if (pp->pln_own == 0)
-           continue;
-       if (hostile) {
-           if (pp->pln_effic > PLANE_MINEFF)
-               pp->pln_effic = PLANE_MINEFF;
-       }
-       pp->pln_mobil = 0;
-       /* Keep track of when this was taken over */
-       time(&pp->pln_access);
-       if (opt_MARKET)
-           trdswitchown(EF_PLANE, (int *)pp, newown);
-       pp->pln_mission = 0;
-       makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
-       pp->pln_own = newown;
-       makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
-                   pp->pln_y);
-       putplane(pp->pln_uid, pp);
+       trdswitchown(unit->ef_type, unit, newown);
+    unit_wipe_orders(unit);
+
+    switch (unit->ef_type) {
+    case EF_SHIP:
+       sp = (struct shpstr *)unit;
+       sp->shp_off = 1;
+       break;
+    case EF_PLANE:
+       pp = (struct plnstr *)unit;
+       if (pp->pln_mobil > 0)
+           pp->pln_mobil = 0;
+       pp->pln_off = 1;
+       break;
+    case EF_LAND:
+       lp = (struct lndstr *)unit;
+       if (lp->lnd_mobil > 0)
+           lp->lnd_mobil = 0;
+       lp->lnd_off = 1;
+       lp->lnd_harden = 0;
+       break;
+    case EF_NUKE:
+       np = (struct nukstr *)unit;
+       np->nuk_off = 1;
+       break;
+    default:
+       CANT_REACH();
     }
-    /* Take over land units */
-    snxtitem_all(&ni, EF_LAND);
-    while (nxtitem(&ni, lp)) {
-       if (lp->lnd_land != landp->lnd_uid)
-           continue;
-       if (lp->lnd_own == 0)
-           continue;
-       takeover_land(lp, newown, hostile);
+
+    put_empobj(unit->ef_type, unit->uid, unit);
+
+    for (type = EF_PLANE; type <= EF_NUKE; type++) {
+       snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
+       while (nxtitem(&ni, &cargo)) {
+           if (cargo.gen.own == newown)
+               continue;
+           if (type == EF_PLANE)
+               cargo.plane.pln_effic = PLANE_MINEFF;
+           takeover_unit(&cargo.gen, newown);
+       }
     }
-    putland(landp->lnd_uid, landp);
 }