From 5ebcb27009ebafb11379d3061d5f88300d366f59 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 15 Apr 2024 11:51:41 -0700 Subject: [PATCH] Minor cleanup, comments. --- README.md | 9 +++++++++ population.py | 17 ++++++++++------- stars.py | 13 ++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b8e0531..6ba2860 100644 --- a/README.md +++ b/README.md @@ -238,3 +238,12 @@ sentient species that's 1% of those, eh? Population can move around. Planets, space stations, ships, where else? +Just a note: I'm not happy with the Poisson layout of the star systems, +too uniform, I know I've said that elsewhere. I'm also not happy with +the radiuseses of the stars, the little ones are hard to click on. It's +not so bad because you can click on the labels, but still. It would be +neat to have a scattering of stars on the background, just tiny points of +light, cosmetic only, no game function. Then the "real" stars could all +be several pixels uh radius for clickability eh? + + diff --git a/population.py b/population.py index 01501ea..40b003e 100644 --- a/population.py +++ b/population.py @@ -17,11 +17,8 @@ # You should have received a copy of the GNU General Public License # along with game. If not see . # -''' -''' from random import sample -# Database stuff. SQL, etc. TABLES = [ '''\ @@ -36,21 +33,27 @@ TABLES = [ INITIAL_POP = 1000 +INITIAL_NUMBER_OF_SENTIENT_PEOPLES = 12 + def init_db(conn): print('Generating population data.') + + # Find the not-too-crappy planets. c = conn.cursor() c.execute('''\ select id, bio, industrial_capacity from planets where bio > 10000 and industrial_capacity > 10000 ''') - home_worlds = sample(list(c.fetchall()), 12) + # Note that bio is initialized to full capacity whereas industry is 0. + + # Select some and set initial population and industry. + home_worlds = sample(list(c.fetchall()), INITIAL_NUMBER_OF_SENTIENT_PEOPLES) for planet_id, bio, industrial_capacity in home_worlds: bio -= INITIAL_POP # We know bio is above 10000 from the query above. ind = min(INITIAL_POP, industrial_capacity) - people_name = get_name_of_planets_star(c, planet_id) - people_name += 'ians' + people_name = get_name_of_planets_star(c, planet_id) + 'ians' c.execute('update planets set bio = ?, industry = ? where id = ?', (bio, ind, planet_id)) c.execute('insert into populations(species, pop, planet) values (?, ?, ?)', (people_name, INITIAL_POP, planet_id)) print(planet_id, bio, industrial_capacity) @@ -59,6 +62,7 @@ def init_db(conn): def get_name_of_planets_star(db_cursor, planet_id): + # My first ever JOIN statement! Squeeeee! db_cursor.execute('''\ select stars.name from stars join planets @@ -68,4 +72,3 @@ def get_name_of_planets_star(db_cursor, planet_id): (planet_id,) ) return db_cursor.fetchone()[0] - diff --git a/stars.py b/stars.py index 267aaf8..1270a41 100644 --- a/stars.py +++ b/stars.py @@ -149,6 +149,10 @@ def get_planets_for_star_named(conn, name): # about to happen (drawing these planet data into the GUI) so it # would be daft to worry about it (after working this all out, that # is.) + # + # Now that I can do JOIN's I could write a mighty SQL statement that + # did all the work in the DB! I could be like the old programmers of + # yore! for ordo, bio_capacity, industrial_capacity in c.fetchall(): assert bio_capacity >= 0 @@ -168,11 +172,15 @@ def generate_stars(width, height, minimum_distance_between_stars): name = generate_name() while name in unique_names: name = generate_name() + # I did get a name collision early on! unique_names.add(name) yield x, y, round(1 + expovariate(1)), name def how_many_planets(): + ''' + Return a non-negative integer, the number of planets a star should have. + ''' n = round(gauss(5, 5)) while n < 0: n = round(gauss(5, 5)) @@ -180,9 +188,12 @@ def how_many_planets(): def has_life(): - return random() < 0.1 # Ten percent chance of life + # Ten percent chance of life + return random() < 0.1 + def generate_planets_for_star(c): + # Only call this just after creating a star. star_id = c.lastrowid for ordo in range(1, how_many_planets() + 1): bio_capacity = round(lognormvariate(10, 5))