Created
January 24, 2016 17:01
-
-
Save pmslavin/21a4e404940ec01aee9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Generates full set of planet names across all galaxies | |
in the Elite series of games. | |
Original name generation algorithm by David Braben and Ian Bell. | |
Leestian Evil Juice also to Christian Pinder and the Oolite crew. | |
Usage: | |
$ python ./elitenames.py <galaxy> | |
Paul Slavin <pmslavin@gmail.com> | |
""" | |
import sys | |
seed0 = 0x5A4A | |
seed1 = 0x0248 | |
seed2 = 0xB753 | |
w0 = seed0 | |
w1 = seed1 | |
w2 = seed2 | |
digrams0 = "ABOUSEITILETSTONLONUTHNO" | |
digrams1 = "..LEXEGEZACEBISOUSESARMAINDIREA.ERATENBERALAVETIEDORQUANTEISRION" | |
digrams = digrams0 + digrams1 | |
def rotl8(b): | |
b &= 0xFF | |
return (b & 127) << 1 | b >> 7 | |
def twist(): | |
global w0, w1, w2 | |
wtemp = w0 + w1 + w2 | |
wtemp &= (1 << 16) - 1 | |
w0 = w1 | |
w1 = w2 | |
w2 = wtemp | |
def select(w2): | |
mask = (1 << 5) - 1 | |
sbits = (w2 >> 8) & mask | |
return sbits << 1 | |
def getPair(idx): | |
dg = digrams1[idx:idx+2] | |
return dg.replace(".", "") | |
def isLongName(w0): | |
return bool(w0 & (1 << 6)) | |
def makeName(): | |
name = "" | |
isLong = isLongName(w0) | |
for _ in xrange(3): | |
idx = select(w2) | |
name += getPair(idx) | |
twist() | |
if isLong: | |
idx = select(w2) | |
name += getPair(idx) | |
twist() | |
return name | |
def resetState(): | |
global w0, w1, w2 | |
w0 = seed0 | |
w1 = seed1 | |
w2 = seed2 | |
def setGalaxy(g): | |
global w0, w1, w2 | |
resetState() | |
while(g > 1): | |
w0t = rotl8(w0 >> 8) | |
w0b = rotl8(w0 & 0xFF) | |
w0 = (w0t << 8) | w0b | |
w1t = rotl8(w1 >> 8) | |
w1b = rotl8(w1 & 0xFF) | |
w1 = (w1t << 8) | w1b | |
w2t = rotl8(w2 >> 8) | |
w2b = rotl8(w2 & 0xFF) | |
w2 = (w2t << 8) | w2b | |
g -= 1 | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
gal = int(sys.argv[1]) | |
else: | |
gal = 1 | |
setGalaxy(gal) | |
for _ in xrange(256): | |
print(makeName()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment