def initialize_settlement(settlement): with perf_timer('Initializing settlement {} ({}), pop. {}'.format( settlement, settlement.id, settlement.population_default)): residences = settlement.building_set.filter( type=world.models.buildings.Building.RESIDENCE).all() fields = settlement.building_set.filter( type=world.models.buildings.Building.GRAIN_FIELD).all() total_field_workplaces = sum(field.max_workers() for field in fields) other_workplaces = settlement.building_set.exclude( type__in=(world.models.buildings.Building.RESIDENCE, world.models.buildings.Building.GRAIN_FIELD)).all() total_other_workplaces = sum(j.max_workers() for j in other_workplaces) settlement.population = settlement.population_default settlement.save() assigned_workers = 0 for i in range(settlement.population): npc = generate_npc(i, residences, settlement) npc.save() settlement.building_set.filter( type=world.models.buildings.Building.GRAIN_FIELD).update( field_production_counter=1500)
def battle_tick(battle: Battle): with perf_timer("Tick {} for {}".format(battle.get_latest_turn().num, battle)): create_next_turn(battle) unit_movement(battle) unit_attack(battle) if check_end(battle): battle.current = False battle.save()
def initialize_world(world): with perf_timer('Initializing world {} ({})'.format(world, world.id)): if world.initialized: raise AlreadyInitializedException( "World {} already initialized!".format(world)) for organization in world.organization_set.all(): initialize_organization(organization) for unit in world.worldunit_set.all(): initialize_unit(unit) for tile in world.tile_set.all(): initialize_tile(tile) world.initialized = True world.save()
def handle(self, *args, **options): for world_id in options['world_id']: try: world = World.objects.get(pk=world_id) except World.DoesNotExist: raise CommandError('World with id "%s" does not exist' % world_id) with perf_timer('World "%s" turn' % world_id): pass_turn(world) self.stdout.write( self.style.SUCCESS('Passed turn in world "%s"' % world_id))
def initialize_unit(unit): with perf_timer('Initializing unit {} ({})'.format(unit, unit.id)): for i in range(unit.generation_size): world.models.npcs.NPC.objects.create( name="Soldier {} of {}".format(i, unit), male=random.getrandbits(1), able=True, age_months=20 * 12, origin=unit.location, residence=None, location=unit.location, workplace=None, unit=unit, trained_soldier=random.getrandbits(4) == 0, skill_fighting=random.randint(0, 80)) unit.generation_size = 0 unit.save()
def handle(self, *args, **options): for world_id in options['world_id']: try: world = World.objects.get(pk=world_id) except World.DoesNotExist: raise CommandError('World with id "%s" does not exist' % world_id) try: with perf_timer('World "%s" initialization' % world_id): initialize_world(world) except AlreadyInitializedException: raise CommandError( 'World with id "%s" is already initialized' % world_id) self.stdout.write( self.style.SUCCESS('Successfully initialized world "%s"' % world_id))
def pass_turn(world): with perf_timer('Turn in {} ({})'.format(world, world.id)): with perf_timer('Block world'): world.blocked_for_turn = True world.save() # with perf_timer('Delete dead realms'): # worldwide_delete_dead_realms(world) with perf_timer('Pause characters'): worldwide_pause_characters(world) with perf_timer('Travels'): worldwide_character_travels(world) with perf_timer('Restore character hours'): worldwide_restore_character_hours(world) with perf_timer('Unit maintenance'): worldwide_unit_maintenance(world) with perf_timer('Battle starts'): worldwide_battle_starts(world) with perf_timer('Battle joins'): worldwide_battle_joins(world) with perf_timer('Battle turns'): worldwide_battle_turns(world) with perf_timer('Battle triggers'): worldwide_trigger_battles(world) with perf_timer('Elections'): worldwide_elections(world) with perf_timer('Conquests'): worldwide_conquests(world) with perf_timer('Barbarian generation'): worldwide_barbarian_generation(world) # with perf_timer('NPC resicence assignment'): # worldwide_npc_residence_assignment() with perf_timer('NPC job updates'): worldwide_npc_job_updates(world) with perf_timer('Building production'): worldwide_building_production(world) # with perf_timer('Trade'): # worldiwde_trade() with perf_timer('Food consumption'): worldwide_food_consumption(world) with perf_timer('Population changes'): worldwide_population_changes(world) # with perf_timer('Food spoilage'): # worldwide_food_spoilage() with perf_timer('Public order'): worldwide_public_order(world) with perf_timer('Taxes'): worldwide_taxes(world) with perf_timer('Finalize turn'): world.current_turn += 1 world.save() world.broadcast("messaging/messages/new_turn.html", 'A month goes by...', {'world': world}) world.blocked_for_turn = False world.save()