Version 1.6: less bitwise op deps, save/load functionality, no cheatmode anymore

This commit is contained in:
Luxferre
2024-01-22 21:39:01 +02:00
parent d050177507
commit dfc78807ab
2 changed files with 57 additions and 61 deletions
+42 -45
View File
@@ -1,5 +1,5 @@
# awlite: POSIX AWK port of Text Elite
# Version 1.5.8
# Version 1.6.0
# Original high-level algorithms from Text Elite 1.5 by Ian Bell, 1999, 2015
# Porting, corrections, improvements and optimizations by Luxferre, 2024
#
@@ -9,7 +9,6 @@
#
# Gameplay differences from the original C version of TE 1.5:
# - multiple typos corrected in text strings
# - the "cash" and "sneak" commands are only available to the REPL if running with -v CHEAT=1
# - galaxy jumps are no longer free and cost 5000 credits each (like in classic Elite and Oolite)
# - cargo hold expansion also is non-free (400 credits) and can only be done once from 20t to 35t (like in classic Elite)
# - alien items are available by default, can be overridden with -v NO_ALIEN_ITEMS=1
@@ -17,6 +16,8 @@
# - the "politically correct" goods names are turned on with -v CENSORED=1
# - the following goods have been renamed: "Robot Slaves" to "Robots", "Liquor/Wines" to "Liquors", "Gem-Strones" to "Gem-stones"
# - market and local info tables are better aligned
# - the "cash" and "sneak" commands are removed since v1.6 (use saves to cheat)
# - state saving functionality since v1.6 with "save" and "load" commands
# ... to be continued ...
# utility functions
@@ -75,26 +76,6 @@ function bw_and(a, b, v, r) {
}
return int(r)
}
function bw_or(a, b, v, r) {
v = 1; r = 0
while(a > 0 || b > 0) {
if((a%2) == 1 || (b%2) == 1) r += v
a = int(a/2)
b = int(b/2)
v *= 2
}
return int(r)
}
function bw_xor(a, b, v, r) {
v = 1; r = 0
while(a > 0 || b > 0) {
if((a%2) != (b%2)) r += v
a = int(a/2)
b = int(b/2)
v *= 2
}
return int(r)
}
# ============================ #
# Game code itself starts here #
@@ -156,8 +137,9 @@ function makesystem(seeds, sinfo, p1, p2, p3, p4, lnf, ecof, sname) {
sinfo["y"] = int(seeds[0] / 256)
sinfo["govtype"] = int(seeds[1] / 8) % 8
sinfo["economy"] = int(seeds[0] / 256) % 8
if(sinfo["govtype"] <= 1) sinfo["economy"] = bw_or(sinfo["economy"], 2)
ecof = bw_xor(sinfo["economy"], 7)
if(sinfo["govtype"] <= 1)
sinfo["economy"] = 2 + (sinfo["economy"]%2) + (sinfo["economy"]>3?4:0)
ecof = 7 - sinfo["economy"]
sinfo["techlev"] = (sinfo["x"] % 4) + ecof + int(sinfo["govtype"] / 2)
if((sinfo["govtype"] % 2) == 1) sinfo["techlev"]++
sinfo["population"] = 4 * (sinfo["techlev"]) + sinfo["economy"] + sinfo["govtype"] + 1
@@ -378,15 +360,6 @@ function dojump(s, dest, d, p, si) {
return 1
}
# dojump without fuel expense
function dosneak(s, kp, r) {
kp = player["fuel"]
player["fuel"] = 666
r = dojump(s)
player["fuel"] = kp
return r
}
# jump to next galaxy (planet number is preserved)
function dogalhyp() {
if(player["cash"] > game["galhypcost"]) {
@@ -486,12 +459,6 @@ function dofuel(f) {
return 1
}
# (cheat) alter cash command implementation
function docash(s) {
player["cash"] += int(10 * s)
if(player["cash"] < 0) player["cash"] = 0
}
# show stock market
function domkt() {
displaymarket()
@@ -505,17 +472,47 @@ function dohelp() {
printf("\nSell product amount")
printf("\nFuel amount (buy amount LY of fuel)")
printf("\nJump planetname (limited by fuel)")
if(CHEAT) printf("\nSneak planetname (any distance - no fuel cost)")
printf("\nGalhyp (jumps to next galaxy)")
printf("\nInfo planetname (prints info on system)")
printf("\nMkt (shows market prices)")
printf("\nLocal (lists systems within 7 lightyears)")
if(CHEAT) printf("\nCash number (alters cash)")
printf("\nHold (expand cargo bay to %dt)", game["cargoexpsize"])
printf("\nSave playername (save game into current working directory)")
printf("\nLoad playername (load game from current working directory)")
printf("\nQuit or ^D (exit)")
printf("\nHelp (display this text)")
printf("\nRand (toggle RNG)")
printf("\n\nAbbreviations allowed eg. b fo 5 = Buy Food 5, m= Mkt")
printf("\n\nAbbreviations allowed eg. b fo 5 = Buy Food 5, m = Mkt")
}
# Save/load functionality
function dosave(savename, fs, fn) {
gsub(/[^[:alnum:]]/, "_", savename) # sanitize all non-alnum chars
player["cargo"] = assocser(shipshold, "@")
fs = assocser(player, "|")
delete player["cargo"]
fn = savename ".awlite"
printf("%s\n", fs) >> fn # write the savefile
close(fn)
printf("\nGame saved in %s", fn)
}
function doload(savename, fn, fs) {
gsub(/[^[:alnum:]]/, "_", savename) # sanitize all non-alnum chars
fn = savename ".awlite"
if((getline fs < fn) > 0) { # read the savefile
split("", player) # clear the player array
split("", shipshold) # clear the shipshold array
assocdes(fs, player, "|")
assocdes(player["cargo"], shipshold, "@")
delete player["cargo"]
split("", globalseeds) # init galaxy seeds
buildgalaxy(player["galaxynum"], globalseeds) # build the current galaxy
genmarket(0, economies[player["currentplanet"]]) # build local market
printf("\nGame loaded from %s", fn)
} else printf("\nError reading the savefile!")
close(fn)
}
# Game init procedure: first thing to call in the BEGIN block
@@ -614,7 +611,7 @@ BEGIN {
split("", globalseeds) # init galaxy seeds
buildgalaxy(player["galaxynum"], globalseeds) # build the current galaxy
genmarket(0, economies[player["currentplanet"]]) # build local market
printf("\nWelcome to awlite 1.5.8 (based on Text Elite 1.5)\n")
printf("\nWelcome to awlite 1.6.0\n")
dohelp()
# start the REPL
printf("\n\nCash: %.1f> ", player["cash"]/10)
@@ -635,8 +632,8 @@ BEGIN {
else if(action == "j" || action == "jump") dojump(paramstr)
else if(action == "f" || action == "fuel") dofuel(paramstr)
else if(action == "hold") dohold(game["cargoexpsize"])
else if(CHEAT && action == "cash") docash(paramstr)
else if(CHEAT && action == "sneak") dosneak(paramstr)
else if(action == "save") dosave(paramstr)
else if(action == "load") doload(paramstr)
else printf("Unknown command")
printf("\n\nCash: %.1f> ", player["cash"]/10)
}