]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/lndsub.c
Don't unlimber when guns unsuccessfully try to fire
[empserver] / src / lib / subs / lndsub.c
index 259aefcae41ecf9c9a7674b3e790baa084d136e4..12f8ecc7f22466011628ce74f611776c02ba2d5d 100644 (file)
@@ -30,7 +30,7 @@
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1998-2000
- *     Markus Armbruster, 2004-2006
+ *     Markus Armbruster, 2004-2008
  */
 
 #include <config.h>
@@ -76,7 +76,7 @@ attack_val(int combat_mode, struct lndstr *lp)
        return 1;
 
     men = lp->lnd_item[I_MILIT];
-    value = men * lp->lnd_att * lp->lnd_effic / 100.0;
+    value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
 
     switch (combat_mode) {
     case A_ATTACK:
@@ -113,7 +113,7 @@ defense_val(struct lndstr *lp)
        !(lcp->l_flags & L_MARINE))
        return men;
 
-    value = men * lp->lnd_def * lp->lnd_effic / 100.0;
+    value = men * lnd_def(lp) * lp->lnd_effic / 100.0;
     value *= ((double)land_mob_max + lp->lnd_harden) / land_mob_max;
 
     /* If there are military on the unit, you get at least a 1
@@ -172,7 +172,7 @@ lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
        llp->unit.land.lnd_effic = 0;
     } else {
        eff_eq = ldround(cas * 100.0 /
-           ((struct lchrstr *)llp->chrp)->l_mil, 1);
+           ((struct lchrstr *)llp->chrp)->l_item[I_MILIT], 1);
        llp->unit.land.lnd_effic -= eff_eq;
        lnd_submil(&llp->unit.land, cas);
     }
@@ -274,7 +274,8 @@ lnd_take_casualty(int combat_mode, struct ulist *llp, int cas)
     if (nowhere_to_go) {
        /* nowhere to go.. take more casualties */
        llp->unit.land.lnd_effic -= 10;
-       lnd_submil(&llp->unit.land, ((struct lchrstr *)llp->chrp)->l_mil / 10);
+       lnd_submil(&llp->unit.land,
+                  ((struct lchrstr *)llp->chrp)->l_item[I_MILIT] / 10);
        if (llp->unit.land.lnd_effic < LAND_MINEFF)
            lnd_delete(llp, "has nowhere to retreat, and dies!");
        else
@@ -321,39 +322,34 @@ int
 lnd_spyval(struct lndstr *lp)
 {
     if (lchr[(int)lp->lnd_type].l_flags & L_RECON)
-       return lp->lnd_spy * (lp->lnd_effic / 100.0) + 2;
+       return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0) + 2;
     else
-       return lp->lnd_spy * (lp->lnd_effic / 100.0);
+       return lchr[lp->lnd_type].l_spy * (lp->lnd_effic / 100.0);
 }
 
-double
+void
 intelligence_report(int destination, struct lndstr *lp, int spy,
                    char *mess)
 {
-    struct lchrstr *lcp;
+    int vis = lnd_vis(lp);
     char buf1[80], buf2[80], buf3[80];
-    double estimate = 0.0;     /* estimated defense value */
 
     if (destination == 0)
-       return 0;
+       return;
 
     if (lp->lnd_own == 0)
-       return 0;
-
-    lcp = &lchr[(int)lp->lnd_type];
+       return;
 
     memset(buf1, 0, sizeof(buf1));
     memset(buf2, 0, sizeof(buf2));
     memset(buf3, 0, sizeof(buf3));
-    if (chance((spy + lp->lnd_vis) / 10.0)) {
+    if (chance((spy + vis) / 10.0)) {
        if (destination == player->cnum)
            pr("%s %s", mess, prland(lp));
        else
            sprintf(buf1, "%s %s", mess, prland(lp));
 
-       estimate = lp->lnd_item[I_MILIT];
-
-       if (chance((spy + lp->lnd_vis) / 20.0)) {
+       if (chance((spy + vis) / 20.0)) {
            if (destination == player->cnum)
                pr(" (eff %d, mil %d",
                   roundintby(lp->lnd_effic, 5),
@@ -362,9 +358,8 @@ intelligence_report(int destination, struct lndstr *lp, int spy,
                sprintf(buf2, " (eff %d, mil %d",
                        roundintby(lp->lnd_effic, 5),
                        roundintby(lp->lnd_item[I_MILIT], 10));
-           estimate = lp->lnd_item[I_MILIT] * lp->lnd_effic / 100.0;
 
-           if (chance((spy + lp->lnd_vis) / 20.0)) {
+           if (chance((spy + vis) / 20.0)) {
                int t;
                t = lp->lnd_tech - 20 + roll(40);
                t = MAX(t, 0);
@@ -389,11 +384,6 @@ intelligence_report(int destination, struct lndstr *lp, int spy,
     if (destination != player->cnum) {
        wu(0, destination, "%s%s%s", buf1, buf2, buf3);
     }
-
-    if (lp->lnd_ship < 0 || lcp->l_flags & L_MARINE)
-       estimate *= lp->lnd_def;
-
-    return estimate;
 }
 
 /* Used by the spy command to count land units in a sector.  If used
@@ -809,7 +799,7 @@ lnd_missile_interdiction(struct emp_qelem *list, coord newx, coord newy,
                            MI_INTERDICT);
     if (dam) {
        mpr(victim, "missile interdiction mission does %d damage!\n", dam);
-       collateral_damage(newx, newy, dam, 0);
+       collateral_damage(newx, newy, dam);
     }
     qp = msl_list.q_forw;
     while (qp != msl_list.q_forw) {
@@ -845,28 +835,14 @@ lnd_fort_interdiction(struct emp_qelem *list,
            continue;
        if (getrel(getnatp(fsect.sct_own), victim) >= NEUTRAL)
            continue;
-       gun = fsect.sct_item[I_GUN];
-       if (gun < 1)
-           continue;
        range = roundrange(fortrange(&fsect));
        trange = mapdist(newx, newy, fsect.sct_x, fsect.sct_y);
        if (trange > range)
            continue;
-       if (fsect.sct_item[I_MILIT] < 5)
-           continue;
-       shell = fsect.sct_item[I_SHELL];
-       if (shell < 1)
-           shell += supply_commod(fsect.sct_own, fsect.sct_x, fsect.sct_y,
-                                  I_SHELL, 1);
-       if (shell < 1)
-           continue;
-       shell--;
-       fsect.sct_item[I_SHELL] = shell;
+       dam = fort_fire(&fsect);
        putsect(&fsect);
-       if (gun > 7)
-           gun = 7;
-       guneff = landgun((int)fsect.sct_effic, gun);
-       dam = (int)guneff;
+       if (dam < 0)
+           continue;
        totdam += dam;
        mpr(victim, "Incoming fire does %d damage!\n", dam);
        wu(0, fsect.sct_own,
@@ -912,8 +888,8 @@ lnd_hardtarget(struct lndstr *lp)
 
     getsect(lp->lnd_x, lp->lnd_y, &sect);
     return (int)((lp->lnd_effic / 100.0) *
-                (10 + dchr[sect.sct_type].d_dstr * 2 + lp->lnd_spd / 2.0
-                 - lp->lnd_vis));
+                (10 + dchr[sect.sct_type].d_dstr * 2 + lnd_spd(lp) / 2.0
+                 - lnd_vis(lp)));
 }
 
 static int
@@ -939,7 +915,7 @@ lnd_pathcost(struct lndstr *lp, double pathcost)
 {
     double effspd;
 
-    effspd = lp->lnd_spd;
+    effspd = lnd_spd(lp);
     if (lchr[(int)lp->lnd_type].l_flags & L_SUPPLY)
        effspd *= lp->lnd_effic * 0.01;
 
@@ -1015,7 +991,7 @@ lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor,
                continue;
            }
        }
-       if ((!intrchr[INT_RAIL].in_enable || sect.sct_rail == 0)
+       if (!SCT_HAS_RAIL(&sect)
            && lnd_mobtype(&llp->unit.land) == MOB_RAIL) {
            if (together) {
                pr("no rail system in %s\n", xyas(newx, newy, actor));
@@ -1141,26 +1117,17 @@ lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
     struct nstr_item ni;
     struct lndstr land;
     int rel, rel2;
-    double dam = 0.0;
+    int dam, dam2;
     int dist;
-    int shell;
-    int gun;
     int range;
 
+    dam = 0;
     snxtitem_all(&ni, EF_LAND);
     while (nxtitem(&ni, &land)) {
-       if (land.lnd_frg == 0)
-           continue;
        if ((land.lnd_x == x) && (land.lnd_y == y))
            continue;
-       if (land.lnd_ship >= 0)
-           continue;
-       if (land.lnd_land >= 0)
-           continue;
-       if (land.lnd_effic < LAND_MINFIREEFF)
-           continue;
-       /* Do we have mil? */
-       if (land.lnd_item[I_MILIT] <= 0)
+       /* Too ponderous for defensive support */
+       if (defending && (lchr[(int)land.lnd_type].l_flags & L_HEAVY))
            continue;
        rel = getrel(getnatp(land.lnd_own), attacker);
        rel2 = getrel(getnatp(land.lnd_own), victim);
@@ -1168,42 +1135,33 @@ lnd_support(natid victim, natid attacker, coord x, coord y, int defending)
            ((rel != ALLIED) || (rel2 != AT_WAR)))
            continue;
 
-       /* do we have supplies? */
-       if (!has_supply(&land))
-           continue;
-
        /* are we in range? */
        dist = mapdist(land.lnd_x, land.lnd_y, x, y);
 
-       range = roundrange(effrange(land.lnd_frg, land.lnd_tech));
+       range = roundrange(lnd_fire_range(&land));
        if (dist > range)
            continue;
 
-       shell = land.lnd_item[I_SHELL];
-       gun = land.lnd_item[I_GUN];
-
-       if (shell == 0 || gun == 0)
+       dam2 = lnd_fire(&land);
+       putland(land.lnd_uid, &land);
+       if (dam2 < 0)
            continue;
 
-       use_supply(&land);
        if (defending)
            nreport(land.lnd_own, N_FIRE_BACK, victim, 1);
        else
            nreport(land.lnd_own, N_FIRE_L_ATTACK, victim, 1);
-       if (roll(100) < land.lnd_acc) {
-           dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
-                              land.lnd_ammo, shell) / 2;
-       } else {
-           dam += landunitgun(land.lnd_effic, land.lnd_dam, gun,
-                              land.lnd_ammo, shell);
-       }
+       if (roll(100) < lnd_acc(&land))
+           dam2 /= 2;
+       dam += dam2;
        if (land.lnd_own != attacker)
            wu(0, land.lnd_own,
               "%s supported %s at %s\n",
               prland(&land), cname(attacker), xyas(x, y, land.lnd_own));
     }
-    return (int)dam;
+    return dam;
 }
+
 int
 lnd_can_attack(struct lndstr *lp)
 {
@@ -1215,31 +1173,81 @@ lnd_can_attack(struct lndstr *lp)
     return 1;
 }
 
+/*
+ * Return mobility required to unlimber LP.
+ */
+static double
+lnd_mob_to_unlimber(struct lndstr *lp)
+{
+    struct lchrstr *lcp = &lchr[(int)lp->lnd_type];
+
+    if (!opt_LIMBER || lcp->l_dam == 0 || lp->lnd_harden != 0)
+       return 0;
+
+    if (lnd_spd(lp) < 22
+       || (lcp->l_flags & (L_HEAVY | L_TRAIN)) == (L_HEAVY | L_TRAIN)) {
+       /*
+        * Slow artillery is towed and needs to be unlimbered.
+        * Heavy railway guns need to be assembled.
+        */
+       return lnd_pathcost(lp, 0.2);
+    }
+
+    return 0;
+}
+
+/*
+ * Unlimber land unit LP.
+ * No effect unless LP is limbered artillery.
+ */
+void
+lnd_unlimber(struct lndstr *lp)
+{
+    double unlimber_mob, mult, newmob;
+
+    unlimber_mob = lnd_mob_to_unlimber(lp);
+    if (unlimber_mob == 0)
+       return;
+
+    mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
+       ? 1.5 : 1.0;
+    newmob = lp->lnd_mobil - (1 + unlimber_mob) / mult;
+    lp->lnd_mobil = (signed char)MAX(-127, floor(newmob));
+    CANT_HAPPEN(lp->lnd_harden != 0);
+    lp->lnd_harden = 1;
+}
+
 /*
  * Increase fortification value of LP.
  * Fortification costs mobility.  Use up to MOB mobility.
+ * Automatically unlimbers.
  * Return actual fortification increase.
  */
 int
 lnd_fortify(struct lndstr *lp, int mob)
 {
     int hard_amt;
-    double mob_used, mult;
+    double mob_used, unlimber_mob, mult;
 
     if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
        return 0;
 
     mob_used = MIN(lp->lnd_mobil, mob);
-    if (mob_used < 0)
-       return 0;
-
     mult = has_helpful_engineer(lp->lnd_x, lp->lnd_y, lp->lnd_own)
        ? 1.5 : 1.0;
+    unlimber_mob = lnd_mob_to_unlimber(lp);
+
+    hard_amt = (int)(mob_used * mult - unlimber_mob);
+    if (hard_amt < 1)
+       /*
+        * FIXME unlimber_mob > land_mob_max breaks fortify command
+        * FIXME > etu_per_update * land_mob_scale breaks auto-fort. at update
+        */
+       return 0;
 
-    hard_amt = (int)(mob_used * mult);
     if (lp->lnd_harden + hard_amt > land_mob_max) {
        hard_amt = land_mob_max - lp->lnd_harden;
-       mob_used = ceil(hard_amt / mult);
+       mob_used = ceil((hard_amt + unlimber_mob) / mult);
     }
 
     lp->lnd_mobil -= (int)mob_used;
@@ -1276,28 +1284,9 @@ void
 lnd_set_tech(struct lndstr *lp, int tlev)
 {
     struct lchrstr *lcp = lchr + lp->lnd_type;
-    int tech_diff = tlev - lcp->l_tech;
 
-    if (CANT_HAPPEN(tech_diff < 0)) {
-      tlev -= tech_diff;
-      tech_diff = 0;
-    }
+    if (CANT_HAPPEN(tlev < lcp->l_tech))
+       tlev = 0;
 
     lp->lnd_tech = tlev;
-    lp->lnd_att = (float)LND_ATTDEF(lcp->l_att, tech_diff);
-    lp->lnd_def = (float)LND_ATTDEF(lcp->l_def, tech_diff);
-    lp->lnd_vul = (int)LND_VUL(lcp->l_vul, tech_diff);
-    lp->lnd_spd = (int)LND_SPD(lcp->l_spd, tech_diff);
-    lp->lnd_vis = (int)LND_VIS(lcp->l_vis, tech_diff);
-    lp->lnd_spy = (int)LND_SPY(lcp->l_spy, tech_diff);
-    lp->lnd_rad = (int)LND_RAD(lcp->l_rad, tech_diff);
-    lp->lnd_frg = (int)LND_FRG(lcp->l_frg, tech_diff);
-    lp->lnd_acc = (int)LND_ACC(lcp->l_acc, tech_diff);
-    lp->lnd_dam = (int)LND_DAM(lcp->l_dam, tech_diff);
-    lp->lnd_ammo = (int)LND_AMM(lcp->l_ammo, tech_diff);
-    lp->lnd_aaf = (int)LND_AAF(lcp->l_aaf, tech_diff);
-    lp->lnd_fuelc = (int)LND_FC(lcp->l_fuelc, tech_diff);
-    lp->lnd_fuelu = (int)LND_FU(lcp->l_fuelu, tech_diff);
-    lp->lnd_maxlight = (int)LND_XPL(lcp->l_nxlight, tech_diff);
-    lp->lnd_maxland = (int)LND_MXL(lcp->l_nland, tech_diff);
 }