]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/attsub.c
subs: Factor lnd_insque() out of lnd_sel(), ask_olist(), ...
[empserver] / src / lib / subs / attsub.c
index 5f3bbe71b88b758cd3c34067eda341e6578cc810..34f664cc8cc7804f7665379bcb5619dfe0f72bb8 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-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1996-2000
- *     Markus Armbruster, 2006-2011
+ *     Markus Armbruster, 2006-2014
  */
 
 #include <config.h>
 
 #include <ctype.h>
 #include <math.h>
+#include "chance.h"
 #include "combat.h"
 #include "empobj.h"
 #include "file.h"
@@ -549,8 +550,8 @@ board_abort(struct combat *off, struct combat *def)
            pr("Victim ship moves faster than you do!\n");
            if (def->own)
                wu(0, def->own,
-                  "%s (#%d) %s failed to catch %s\n",
-                  cname(aship.shp_own), aship.shp_own,
+                  "%s %s failed to catch %s\n",
+                  prnatid(aship.shp_own),
                   pr_com(0, off, def->own), pr_com(0, def, def->own));
            return abort_attack();
        }
@@ -638,14 +639,10 @@ att_approach(struct combat *off, struct combat *def)
 
 /* The attack is valid.  Tell the attacker about what they're going to hit */
 
-int
+void
 att_show(struct combat *def)
 {
-    /* Note that we tell the player about the treaty BEFORE we tell them
-       about the item.  If we didn't, then they gain free information */
     if (def->type == EF_SECTOR) {
-       if (!trechk(player->cnum, def->own, LANATT))
-           return abort_attack();
        pr("%s is a %d%% %s %s with approximately %d military.\n",
           xyas(def->x, def->y, player->cnum),
           roundintby((int)def->eff, 10),
@@ -654,19 +651,10 @@ att_show(struct combat *def)
        if (map_set(player->cnum, def->x, def->y, def->sct_dcp->d_mnem, 0))
            writemap(player->cnum);
     } else if (def->type == EF_SHIP || def->type == EF_LAND) {
-       if (def->type == EF_SHIP) {
-           if (!trechk(player->cnum, def->own, SEAATT))
-               return abort_attack();
-       } else {
-           if (!trechk(player->cnum, def->own, LNDATT))
-               return abort_attack();
-       }
        pr("%s is about %d%% efficient and has approximately %d mil on board.\n",
           prcom(0, def), roundintby((int)def->eff, 10),
           roundintby(def->troops, 10));
     }
-    /* Ok, everything is fine */
-    return 0;
 }
 
 /* Attack and assault ask the user which kind of support they want */
@@ -1080,18 +1068,11 @@ ask_olist(int combat_mode, struct combat *off, struct combat *def,
                land_answer[(int)land.lnd_army] != 'Y')
                continue;
        }
-       if (!(llp = malloc(sizeof(struct ulist)))) {
-           logerror("Malloc failed in attack!\n");
-           abort_attack();
-           return;
-       }
-       memset(llp, 0, sizeof(struct ulist));
-       emp_insque(&llp->queue, olist);
+       llp = lnd_insque(&land, olist);
+       llp->supplied = 1;
        llp->mobil = mobcost;
-       llp->unit.land = land;
        llp->x = llp->unit.land.lnd_x;
        llp->y = llp->unit.land.lnd_y;
-       llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
        llp->eff = llp->unit.land.lnd_effic;
        if (lnd_spyval(&land) > *a_spyp)
            *a_spyp = lnd_spyval(&land);
@@ -1210,18 +1191,11 @@ get_dlist(struct combat *def, struct emp_qelem *list, int a_spy,
            continue;
        intelligence_report(player->cnum, &land, a_spy,
                            "Scouts report defending unit:");
-       if (!(llp = malloc(sizeof(struct ulist)))) {
-           logerror("Malloc failed in attack!\n");
-           abort_attack();
-           return;
-       }
-       memset(llp, 0, sizeof(struct ulist));
-       emp_insque(&llp->queue, list);
+       llp = lnd_insque(&land, list);
        llp->supplied = lnd_supply_all(&land);
-       llp->unit.land = land;
+       llp->mobil = 0.0;
        llp->x = llp->unit.land.lnd_x;
        llp->y = llp->unit.land.lnd_y;
-       llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
        llp->eff = llp->unit.land.lnd_effic;
        if (lnd_spyval(&land) > *d_spyp)
            *d_spyp = lnd_spyval(&land);
@@ -1440,7 +1414,7 @@ put_oland(struct emp_qelem *list)
        llp->mobil = 0.0;
        putland(llp->unit.land.lnd_uid, &llp->unit.land);
        if (llp->unit.land.lnd_own != player->cnum) {
-           emp_remque((struct emp_qelem *)llp);
+           emp_remque(&llp->queue);
            free(llp);
        } else
            get_oland(A_ATTACK, llp);
@@ -1519,16 +1493,12 @@ att_reacting_units(struct combat *def, struct emp_qelem *list, int a_spy,
        wu(0, land.lnd_own, "%s reacts to %s.\n",
           prland(&land), xyas(land.lnd_x, land.lnd_y, land.lnd_own));
 
-       llp = malloc(sizeof(struct ulist));
-
-       memset(llp, 0, sizeof(struct ulist));
+       llp = lnd_insque(&land, list);
        llp->supplied = 1;
+       llp->mobil = 0.0;
        llp->x = origx;
        llp->y = origy;
-       llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
-       llp->unit.land = land;
        llp->eff = land.lnd_effic;
-       emp_insque(&llp->queue, list);
        if (lnd_spyval(&land) > *d_spyp)
            *d_spyp = lnd_spyval(&land);
 
@@ -1836,12 +1806,12 @@ att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
      * since a single dead guy normally wouldn't cause a commander to
      * rethink his strategies, but 50 dead guys might.
      */
-    odds += (random() % 11 - 5) / 100.0;
+    odds += (roll(11) - 6) / 100.0;
     if (odds < 0.0)
        odds = 0.1;
     if (odds > 1.0)
        odds = 1.0;
-    recalctime = 8 + (random() % 43);
+    recalctime = 7 + roll(43);
     while (!success && ototal) {
        if (chance(odds)) {
            pr("!");
@@ -1857,9 +1827,9 @@ att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
        if (((a_cas + d_cas) % 70) == 69)
            pr("\n");
        if (recalctime-- <= 0) {
-           recalctime = 8 + (random() % 43);
+           recalctime = 7 + roll(43);
            odds = att_calcodds(ototal, dtotal);
-           odds += (random() % 11 - 5) / 100.0;
+           odds += (roll(11) - 6) / 100.0;
            if (odds < 0.0)
                odds = 0.1;
            if (odds > 1.0)
@@ -1981,8 +1951,8 @@ att_fight(int combat_mode, struct combat *off, struct emp_qelem *olist,
     nreport(player->cnum, news_item, def->own, 1);
     if (def->own) {
        wu(0, def->own,
-          "%s (#%d) lost %d troops %s %s\nWe lost %d troops defending\n",
-          cname(player->cnum), player->cnum, a_cas,
+          "%s lost %d troops %s %s\nWe lost %d troops defending\n",
+          prnatid(player->cnum), a_cas,
           action, pr_com(0, def, def->own), d_cas);
     }
 
@@ -2049,8 +2019,8 @@ take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
     int to_take = CASUALTY_LUMP;
     int biggest_troops = 0, index = -1;
     int n, tot_troops = 0, biggest_mil, cas;
-    struct emp_qelem *qp, *biggest;
-    struct ulist *llp;
+    struct emp_qelem *qp;
+    struct ulist *llp, *biggest;
 
     for (n = 0; n <= off->last; ++n) {
        if (off[n].type != EF_BAD) {
@@ -2111,14 +2081,13 @@ take_casualty(int combat_mode, struct combat *off, struct emp_qelem *olist)
 
        if (llp->unit.land.lnd_item[I_MILIT] > biggest_mil) {
            biggest_mil = llp->unit.land.lnd_item[I_MILIT];
-           biggest = qp;
+           biggest = llp;
        }
     }
     if (biggest == NULL)
        return CASUALTY_LUMP - to_take;
 
-    llp = (struct ulist *)biggest;
-    cas = lnd_take_casualty(combat_mode, llp, to_take);
+    cas = lnd_take_casualty(combat_mode, biggest, to_take);
     return CASUALTY_LUMP - (to_take - cas);
 }
 
@@ -2154,8 +2123,8 @@ att_empty_attack(int combat_mode, int ototal, struct combat *def)
     if (ototal <= 0) {
        if (def->own && player->cnum != def->own) {
            wu(0, def->own,
-              "%s (#%d) considered %sing you @%s\n",
-              cname(player->cnum), player->cnum,
+              "%s considered %sing you @%s\n",
+              prnatid(player->cnum),
               att_mode[combat_mode], xyas(def->x, def->y, def->own));
        }
        pr("No troops for %s...\n", att_mode[combat_mode]);
@@ -2586,9 +2555,5 @@ sector_strength(struct sctstr *sp)
     double base = sp->sct_type == SCT_MOUNT ? 2.0 : 1.0;
     double d = base + (dchr[sp->sct_type].d_dstr - base) * def;
 
-    if (d > dchr[sp->sct_type].d_dstr)
-       d = dchr[sp->sct_type].d_dstr;
-    if (d < base)
-       d = base;
-    return d;
+    return LIMIT_TO(d, base, dchr[sp->sct_type].d_dstr);
 }