From 1227d2c931ed4c33e03946847aa237c2ef4d4420 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 12 Jan 2014 18:24:06 +0100 Subject: [PATCH] build: Stop abuse of construction material random rounding Construction materials required for building a ship, plane or land unit are rounded randomly. Crafty players exploit this to save materials: they put just enough materials there so that build succeeds when it rounds down. Then they simply keep trying until it succeeds. Planes and land units are built at 10%, so rounding happens when materials for 100% aren't multiples of ten. If they're below ten, you can even build without materials. In the stock game, this is the case for linf, and many plane types. Ships are built at 20%, so multiples of five aren't rounded. Ship building never rounds in the stock game. Prevent the abuse of random rounding by requiring the required fractional amount rounded up to be present. Don't change the actual charging of materials; that's still randomly rounded. Signed-off-by: Markus Armbruster --- src/lib/commands/buil.c | 43 +++++++++++++---------------------- tests/build/final.xdump | 8 +++---- tests/build/journal.log | 8 +++---- tests/build/units/02-planes-2 | 4 ---- tests/build/units/03-lands-3 | 4 ---- tests/smoke/final.xdump | 2 +- tests/smoke/journal.log | 8 +++---- 7 files changed, 29 insertions(+), 48 deletions(-) diff --git a/src/lib/commands/buil.c b/src/lib/commands/buil.c index 6ce1081e..89a42f8e 100644 --- a/src/lib/commands/buil.c +++ b/src/lib/commands/buil.c @@ -56,7 +56,6 @@ static int pick_unused_unit_uid(int); static int build_bridge(char); static int build_bspan(struct sctstr *sp); static int build_btower(struct sctstr *sp); -static void build_material_use(short[], short[], int); static int sector_can_build(struct sctstr *, short[], int, int, char *); static void build_charge(struct sctstr *, short[], int, double, int); static int build_can_afford(double, int, char *); @@ -208,14 +207,13 @@ static int build_ship(struct sctstr *sp, int type, int tlev) { struct mchrstr *mp = &mchr[type]; - short mat[I_MAX+1], mat_100[I_MAX+1]; + short mat[I_MAX+1]; int work; struct shpstr ship; - memset(mat_100, 0, sizeof(mat_100)); - mat_100[I_LCM] = mp->m_lcm; - mat_100[I_HCM] = mp->m_hcm; - build_material_use(mat, mat_100, SHIP_MINEFF); + memset(mat, 0, sizeof(mat)); + mat[I_LCM] = mp->m_lcm; + mat[I_HCM] = mp->m_hcm; work = SHP_BLD_WORK(mp->m_lcm, mp->m_hcm); if (sp->sct_type != SCT_HARBR) { @@ -262,14 +260,13 @@ static int build_land(struct sctstr *sp, int type, int tlev) { struct lchrstr *lp = &lchr[type]; - short mat[I_MAX+1], mat_100[I_MAX+1]; + short mat[I_MAX+1]; int work; struct lndstr land; - memset(mat_100, 0, sizeof(mat_100)); - mat_100[I_LCM] = lp->l_lcm; - mat_100[I_HCM] = lp->l_hcm; - build_material_use(mat, mat_100, LAND_MINEFF); + memset(mat, 0, sizeof(mat)); + mat[I_LCM] = lp->l_lcm; + mat[I_HCM] = lp->l_hcm; work = LND_BLD_WORK(lp->l_lcm, lp->l_hcm); if (sp->sct_type != SCT_HEADQ) { @@ -378,7 +375,7 @@ static int build_plane(struct sctstr *sp, int type, int tlev) { struct plchrstr *pp = &plchr[type]; - short mat[I_MAX+1], mat_100[I_MAX+1]; + short mat[I_MAX+1]; int work; struct plnstr plane; double eff = PLANE_MINEFF / 100.0; @@ -388,10 +385,9 @@ build_plane(struct sctstr *sp, int type, int tlev) /* Always use at least 1 mil to build a plane */ if (mil == 0 && pp->pl_crew > 0) mil = 1; - memset(mat_100, 0, sizeof(mat_100)); - mat_100[I_LCM] = pp->pl_lcm; - mat_100[I_HCM] = pp->pl_hcm; - build_material_use(mat, mat_100, PLANE_MINEFF); + memset(mat, 0, sizeof(mat)); + mat[I_LCM] = pp->pl_lcm; + mat[I_HCM] = pp->pl_hcm; work = PLN_BLD_WORK(pp->pl_lcm, pp->pl_hcm); if (sp->sct_type != SCT_AIRPT && !player->god) { @@ -714,21 +710,12 @@ build_btower(struct sctstr *sp) return 1; } -static void -build_material_use(short mat[], short mat_100[], int effic) -{ - int i; - - for (i = I_MAX; i > I_NONE; i--) { - mat[i] = mat_100[i] ? roundavg(mat_100[i] * (effic / 100.0)) : 0; - } -} - static int sector_can_build(struct sctstr *sp, short mat[], int work, int effic, char *what) { int i, avail; + double needed; if (sp->sct_effic < 60 && !player->god) { pr("Sector %s is not 60%% efficient.\n", @@ -737,11 +724,13 @@ sector_can_build(struct sctstr *sp, short mat[], int work, } for (i = I_NONE + 1; i <= I_MAX; i++) { - if (sp->sct_item[i] < mat[i]) { + needed = mat[i] * (effic / 100.0); + if (sp->sct_item[i] < needed) { pr("Not enough materials in %s\n", xyas(sp->sct_x, sp->sct_y, player->cnum)); return 0; } + mat[i] = roundavg(needed); } avail = (work * effic + 99) / 100; diff --git a/tests/build/final.xdump b/tests/build/final.xdump index 75c97cd3..5fc6dc65 100644 --- a/tests/build/final.xdump +++ b/tests/build/final.xdump @@ -10,8 +10,8 @@ owner xloc yloc des effic mobil off loyal terr0 terr1 terr2 terr3 dterr xdist yd 0 -6 0 0 0 0 0 0 0 0 0 0 0 -6 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 0 -4 0 0 0 0 0 0 0 0 0 0 0 -4 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 1 -2 0 5 100 127 0 0 0 0 0 0 0 -2 0 0 0 0 1 5 0 0 0 0 0 1 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 -2 1 1 14 100 127 0 0 0 0 0 0 0 1 1 3 0 0 0 14 0 0 0 0 0 2 100 2 0 0 0 0 0 0 0 0 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 -2 3 1 14 60 127 0 0 0 0 0 0 0 3 1 7 0 0 0 14 0 0 0 0 0 2 100 3 0 0 0 0 0 0 0 0 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 +2 1 1 14 100 127 0 0 0 0 0 0 0 1 1 3 0 0 0 14 0 0 0 0 0 2 100 2 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 +2 3 1 14 60 127 0 0 0 0 0 0 0 3 1 7 0 0 0 14 0 0 0 0 0 2 100 3 0 0 0 0 0 0 0 0 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 2 5 1 14 59 127 0 0 0 0 0 0 0 5 1 11 0 0 1 14 0 0 0 0 0 2 100 50 0 0 0 0 0 0 0 500 500 500 0 500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 0 7 1 0 0 0 0 0 0 0 0 0 0 7 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 0 9 1 0 0 0 0 0 0 0 0 0 0 9 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 @@ -21,8 +21,8 @@ owner xloc yloc des effic mobil off loyal terr0 terr1 terr2 terr3 dterr xdist yd 2 -3 1 5 100 127 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 3 6 2 26 59 127 0 0 0 0 0 0 0 6 2 11 0 0 1 26 0 0 0 0 0 3 100 50 0 0 0 0 0 0 0 500 500 500 0 500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 0 8 2 0 0 0 0 0 0 0 0 0 0 8 2 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 0 -10 2 0 0 0 0 0 0 0 0 0 0 -10 2 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 diff --git a/tests/build/journal.log b/tests/build/journal.log index 71583822..819a77eb 100644 --- a/tests/build/journal.log +++ b/tests/build/journal.log @@ -371,8 +371,8 @@ Play#2 output Play#2 1 Thu Jan 1 00:00:00 1970 Play#2 output Play#2 1 COMMODITIES deliver-- distribute Play#2 output Play#2 1 sect sgpidbolhr sgpidbolhr sh gun pet iron dust bar oil lcm hcm rad - Play#2 output Play#2 1 1,1 * .......... .......... 0 0 0 0 0 0 0 3 4 0 - Play#2 output Play#2 1 3,1 * .......... .......... 0 0 0 0 0 0 0 3 3 0 + Play#2 output Play#2 1 1,1 * .......... .......... 0 0 0 0 0 0 0 2 2 0 + Play#2 output Play#2 1 3,1 * .......... .......... 0 0 0 0 0 0 0 3 4 0 Play#2 output Play#2 1 5,1 * .......... .......... 0 0 0 0 0 0 500 500 500 500 Play#2 output Play#2 1 3 sectors Play#2 output Play#2 6 0 621 @@ -516,8 +516,8 @@ Play#3 output Play#3 1 Thu Jan 1 00:00:00 1970 Play#3 output Play#3 1 COMMODITIES deliver-- distribute Play#3 output Play#3 1 sect sgpidbolhr sgpidbolhr sh gun pet iron dust bar oil lcm hcm rad - Play#3 output Play#3 1 2,2 ! .......... .......... 0 0 0 0 0 0 0 3 3 0 - Play#3 output Play#3 1 4,2 ! .......... .......... 0 0 0 0 0 0 0 3 3 0 + Play#3 output Play#3 1 2,2 ! .......... .......... 0 0 0 0 0 0 0 2 2 0 + Play#3 output Play#3 1 4,2 ! .......... .......... 0 0 0 0 0 0 0 3 4 0 Play#3 output Play#3 1 6,2 ! .......... .......... 0 0 0 0 0 0 500 500 500 500 Play#3 output Play#3 1 3 sectors Play#3 output Play#3 6 0 624 diff --git a/tests/build/units/02-planes-2 b/tests/build/units/02-planes-2 index 2f596c2b..e492edb5 100644 --- a/tests/build/units/02-planes-2 +++ b/tests/build/units/02-planes-2 @@ -12,12 +12,8 @@ build p -1,1 f2 1 101 build p -1,1 f2 1 79 | not an airfield, no materials, inefficient build p -1,1 f2 -| Note: fractional cms are randomly rounded, which can make build -| succeed, upsetting the rest of the test build p 3:5,1 f2 | insufficient materials: 1,1 short 1l/1h, 3,1 short 1m -| Note: fractional cms are randomly rounded, which can make build -| succeed in 1,1, upsetting the rest of the test move m -1,1 1 jh move l -1,1 1 jjh move h -1,1 1 jjh diff --git a/tests/build/units/03-lands-3 b/tests/build/units/03-lands-3 index c516684b..5b761eb7 100644 --- a/tests/build/units/03-lands-3 +++ b/tests/build/units/03-lands-3 @@ -11,13 +11,9 @@ n build l 0,2 linf 1 101 build l 0,2 linf 1 39 | not an headquarters, no materials, inefficient -| Note: fractional cms are randomly rounded, which can make build -| succeed in 4,2, upsetting the rest of the test build l 0,2 linf build l 4:6,2 linf | insufficient materials: 2,2 short 1l, 4,2 short 1h -| Note: fractional cms are randomly rounded, which can make build -| succeed in either sector, upsetting the rest of the test move l 0,2 1 jjh move h 0,2 1 jh build l 2:4,2 linf diff --git a/tests/smoke/final.xdump b/tests/smoke/final.xdump index 23fecc54..db7a9aa0 100644 --- a/tests/smoke/final.xdump +++ b/tests/smoke/final.xdump @@ -677,7 +677,7 @@ owner xloc yloc des effic mobil off loyal terr0 terr1 terr2 terr3 dterr xdist yd 1 5 -11 10 100 127 0 0 0 0 0 0 0 11 -11 0 93 100 0 10 100 0 0 0 100 1 1000 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 1 0 0 0 0 0 1 7 -11 14 100 127 0 0 0 0 0 0 0 11 -11 657 79 100 1 14 91 0 0 0 68 1 1000 30 180 0 200 0 0 0 0 0 200 200 0 0 0 30 180 0 200 0 0 0 0 0 200 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 1 0 0 0 0 0 1 9 -11 21 100 127 0 0 0 0 0 0 0 11 -11 490 75 100 1 21 85 0 0 0 57 1 1000 0 0 0 0 0 10 0 0 50 100 0 0 0 0 0 0 0 0 0 10 0 0 50 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 1 0 0 0 0 0 -1 11 -11 13 100 127 0 0 0 0 0 0 0 11 -11 667 71 100 1 13 78 0 0 0 45 1 1000 105 181 0 1226 2071 473 0 0 740 4113 4521 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 1 0 0 0 0 0 +1 11 -11 13 100 127 0 0 0 0 0 0 0 11 -11 667 71 100 1 13 78 0 0 0 45 1 1000 105 181 0 1226 2071 473 0 0 740 4114 4522 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 1 0 0 0 0 0 1 13 -11 31 100 127 0 12 0 0 0 0 0 11 -11 649 64 100 1 31 67 0 0 0 25 1 967 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 1 0 0 0 0 0 1 15 -11 26 100 127 0 0 0 0 0 0 0 11 -11 656 57 100 1 26 56 0 0 0 5 1 1000 25 200 17 0 0 0 0 0 0 200 200 0 0 0 25 200 25 0 0 0 0 0 0 200 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 1 0 0 0 0 0 0 17 -11 0 0 0 0 0 0 0 0 0 0 17 -11 0 -9 0 1 0 0 0 50 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 healthy 0 0 0 0 0 0 0 0 diff --git a/tests/smoke/journal.log b/tests/smoke/journal.log index 53a89af7..0e7c6720 100644 --- a/tests/smoke/journal.log +++ b/tests/smoke/journal.log @@ -14039,7 +14039,7 @@ Play#0 output Play#0 1 Play#0 output Play#0 1 sects eff civ mil shell gun pet iron dust oil pln ship unit money Play#0 output Play#0 1 1 33 100% 31K 210 514 22 0 9.1K 849 1.2K 3 5 4 46K - Play#0 output Play#0 1 639.34 + Play#0 output Play#0 1 639.37 Play#0 output Play#0 1 8 29 91% 25K 110 101 0 0 14K 2.8K 527 0 0 0 21K Play#0 output Play#0 1 329.62 Play#0 output Play#0 1 2 30 28% 4.9K 0 0 0 0 1.7K 1.3K 170 0 0 0 35K @@ -14249,7 +14249,7 @@ Play#0 output Play#0 1 10 -11,-11 g .......... .......... 0 0 0 0 225 0 0 0 0 0 Play#0 output Play#0 1 1 3,-11 i .......... 0......63. 1 0 0 0 0 0 0 600 300 0 Play#0 output Play#0 1 1 5,-11 m .......... ...0...... 0 0 0 1 0 0 0 0 0 0 - Play#0 output Play#0 1 1 7,-11 * .......... 1.2....22. 0 0 0 0 0 0 0 197 199 0 + Play#0 output Play#0 1 1 7,-11 * .......... 1.2....22. 0 0 0 0 0 0 0 198 200 0 Play#0 output Play#0 1 1 9,-11 r .......... ....0.01.. 0 0 0 0 10 0 50 100 0 0 Play#0 output Play#0 1 1 11,-11 w .......... .......... 273 0 0 2519 523 0 849 2514 3122 0 Play#0 output Play#0 1 1 13,-11 e .......... .......... 0 0 0 0 0 0 0 0 0 0 @@ -15146,7 +15146,7 @@ Play#0 output Play#0 1 Play#0 output Play#0 1 sects eff civ mil shell gun pet iron dust oil pln ship unit money Play#0 output Play#0 1 1 33 100% 31K 232 659 30 994 8.9K 524 1.0K 3 5 4 51K - Play#0 output Play#0 1 756.23 + Play#0 output Play#0 1 756.27 Play#0 output Play#0 1 8 29 100% 29K 105 215 5 0 15K 2.5K 541 0 0 0 23K Play#0 output Play#0 1 447.37 Play#0 output Play#0 1 2 30 33% 6.3K 0 0 0 0 3.2K 1.2K 523 0 0 0 36K @@ -15358,7 +15358,7 @@ Play#0 output Play#0 1 1 5,-11 m .......... ...0...... 0 0 0 1 0 0 0 0 0 0 Play#0 output Play#0 1 1 7,-11 * .......... 1.2....22. 180 0 200 0 0 0 0 200 200 0 Play#0 output Play#0 1 1 9,-11 r .......... ....0.01.. 0 0 0 0 10 0 50 100 0 0 - Play#0 output Play#0 1 1 11,-11 w .......... .......... 25 0 778 2295 498 0 828 3245 3775 0 + Play#0 output Play#0 1 1 11,-11 w .......... .......... 25 0 778 2295 498 0 828 3246 3776 0 Play#0 output Play#0 1 1 13,-11 e .......... .......... 0 0 0 0 0 0 0 0 0 0 Play#0 output Play#0 1 1 15,-11 ! .......... 20.....22. 200 11 0 0 0 0 0 200 200 0 Play#0 output Play#0 1 8 21,-11 p .......... .......0.. 0 0 0 0 1 0 0 75 0 0