aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/data/test_npc_assignments.py (6529B)


      1 import collections
      2 import copy
      3 import unittest
      4 
      5 import ruamel.yaml
      6 
      7 import npc_assignments
      8 
      9 
     10 class NPCAssignmentTests(unittest.TestCase):
     11     def test_population_regeneration_preserves_shop_binding(self):
     12         assignments = npc_assignments.load_reviewed_groups()
     13         group = {'id': '127/district/0/fixed/4', 'template_code': 'BTMV'}
     14         population = {'playfields': [{'districts': [{'groups': [group]}]}]}
     15 
     16         npc_assignments.apply_to_population(population, assignments)
     17 
     18         self.assertEqual(group['shop_template'], 99601)
     19 
     20     def test_population_regeneration_preserves_recovered_visuals(self):
     21         assignments = npc_assignments.load_reviewed_groups()
     22         group = {'id': '705/district/0/fixed/15', 'template_code': 'MACA'}
     23         population = {'playfields': [{'districts': [{'groups': [group]}]}]}
     24 
     25         npc_assignments.apply_to_population(population, assignments)
     26 
     27         self.assertEqual(group['visual'], {
     28             'head': 40171,
     29             'textures': [[0, 15806], [1, 15809], [2, 15807], [3, 15808], [4, 15805]],
     30             'meshes': [[0, 20038, 0, 2], [0, 40171, 0, 4], [1, 35566, 0, 2]],
     31         })
     32 
     33     def test_population_regeneration_preserves_female_officer_uniforms(self):
     34         assignments = npc_assignments.load_reviewed_groups()
     35         groups = [
     36             {'id': '705/district/0/fixed/9', 'template_code': 'FECA'},
     37             {'id': '705/district/0/fixed/13', 'template_code': 'FELI'},
     38         ]
     39         population = {'playfields': [{'districts': [{'groups': groups}]}]}
     40 
     41         npc_assignments.apply_to_population(population, assignments)
     42 
     43         self.assertEqual(groups[0]['visual'], {
     44             'head': 40209,
     45             'textures': [[0, 15806], [1, 15809], [2, 15807], [3, 15808], [4, 15805]],
     46             'meshes': [[0, 20055, 0, 2], [0, 40209, 0, 4], [1, 35566, 0, 2]],
     47         })
     48         self.assertEqual(groups[1]['visual'], {
     49             'head': 40635,
     50             'textures': [[0, 15806], [1, 15809], [2, 15807], [3, 15808], [4, 15805]],
     51             'meshes': [[0, 20089, 0, 2], [0, 40635, 0, 4], [1, 35566, 0, 2]],
     52         })
     53 
     54     def test_context_variants_keep_their_own_choice_and_evidence(self):
     55         # Define two context choices for the same template code.
     56         catalog = {'TEST': {'scope': 'maintained_population', 'selected': {'name': 'Common'}, 'variants': [
     57             {'name': 'Common', 'asset': {'kind': 'monster_data', 'id': 17655}, 'appearance': 229,
     58              'confidence': 'inferred_species', 'reason': 'Original source A.', 'alternatives': ['Other'],
     59              'groups': ['950/a'], 'placements': 9},
     60             {'name': 'Local', 'asset': {'kind': 'monster_data', 'id': 26088}, 'appearance': 277,
     61              'confidence': 'low', 'reason': 'Original source B.', 'alternatives': [26092, 'Resident'],
     62              'groups': ['950/b'], 'placements': 1}]}}
     63 
     64         # Expand each variant into its own group assignment.
     65         assigned = npc_assignments.group_assignments(catalog)
     66 
     67         # Keep local choices and their uncertainty independent.
     68         self.assertEqual(assigned['950/a']['fields']['name'], 'Common')
     69         self.assertEqual(assigned['950/b']['fields']['name'], 'Local')
     70         self.assertEqual(assigned['950/b']['fields']['appearance_status'], 'best_effort')
     71         self.assertEqual(assigned['950/b']['evidence']['reason'], 'Original source B.')
     72         self.assertEqual(assigned['950/b']['evidence']['alternatives'], [26092, 'Resident'])
     73 
     74     def test_zone_merge_changes_only_selected_fields_and_is_repeatable(self):
     75         # Include source ranges and positions that the merge must preserve.
     76         text = '''id: 950
     77 districts:
     78 - name: District
     79   groups:
     80   - template_code: TEST
     81     name: Placeholder TEST
     82     monster_data: 17655
     83     appearance: 229
     84     levels: [2, 4] # retain the complete range
     85     appearance_status: placeholder_creature
     86     placement_status: fixed_point
     87     comment: Old evidence.
     88     positions:
     89     - [1.234567, 2, 3] # edited position
     90     id: 950/a
     91     source_index: 3
     92   source_index: 0
     93 source:
     94   original: [1, 2, 3]
     95 '''
     96         assigned = {'950/a': {'code': 'TEST', 'fields': {'name': 'Rock', 'monster_data': 0,
     97                     'static_mesh': 55669, 'appearance': 0, 'appearance_status': 'static_mesh',
     98                     'comment': 'Original mesh evidence.'}}}
     99         original = ruamel.yaml.YAML(typ='safe').load(text)
    100 
    101         # Merge twice to check preservation and repeatability.
    102         merged = npc_assignments.merge_zone(text, assigned)
    103         repeated = npc_assignments.merge_zone(merged, assigned)
    104         result = ruamel.yaml.YAML(typ='safe').load(merged)
    105         expected = copy.deepcopy(original)
    106         expected['districts'][0]['groups'][0].update(assigned['950/a']['fields'])
    107 
    108         # Only the selected metadata fields may change.
    109         self.assertEqual(result, expected)
    110         self.assertEqual(repeated, merged)
    111         self.assertIn('    levels: [2, 4] # retain the complete range\n', merged)
    112         self.assertIn('    - [1.234567, 2, 3] # edited position\n', merged)
    113         self.assertTrue(merged.endswith('  source_index: 0\nsource:\n  original: [1, 2, 3]\n'))
    114 
    115     def test_complete_catalog_keeps_nonspawned_scopes_and_original_assets(self):
    116         # Read the complete retained catalog.
    117         catalog = npc_assignments.load_assignments()
    118 
    119         # Expand group choices and count all evidence scopes.
    120         assigned = npc_assignments.group_assignments(catalog)
    121         scopes = collections.Counter(entry['scope'] for entry in catalog.values())
    122         placements = sum(variant['placements'] for entry in catalog.values() for variant in entry['variants'])
    123 
    124         # Keep nonspawned references, static assets and alternatives intact.
    125         self.assertEqual(scopes, {'maintained_population': 1830, 'summoned_actor': 136, 'dependency_reference': 44})
    126         self.assertEqual(len(assigned), 11965)
    127         self.assertEqual(placements, 68609)
    128         self.assertEqual(sum(choice['fields'].get('static_mesh') == 55669 for choice in assigned.values()), 586)
    129         self.assertEqual(assigned['125/district/0/fixed/48']['fields']['monster_data'], 0)
    130         for entry in catalog.values():
    131             if entry['scope'] != 'maintained_population':
    132                 self.assertTrue(all(not variant['groups'] and not variant['placements'] for variant in entry['variants']))
    133             for variant in entry['variants']:
    134                 self.assertTrue(variant['reason'] and variant['alternatives'])
    135 
    136 
    137 if __name__ == '__main__':
    138     unittest.main()