From 1eaf8d5e9b5c4df074b07749b118bbb71a022f22 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 15 Apr 2024 13:40:22 -0700 Subject: [PATCH] A crude but servicable growth cycle. Bio grows, populations consume bio and grow or consume too much and crash. THat could set up the first main game dynamic or challenge: don't let your people eat everything and die. How to draw off population? Send them to the stars of course! We can worry about controlling the growth rate later (either directly under user control or affected indirectly though factors the user can control? Something else?) Remember that by Insect Law populations can only reproduce on their home worlds! That constraint isn't in the DB nor the "server" code yet though. --- game.sqlite | Bin 389120 -> 389120 bytes server.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/game.sqlite b/game.sqlite index a2f89f7a7042ecaaf1a8290ad8c3ef47c298b8f8..54b5b4bb1fca84a465e52f129070cfed8c0ed9a6 100644 GIT binary patch delta 334 zcmZp8Al~pme1bHi=tLQ3M$wH4%l#RdHgg1==96PNEY~c_&B~z9#OR!ul9ZEJlAK>$ zkeZj7m{-hlc=E?MX(k}W7cT>%bmAq%Sq_WU3bV5^C^9j5B<7T4q(W6j$IF?p9M-)l z#LCK`%)t~~l3I|To)3~_(qLeaVgXsfd;sJFdsGP8~xlZxy1EgPBNuV;EZVf)fV7TyU=pZ;&(@{#48J=3P?+m{%!o^@n0 MJGZ^Kn6fXdjJ3c delta 334 zcmZp8Al~pme1bG1>qHr6M%Il9%l#P{Hgg1==96Q7A=fO)&B~z9#OR!ul9ZEJlAK>$ zkeZj7m{-jFV)Dm0X(k}W7cT>%bmAq%nO}(23bV5^C^9j5B<7T4q(W6j$IF>8ztFuY z!ph2^%)}I2l3I|To)3{^(vV^SS;6F-UzVCvT2x$`o>-Iu)wniZQkwaN)(3GWRt8O= zl8nsc#GK@m)I_NK!+4MrHvfx1#mLkpx%pJ0jU40h&Fl?gjEu`C%QbfL3$C2~3|)Z{PBf<()m#=H%^53|Y@QGMUfX KUR=!DTLS. # +import data + + +def turn(): + conn = data.open_db() + cursor = conn.cursor() + life_grows(cursor) + population_grows(cursor) + industry_produces() + vessels_move() + conn.commit() + data.close_db() + + +BIO_GROWTH = 0.03 +POP_GROWTH = 0.0125 + +def life_grows(db_cursor): + print('life_grows') + db_cursor.execute(''' + select id, bio, bio_capacity from planets + where bio > 0 and bio < bio_capacity + ''') + planets = list(db_cursor.fetchall()) + # Get all the results before updating some in this loop. + for planet_id, bio, bio_capacity in planets: + + # We add at least one unit of population per turn. + new_bio = round(bio * BIO_GROWTH) or 1 + + available_capacity = bio_capacity - bio + + if new_bio > available_capacity: + excess_bio = new_bio - available_capacity + new_bio = available_capacity + + else: + excess_bio = 0 + + bio = bio + new_bio + assert bio <= bio_capacity + print(f'setting planet {planet_id} bio to {bio} increment of {new_bio}, excess_bio {excess_bio}') + db_cursor.execute('update planets set bio = ? where id = ?', (bio, planet_id)) + + +def population_grows(db_cursor): + print('population_grows') + db_cursor.execute(''' + select populations.id, populations.pop, planets.id, planets.bio + from populations join planets + on populations.planet = planets.id + ''') + pops = list(db_cursor.fetchall()) + for pop_id, pop, planet_id, bio in pops: + if pop > bio: + # famine and death! Woe! + pop = bio >> 1 + bio //= 10 + else: + bio -= pop # Everyone eats. + new_pop = round(pop * POP_GROWTH) + pop += new_pop + print(f'population {pop_id} of planet {planet_id} is now {pop}, bio is {bio}') + db_cursor.execute('update planets set bio = ? where id = ?', (bio, planet_id)) + db_cursor.execute('update populations set pop = ? where id = ?', (pop, pop_id)) + + +def industry_produces(): + print('industry_produces') + +def vessels_move(): + print('vessels_move') + + +if __name__ == '__main__': + turn()