diff --git a/game.sqlite b/game.sqlite index 62e2e02..90a750d 100644 Binary files a/game.sqlite and b/game.sqlite differ diff --git a/stars.py b/stars.py index 03ec5be..e315b60 100644 --- a/stars.py +++ b/stars.py @@ -17,7 +17,22 @@ # You should have received a copy of the GNU General Public License # along with game. If not see . # -from random import randint, expovariate, seed +''' +A field of stars. Each has planets. Each planet has...? + +Keep it simple. + +Stars have what qualities? +- Cosmetic + - color + - brightness + - size +- Game stats + - ? + + +''' +from random import randint, expovariate, seed, getrandbits from poisson import poisson @@ -30,21 +45,38 @@ MINIMUM_DISTANCE_BETWEEN_STARS = 160 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 (x INTEGER, y INTEGER, radius INTEGER)', + 'create table stars (id INTEGER PRIMARY KEY, x INTEGER, y INTEGER, radius INTEGER)', ] -def generate_stars(width, height, minimum_distance_between_stars): - for x, y in poisson(width, height, minimum_distance_between_stars): - yield x, y, round(1 + expovariate(1)) - - 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) + c.execute('insert into stars values (?, ?, ?, ?)', values) c.close() conn.commit() @@ -54,3 +86,24 @@ def iter_stars(conn): c.execute('select x, y, radius from stars') yield from c.fetchall() c.close() + + +# Procedural Generation of solar system data + + +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)) + + +def how_many_planets(): + n = round(gauss(5, 5)) + while n < 0: + n = round(gauss(5, 5)) + return n + +