overthesun / simoc

A scalable model of an interactive, off-world community
https://ngs.simoc.space/
GNU General Public License v3.0
2 stars 1 forks source link

Fix custom plant agents support #470

Open ezio-melotti opened 6 months ago

ezio-melotti commented 6 months ago

It should be possible to define custom agents (including plants), by adding their definition to the game_config, at the top level. The convert_configuration() function will extract certain items from the game_config, and assume that all the remaining top-level items are agents, copying them in full_game_config['agents']:

https://github.com/overthesun/simoc/blob/098696a40389eb72284c9e4c164fe5a39478d33c/simoc_server/front_end_routes.py#L450-L451

Plants however are handled differently:

https://github.com/overthesun/simoc/blob/098696a40389eb72284c9e4c164fe5a39478d33c/simoc_server/front_end_routes.py#L293-L306

Lines 296-297 extract the species and amount, and line 301 copies the plant agent to the top level. With this approach:

While testing, we were able to work around the issue by implementing the following changes to preserve the additional information:

@@ -294,11 +294,11 @@ def convert_configuration(game_config, agent_desc=None, save_output=False):
         plants = working_config.pop('plants')
         for plant in plants:
             amount = plant.get('amount', 0) or 0
-            plant_type = plant.get('species', None)
+            plant_type = plant.pop('species')
             if not (plant_type and amount):
                 continue
             plants_in_config.append(plant_type)
-            working_config[plant_type] = dict(amount=amount)
+            working_config[plant_type] = plant
             if is_b2:
                 working_config[plant_type]['properties'] = {
                     'crop_management_factor': {'value': crop_mgmt_factor},

(the is_b2 case also needs to be handled, since it overrides the properties)

Later in the code flows were getting overridden too, so we tried to update the lamp by hardcoding a workaround, but it doesn't seem to work (and it should be fixed properly anyway):

@@ -320,7 +320,10 @@ def convert_configuration(game_config, agent_desc=None, save_output=False):
                                            'flows': {'out': {'par': {'connections': [lamp_id]}}}}
                 # Add connection to plant 'par' flow
                 par_flow_stub = {'in': {'par': {'connections': [lamp_id]}}}
-                working_config[species]['flows'] = par_flow_stub
+                if 'flows' in working_config[species]:
+                    working_config[species]['flows']['in']['par']['connections'].append(lamp_id)
+                else:
+                    working_config[species]['flows'] = par_flow_stub

Unless we are missing something, custom plant agents don't seem to currently be supported. If this is true, a proper fix that adds custom plants support by doing something similar to the workarounds pasted above should be implemented. @granawkins, can you confirm that this is necessary and that the approach we followed (passing the custom plant in game_config['plants']) makes sense?