aboutsummaryrefslogtreecommitdiff
path: root/taipan.c
diff options
context:
space:
mode:
authorB. Watson <yalhcru@gmail.com>2016-01-01 19:26:06 -0500
committerB. Watson <yalhcru@gmail.com>2016-01-01 19:26:06 -0500
commitd3395e701c2a14d30508a7f36c84be620ff2261a (patch)
tree78c83959e9dbcca46954bba72163152ceeec04d8 /taipan.c
parent1a94716516d3786acd1eb34193aafcab2e2dea31 (diff)
downloadtaipan-d3395e701c2a14d30508a7f36c84be620ff2261a.tar.gz
fix calculation for storm sinking the ship
Diffstat (limited to 'taipan.c')
-rw-r--r--taipan.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/taipan.c b/taipan.c
index 27f2adb..8080d13 100644
--- a/taipan.c
+++ b/taipan.c
@@ -74,8 +74,15 @@ extern void clrtoeol();
/**** End of atari-specific stuff */
+/* old version of this used to just 'return randl()%clamp'.
+ If clamp were 0, the return value would be the unclamped
+ result from randl() (x % 0 == x, in cc65). If it were 1,
+ the return value would always be 1 (no randomness there). */
unsigned long randclamp(unsigned long clamp) {
- return randl() % clamp;
+ unsigned long r = randl();
+ if(clamp == 0) return clamp;
+ if(clamp == 1) return r & 0x01;
+ return r % clamp;
}
/* wrapper for cgetc() that returns letters as lowercase only
@@ -893,7 +900,7 @@ int sea_battle(int id, int num_ships) {
// cc65 runtime can't detect it...
// If ((ed * i * id)/2)) works out to 1, anything%1 is 0.
// damage = damage + (randi() % ((ed * i * id)/2)) + (i / 2);
- // The is to avoid to % operator if the 2nd arg would be
+ // The answer is to avoid to % operator if the 2nd arg would be
// 0 or 1: the intended result would just be 0 or 1 anyway.
newdamage = ((ed * i * id)/2) + (i / 2);
@@ -1608,7 +1615,8 @@ void transfer(void)
void quit(void)
{
int choice = 0,
- result = 0;
+ result = 0,
+ damagepct, sunk;
compradores_report();
cputs("Taipan, do you wish me to go to:\r\n");
@@ -1688,7 +1696,7 @@ void quit(void)
num_ships = randi()%((capacity / 5) + guns) + 5;
cprintulong(num_ships);
- cputs("ships of Li Yuen's pirate\r\n");
+ cputs(" ships of Li Yuen's pirate\r\n");
cputs("fleet, Taipan!!\r\n");
timed_getch(TMOUT_3S);
@@ -1739,10 +1747,21 @@ void quit(void)
cputs(" I think we're going down!!\r\n\n");
timed_getch(TMOUT_3S);
- // FIXME: the randclamp() version of this is broken badly!
// if (((damage / capacity * 3) * ((float) randi() / RAND_MAX)) >= 1)
- if(randclamp(damage / capacity * 3) >= 1)
- {
+ // in the float version, damage/capacity*3 is your damage percentage,
+ // scaled 0 (0%) to 3 (100%). So if you're less than 34% damaged,
+ // you have no chance of sinking. If you're 34%-66% damaged, you
+ // have a 1 in 3 chance. If you're over 66%, you have a 2 in
+ // 3 chance.
+ damagepct = damage * 100 / capacity;
+ if(damagepct < 34)
+ sunk = 0;
+ else if(damagepct < 67)
+ sunk = randclamp(3) == 0;
+ else
+ sunk = randclamp(3) != 0;
+
+ if(sunk) {
cputs("We're going down, Taipan!!\r\n");
timed_getch(TMOUT_5S);