diff --git a/game.sqlite b/game.sqlite index c13cb92..5836a6d 100644 Binary files a/game.sqlite and b/game.sqlite differ diff --git a/stars.py b/stars.py index b620a28..416d1d6 100644 --- a/stars.py +++ b/stars.py @@ -35,7 +35,7 @@ Stars have what qualities? ♈ ♉ ♊ ♋ ♌ ♍ ♎ ♏ ♐ ♑ ♒ ♓ ''' -from random import randint, expovariate, seed, getrandbits, gauss +from random import randint, expovariate, seed, gauss from poisson import poisson @@ -50,28 +50,8 @@ WIDTH, HEIGHT = 10240, 7680 # Database stuff. SQL, etc. -def generate_list_of_u64_for_ids(n): - ''' - Return a sorted list of n random 64-bit integers - (suitable for SQLite rowid columns.) - See https://www.sqlite.org/lang_createtable.html#rowid - ''' - result = set() - safety = 10 - while n > 0: - canidate = getrandbits(63) - while canidate in result: - if not safety: - raise RuntimeError('this should not happen') - safety -= 1 - canidate = getrandbits(63) - result.add(canidate) - n -= 1 - return sorted(result) - - TABLES = [ - 'create table stars (id INTEGER PRIMARY KEY, x INTEGER, y INTEGER, radius INTEGER)', + 'create table stars (id INTEGER PRIMARY KEY AUTOINCREMENT, x INTEGER, y INTEGER, radius INTEGER)', ('''\ create table planets ( id INTEGER PRIMARY KEY, @@ -88,9 +68,8 @@ def init_db(conn): print('Generating star data.') c = conn.cursor() for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS): - c.execute('insert into stars values (?, ?, ?, ?)', values) - star_id = values[0] - generate_planets_for_star(c, star_id) + c.execute('insert into stars(x, y, radius) values (?, ?, ?)', values) + generate_planets_for_star(c) c.close() conn.commit() @@ -106,12 +85,8 @@ def iter_stars(conn): def generate_stars(width, height, minimum_distance_between_stars): - # we could just call getrandbits(64) here for ID and hope for the best. - # it's a large domain (u64) so collisions are unlikely(!). - coords = list(poisson(width, height, minimum_distance_between_stars)) - ids = generate_list_of_u64_for_ids(len(coords)) - for star_id, (x, y) in zip(ids, coords): - yield star_id, x, y, round(1 + expovariate(1)) + for (x, y) in poisson(width, height, minimum_distance_between_stars): + yield x, y, round(1 + expovariate(1)) def how_many_planets(): @@ -120,7 +95,8 @@ def how_many_planets(): n = round(gauss(5, 5)) return n -def generate_planets_for_star(c, star_id): +def generate_planets_for_star(c): + star_id = c.lastrowid for ordo in range(1, how_many_planets() + 1): c.execute('insert into planets(ordo, star) values (?, ?)', (ordo, star_id))