From: Markus Armbruster Date: Fri, 16 Sep 2016 07:07:52 +0000 (+0200) Subject: subs: Factor common military counting out of shoo() and conv() X-Git-Tag: v4.4.0~104 X-Git-Url: http://git.pond.sub.org/?p=empserver;a=commitdiff_plain;h=ffd6651f8650696eeef4ee65072cb5839c295d51 subs: Factor common military counting out of shoo() and conv() Put the new function security_strength() next to military_control(), because that one does a similar count. Signed-off-by: Markus Armbruster --- diff --git a/include/prototypes.h b/include/prototypes.h index 2e4e9506a..d31ed61d5 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -399,6 +399,7 @@ extern int check_trade_ok(struct trdstr *); /* coastal.c */ extern void set_coastal(struct sctstr *, int, int); /* control.c */ +extern int security_strength(struct sctstr *, int *); extern int military_control(struct sctstr *); extern int abandon_askyn(struct sctstr *, i_type, int, struct ulist *); extern int would_abandon(struct sctstr *, i_type, int, struct ulist *); diff --git a/src/lib/commands/conv.c b/src/lib/commands/conv.c index 04ac92a22..76a1523a7 100644 --- a/src/lib/commands/conv.c +++ b/src/lib/commands/conv.c @@ -28,7 +28,7 @@ * * Known contributors to this file: * Dave Pare, 1986 - * Markus Armbruster, 2004-2015 + * Markus Armbruster, 2004-2016 */ /* @@ -48,10 +48,8 @@ conv(void) struct sctstr sect; struct nstr_sect nstr; int uwtoconvert, newuw, totaluw, uw; - int maxpop, civ, mil, adj_mob, mob; + int maxpop, civ, mil, nsec, adj_mob, mob; double security_extra = 1.0; - struct lndstr land; - struct nstr_item ni; if (!snxtsct(&nstr, player->argp[1])) return RET_SYN; @@ -68,27 +66,7 @@ conv(void) natp = getnatp(sect.sct_own); maxpop = max_pop(natp->nat_level[NAT_RLEV], §); civ = sect.sct_item[I_CIVIL]; - mil = sect.sct_item[I_MILIT]; - - /* - * Military units count according to the number of - * mil in them. (i.e. attack/defense modifier don't - * count. - */ - snxtitem_xy(&ni, EF_LAND, sect.sct_x, sect.sct_y); - while (nxtitem(&ni, &land)) { - mil += land.lnd_item[I_MILIT]; - - /* Anti-terrorist units count double */ - if (lchr[(int)land.lnd_type].l_flags & L_SECURITY) { - /* - * They also increase the efficiency of - * the conversion process by 10% each. - */ - security_extra += .1; - mil += land.lnd_item[I_MILIT]; - } - } + mil = security_strength(§, &nsec); /* * Must have military control to convert captured civs. */ @@ -110,6 +88,7 @@ conv(void) mob = sect.sct_mobil * 5; /* security troops make conversion more effective */ + security_extra = 1.0 + nsec / 10.0; adj_mob = ldround(((double)mob * security_extra), 1); if (adj_mob < newuw) diff --git a/src/lib/commands/shoo.c b/src/lib/commands/shoo.c index 0cd7ad032..45f9e3290 100644 --- a/src/lib/commands/shoo.c +++ b/src/lib/commands/shoo.c @@ -43,12 +43,10 @@ shoo(void) { struct sctstr sect; struct nstr_sect nstr; - struct nstr_item ni; int nshot; double m; i_type item; struct ichrstr *ip; - struct lndstr land; int targets; char *p; int mil, nsec; @@ -68,18 +66,7 @@ shoo(void) while (nxtsct(&nstr, §)) { if (!player->owner) continue; - mil = sect.sct_item[I_MILIT]; - nsec = 0; - snxtitem_xy(&ni, EF_LAND, sect.sct_x, sect.sct_y); - while (nxtitem(&ni, &land)) { - mil += land.lnd_item[I_MILIT]; - - if (lchr[(int)land.lnd_type].l_flags & L_SECURITY) { - mil += land.lnd_item[I_MILIT]; - nsec++; - } - } - + mil = security_strength(§, &nsec); if (sect.sct_item[item] == 0 || sect.sct_item[I_CIVIL] > mil * 10) continue; nshot = sect.sct_item[item] > targets ? targets : sect.sct_item[item]; diff --git a/src/lib/subs/control.c b/src/lib/subs/control.c index a2076a4fa..3fc5b1f95 100644 --- a/src/lib/subs/control.c +++ b/src/lib/subs/control.c @@ -40,6 +40,33 @@ #include "sect.h" #include "unit.h" +/* + * Return strength of security detail in @sp. + * Store number of land units with security capability in @nsecurity. + */ +int +security_strength(struct sctstr *sp, int *nsecurity) +{ + int strength; + int nsec; + struct nstr_item ni; + struct lndstr land; + + strength = sp->sct_item[I_MILIT]; + nsec = 0; + snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y); + while (nxtitem(&ni, &land)) { + strength += land.lnd_item[I_MILIT]; + if (lchr[land.lnd_type].l_flags & L_SECURITY) { + strength += land.lnd_item[I_MILIT]; + nsec++; + } + } + + *nsecurity = nsec; + return strength; +} + /* * Does the player->owner have military control of this sector? */