def test_one_object_single_turn_move(self): state = { 'turn': 2500, 'width': 1000, 'locatables': { 0: { 'x': 480, 'y': 235, 'z': 187 } }, 'actions': {} } # actual speed is speed**2 updates = [{ 'locatable_id': 0, 'seq': 0, 'x_t': 422, 'y_t': 210, 'z_t': 178, 'speed': 10 }] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(results['locatables'], {0: { 'x': 422, 'y': 210, 'z': 178 }}) self.assertEqual(results['actions'], {})
def test_previously_queued_move(self): state = { 'turn': 2500, 'width': 1000, 'locatables': { 0: { 'x': 480, 'y': 235, 'z': 187 } }, 'actions': { 0: [{ 'x_t': 422, 'y_t': 210, 'z_t': 178, 'speed': 10 }] } } updates = [] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(results['locatables'], {0: { 'x': 422, 'y': 210, 'z': 178 }}) self.assertEqual(results['actions'], {})
def test_one_stationary_object(self): state = { 'turn': 2500, 'width': 1000, 'locatables': { 0: { 'x': 456, 'y': 337, 'z': 441 } }, 'actions': {} } updates = [] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(results['locatables'], {0: { 'x': 456, 'y': 337, 'z': 441 }}) self.assertEqual(results['actions'], {})
def test_three_way_cycle_intercept(self): state = { 'turn': 2500, 'width': 1000, 'locatables': { 0: { 'x': 480, 'y': 235, 'z': 187 }, 1: { 'x': 460, 'y': 215, 'z': 207 }, 2: { 'x': 500, 'y': 205, 'z': 190 } }, 'actions': { 0: [{ 'target_id': 1, 'speed': 10 }], 1: [{ 'target_id': 2, 'speed': 10 }], 2: [{ 'target_id': 0, 'speed': 10 }] } } updates = [] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(len(results['locatables']), 3) self.assertEqual(results['locatables'][0], results['locatables'][1]) self.assertEqual(results['locatables'][0], results['locatables'][2]) self.assertTrue(results['locatables'][0] == { 'x': 480, 'y': 235, 'z': 187 } or results['locatables'][0] == { 'x': 460, 'y': 215, 'z': 207 } or results['locatables'][0] == { 'x': 500, 'y': 205, 'z': 190 }) self.assertEqual(results['actions'], {})
def test_create_with_existing(self): state = { 'turn': 2500, 'width': 1000, 'seq': 3, 'entities': [{ 'pk': 0, 'type': 'species', 'name': 'Human', 'plural_name': 'Humans', 'growth_rate': 15, 'gravity_immune': True, 'temperature_immune': True, 'radiation_immune': True, }, { 'pk': 1, 'type': 'ship', 'x': 480, 'y': 235, 'owner_id': 0 }, { 'pk': 2, 'type': 'movement_order', 'actor_id': 1, 'seq': 0, 'x_t': 637, 'y_t': 786, 'warp': 8 }] } updates = { 0: [{ 'action': 'create', 'type': 'movement_order', 'actor_id': 1, 'seq': 1, 'x_t': 422, 'y_t': 210, 'warp': 10 }], } S = engine.GameState(state, updates) systems.UpdateSystem().process(S.manager) orders = S.manager.get_entities('orders') self.assertEqual(len(orders), 2) self.assertEqual( orders[3].serialize(), { 'pk': 3, 'type': 'movement_order', 'actor_id': 1, 'seq': 1, 'x_t': 422, 'y_t': 210, 'warp': 10 })
def test_invalid_ownership(self): state = { 'turn': 2500, 'width': 1000, 'seq': 2, 'entities': [ { 'pk': 1, 'type': 'planet', 'x': 300, 'y': 600, 'gravity': 27, 'temperature': 36, 'radiation': 45, 'ironium_conc': 67, 'boranium_conc': 78, 'germanium_conc': 82, 'ironium': 20, 'boranium': 30, 'germanium': 40, 'owner_id': 0, # Not an entity in the pool. 'population': 1000, } ] } with self.assertRaises(exceptions.ValidationError) as e: S = engine.GameState(state, {}) self.assertEqual(str(e.exception), "'owner_id' is not an existing entity.")
def test_species(self): state = { 'turn': 2500, 'width': 1000, 'seq': 1, 'entities': [ { 'pk': 0, 'type': 'species', 'name': 'Human', 'plural_name': 'Humans', 'growth_rate': 15, 'gravity_immune': True, 'temperature_immune': True, 'radiation_immune': True, }, ] } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(len(results['entities']), 1) species = next(E for E in results['entities'] if E['pk'] == 0) self.assertEqual(species['type'], 'species') self.assertEqual(species['name'], 'Human') self.assertEqual(species['plural_name'], 'Humans') self.assertEqual(species['growth_rate'], 15) self.assertEqual(species['gravity_immune'], True) self.assertEqual(species['temperature_immune'], True) self.assertEqual(species['radiation_immune'], True)
def test_empty_universe(self): state = {'turn': 2500, 'width': 1000, 'entities': []} S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results, {'turn': 2501, 'width': 1000, 'seq': 0, 'entities': []})
def test_empty_universe(self): state = {'turn': 2500, 'width': 1000, 'entities': {}} updates = {} S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results, {'turn': 2501, 'width': 1000, 'entities': {}})
def test_planet(self): state = { 'turn': 2500, 'width': 1000, 'seq': 2, 'entities': [ { 'pk': 0, 'type': 'species', 'name': 'Human', 'plural_name': 'Humans', 'growth_rate': 15, 'gravity_immune': True, 'temperature_immune': True, 'radiation_immune': True, }, { 'pk': 1, 'type': 'planet', 'x': 300, 'y': 600, 'gravity': 27, 'temperature': 36, 'radiation': 45, 'ironium_conc': 67, 'boranium_conc': 78, 'germanium_conc': 82, 'ironium': 20, 'boranium': 30, 'germanium': 40, 'owner_id': 0, 'population': 1000, } ] } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(len(results['entities']), 2) planet = next(E for E in results['entities'] if E['pk'] == 1) self.assertEqual(planet['type'], 'planet') self.assertEqual(planet['x'], 300) self.assertEqual(planet['y'], 600) self.assertEqual(planet['gravity'], 27) self.assertEqual(planet['temperature'], 36) self.assertEqual(planet['radiation'], 45) self.assertEqual(planet['ironium_conc'], 67) self.assertEqual(planet['boranium_conc'], 78) self.assertEqual(planet['germanium_conc'], 82) self.assertEqual(planet['ironium'], 20) self.assertEqual(planet['boranium'], 30) self.assertEqual(planet['germanium'], 40) self.assertEqual(planet['owner_id'], 0) self.assertGreater(planet['population'], 0)
def test_empty_universe(self): state = {'turn': 2500, 'width': 1000, 'locatables': {}, 'actions': {}} updates = [] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results, { 'turn': 2501, 'width': 1000, 'locatables': {}, 'actions': {} })
def test_one_stationary_object(self): state = {'turn': 2500, 'width': 1000, 'seq': 1, 'entities': [{'pk': 0, 'type': 'ship', 'x': 456, 'y': 337}]} S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], [{'pk': 0, 'type': 'ship', 'x': 456, 'x_prev': 456, 'y': 337, 'y_prev': 337}] )
def test_one_stationary_object(self): state = {'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 456, 'y': 337, 'queue': []}}} updates = {} S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], {0: {'type': 'ship', 'x': 456, 'x_prev': 456, 'y': 337, 'y_prev': 337, 'queue': []}} )
def test_target_moving_object(self): state = { 'turn': 2500, 'width': 1000, 'locatables': { 0: { 'x': 480, 'y': 235, 'z': 187 }, 1: { 'x': 460, 'y': 215, 'z': 207 } }, 'actions': { 0: [{ 'target_id': 1, 'speed': 10 }], 1: [{ 'x_t': 465, 'y_t': 220, 'z_t': 197, 'speed': 10 }] } } updates = [] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(results['locatables'], { 0: { 'x': 465, 'y': 220, 'z': 197 }, 1: { 'x': 465, 'y': 220, 'z': 197 } }) self.assertEqual(results['actions'], {})
def test_slow_motion(self): state = { 'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 500, 'y': 500, 'queue': [{'x_t': 501, 'y_t': 500, 'warp': 1}]}} } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], {0: {'type': 'ship', 'x': 501, 'x_prev': 500, 'y': 500, 'y_prev': 500, 'queue': []}} )
def test_one_object_single_turn_move(self): state = {'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 480, 'y': 235, 'queue': []}}} # actual speed is warp**2 updates = {0: [{'seq': 0, 'x_t': 422, 'y_t': 210, 'warp': 10}]} S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], {0: {'type': 'ship', 'x': 422, 'x_prev': 480, 'y': 210, 'y_prev': 235, 'queue': []}} )
def test_replace_queued_move(self): state = {'turn': 2500, 'width': 1000, 'entities': { 0: {'type': 'ship', 'x': 480, 'y': 235, 'queue': [{'x_t': 637, 'y_t': 786, 'warp': 8}]}}} updates = {0: [{'seq': 0, 'x_t': 422, 'y_t': 210, 'warp': 10}]} S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], {0: {'type': 'ship', 'x': 422, 'x_prev': 480, 'y': 210, 'y_prev': 235, 'queue': []}} )
def test_population_growth(self): state = { 'turn': 2500, 'width': 1000, 'entities': { 0: { 'type': 'species', 'singular_name': 'Human', 'plural_name': 'Humans', 'growth_rate': 15, }, 1: { 'type': 'planet', 'population': 1000, 'x': 480, 'y': 235, 'owner_id': 0, 'queue': [], }, } } S = engine.GameState(state, {}) results = S.generate() self.assertEqual( results, { 'turn': 2501, 'width': 1000, 'entities': { 0: { 'type': 'species', 'singular_name': 'Human', 'plural_name': 'Humans', 'growth_rate': 15, }, 1: { 'type': 'planet', 'population': 1150, 'x': 480, 'x_prev': 480, 'y': 235, 'y_prev': 235, 'owner_id': 0, 'queue': [], }, } })
def test_add_to_queued_moves(self): state = { 'turn': 2500, 'width': 1000, 'locatables': { 0: { 'x': 480, 'y': 235, 'z': 187 } }, 'actions': { 0: [{ 'x_t': 422, 'y_t': 210, 'z_t': 178, 'speed': 10 }] } } updates = [{ 'locatable_id': 0, 'seq': 1, 'x_t': 637, 'y_t': 786, 'z_t': 255, 'speed': 8 }] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(results['locatables'], {0: { 'x': 422, 'y': 210, 'z': 178 }}) self.assertEqual( results['actions'], {0: [{ 'x_t': 637, 'y_t': 786, 'z_t': 255, 'speed': 8 }]})
def test_mutual_intercept(self): state = {'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 480, 'y': 235, 'queue': [{'target_id': 1, 'warp': 10}]}, 1: {'type': 'ship', 'x': 460, 'y': 215, 'queue': [{'target_id': 0, 'warp': 10}]}}} updates = {} S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(len(results['entities']), 2) coordinates = {(entity['x'], entity['y']) for entity in results['entities'].values()} self.assertEqual(coordinates, {(470, 225)}) self.assertEqual(results['entities'][0]['queue'], []) self.assertEqual(results['entities'][1]['queue'], [])
def test_three_way_cycle_intercept(self): state = {'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 480, 'y': 235, 'queue': [{'target_id': 1, 'warp': 10}]}, 1: {'type': 'ship', 'x': 460, 'y': 215, 'queue': [{'target_id': 2, 'warp': 10}]}, 2: {'type': 'ship', 'x': 500, 'y': 205, 'queue': [{'target_id': 0, 'warp': 10}]}}} updates = {} S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(len(results['entities']), 3) coordinates = {(entity['x'], entity['y']) for entity in results['entities'].values()} self.assertEqual(coordinates, {(480, 217)}) self.assertTrue(all(not entity['queue'] for entity in results['entities'].values()))
def test_target_stationary_object(self): state = {'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 480, 'y': 235, 'queue': [{'target_id': 1, 'warp': 10}]}, 1: {'type': 'ship', 'x': 460, 'y': 215, 'queue': []}}} updates = {} S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], {0: {'type': 'ship', 'x': 460, 'x_prev': 480, 'y': 215, 'y_prev': 235, 'queue': []}, 1: {'type': 'ship', 'x': 460, 'x_prev': 460, 'y': 215, 'y_prev': 215, 'queue': []}} )
def test_target_can_barely_reach(self): state = { 'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 560, 'y': 315, 'queue': [{'target_id': 1, 'warp': 10}]}, 1: {'type': 'ship', 'x': 460, 'y': 215, 'queue': [{'x_t': 660, 'y_t': 215, 'warp': 10}]}} } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], {0: {'type': 'ship', 'x': 560, 'x_prev': 560, 'y': 215, 'y_prev': 315, 'queue': []}, 1: {'type': 'ship', 'x': 560, 'x_prev': 460, 'y': 215, 'y_prev': 215, 'queue': [{'x_t': 660, 'y_t': 215, 'warp': 10}]}} )
def test_slow_motion(self): state = { 'turn': 2500, 'width': 1000, 'seq': 2, 'entities': [ {'pk': 0, 'type': 'ship', 'x': 500, 'y': 500}, {'pk': 1, 'type': 'movement_order', 'actor_id': 0, 'seq': 0, 'x_t': 501, 'y_t': 500, 'warp': 1}, ] } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], [{'pk': 0, 'type': 'ship', 'x': 501, 'x_prev': 500, 'y': 500, 'y_prev': 500}] )
def test_target_stops_short(self): state = { 'turn': 2500, 'width': 1000, 'entities': {0: {'type': 'ship', 'x': 560, 'y': 315, 'queue': [{'target_id': 1, 'warp': 10}]}, 1: {'type': 'ship', 'x': 460, 'y': 215, 'queue': [{'x_t': 510, 'y_t': 215, 'warp': 10}]}} } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) # Ship 0 should have moved directly north for the first 50 ticks, then northwest for the remainder # i.e. roughly 35 lightyears in each direction self.assertEqual( results['entities'], {0: {'type': 'ship', 'x': 524, 'x_prev': 560, 'y': 230, 'y_prev': 315, 'queue': [{'target_id': 1, 'warp': 10}]}, 1: {'type': 'ship', 'x': 510, 'x_prev': 460, 'y': 215, 'y_prev': 215, 'queue': []}} )
def test_one_object_single_turn_move(self): state = { 'turn': 2500, 'width': 1000, 'seq': 1, 'entities': [ {'pk': 0, 'type': 'ship', 'x': 480, 'y': 235}, {'pk': 1, 'type': 'movement_order', 'actor_id': 0, 'seq': 0, 'x_t': 422, 'y_t': 210, 'warp': 10} ] } # actual speed is warp**2 S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], [{'pk': 0, 'type': 'ship', 'x': 422, 'x_prev': 480, 'y': 210, 'y_prev': 235}] )
def test_mutual_intercept(self): state = { 'turn': 2500, 'width': 1000, 'seq': 4, 'entities': [ {'pk': 0, 'type': 'ship', 'x': 480, 'y': 235}, {'pk': 1, 'type': 'ship', 'x': 460, 'y': 215}, {'pk': 2, 'type': 'movement_order', 'actor_id': 0, 'seq': 0, 'target_id': 1, 'warp': 10}, {'pk': 3, 'type': 'movement_order', 'actor_id': 1, 'seq': 0, 'target_id': 0, 'warp': 10}, ] } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(len(results['entities']), 2) coordinates = {(entity['x'], entity['y']) for entity in results['entities']} self.assertEqual(coordinates, {(470, 225)})
def test_multi_turn_single_move(self): state = { 'turn': 2500, 'width': 1000, 'locatables': { 0: { 'x': 480, 'y': 235, 'z': 187 } }, 'actions': {} } # actual speed is speed**2 updates = [{ 'locatable_id': 0, 'seq': 0, 'x_t': 168, 'y_t': 870, 'z_t': 390, 'speed': 10 }] S = engine.GameState(state, updates) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual(results['locatables'], {0: { 'x': 438, 'y': 321, 'z': 215 }}) self.assertEqual( results['actions'], {0: [{ 'x_t': 168, 'y_t': 870, 'z_t': 390, 'speed': 10 }]})
def test_target_stationary_object(self): state = { 'turn': 2500, 'width': 1000, 'seq': 3, 'entities': [ {'pk': 0, 'type': 'ship', 'x': 480, 'y': 235}, {'pk': 1, 'type': 'ship', 'x': 460, 'y': 215}, {'pk': 2, 'type': 'movement_order', 'actor_id': 0, 'seq': 0, 'target_id': 1, 'warp': 10}, ] } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(results['turn'], 2501) self.assertEqual(results['width'], 1000) self.assertEqual( results['entities'], [{'pk': 0, 'type': 'ship', 'x': 460, 'x_prev': 480, 'y': 215, 'y_prev': 235}, {'pk': 1, 'type': 'ship', 'x': 460, 'x_prev': 460, 'y': 215, 'y_prev': 215}] )
def test_lose_ownership(self): state = { 'turn': 2500, 'width': 1000, 'entities': [ { 'pk': 0, 'type': 'species', 'name': 'Human', 'plural_name': 'Humans', 'growth_rate': 15, 'gravity_immune': True, 'temperature_immune': True, 'radiation_immune': True, }, { 'pk': 1, 'type': 'planet', 'population': 0, 'x': 480, 'y': 235, 'gravity': 50, 'temperature': 50, 'radiation': 50, 'ironium_conc': 50, 'boranium_conc': 50, 'germanium_conc': 50, 'owner_id': 0, }, ] } S = engine.GameState(state, {}) results = S.generate() self.assertEqual(len(results['entities']), 2) self.assertNotIn('population', results['entities'][1]) self.assertNotIn('owner_id', results['entities'][1])