rpgame_semantics.alc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "library.alh"
  5. include "conformance_scd.alh"
  6. include "random.alh"
  7. Void function main():
  8. Element model
  9. String verify_result
  10. while (True):
  11. output("Which model do you want to execute with RPGame semantics?")
  12. model = import_node(input())
  13. if (element_eq(model, read_root())):
  14. output("Could not find model; aborting")
  15. elif (element_neq(model["metamodel"], import_node("models/RPGame"))):
  16. log("Got metamodel: " + cast_e2s(model["metamodel"]))
  17. log("Expected metamodel: " + cast_e2s(import_node("models/RPGame")))
  18. output("Not a RPGame model; aborting")
  19. else:
  20. verify_result = conformance_scd(model)
  21. if (verify_result == "OK"):
  22. output("Model OK!")
  23. execute_rpgame(model)
  24. else:
  25. output("Non-conforming model: " + verify_result)
  26. return!
  27. String function getHeroTile(model : Element, hero : String):
  28. return set_pop(followAssociation(model, hero, "Character_on_tile"))!
  29. String function getGoalTile(model : Element, goal : String):
  30. return set_pop(followAssociation(model, goal, "Item_on_tile"))!
  31. Void function setHeroTile(model : Element, hero : String, tile : String):
  32. Element assocs
  33. assocs = allOutgoingAssociationInstances(model, hero, "Character_on_tile")
  34. while (0 < list_len(assocs)):
  35. model_delete_element(model, set_pop(assocs))
  36. instantiate_link(model, "Character_on_tile", "", hero, tile)
  37. return!
  38. Element function getConnectedTiles(model : Element, tile : String):
  39. Element left
  40. Element right
  41. Element top
  42. Element bottom
  43. Element result
  44. result = create_node()
  45. left = followAssociation(model, tile, "tile_left")
  46. right = followAssociation(model, tile, "tile_right")
  47. top = followAssociation(model, tile, "tile_top")
  48. bottom = followAssociation(model, tile, "tile_bottom")
  49. while (dict_len(left) > 0):
  50. set_add(result, set_pop(left))
  51. while (dict_len(right) > 0):
  52. set_add(result, set_pop(right))
  53. while (dict_len(top) > 0):
  54. set_add(result, set_pop(top))
  55. while (dict_len(bottom) > 0):
  56. set_add(result, set_pop(bottom))
  57. return result!
  58. Void function execute_rpgame(model : Element):
  59. String hero
  60. String goal
  61. String hero_tile
  62. String goal_tile
  63. log("Executing model!")
  64. // Make the assumption that only a single hero and goal exist
  65. hero = set_pop(allInstances(model, "Hero"))
  66. goal = set_pop(allInstances(model, "Goal"))
  67. log("Got all tiles")
  68. goal_tile = getGoalTile(model, goal)
  69. log("Got goal tile: " + goal_tile)
  70. hero_tile = getHeroTile(model, hero)
  71. log("Got hero tile: " + hero_tile)
  72. log("Keep executing")
  73. while (hero_tile != goal_tile):
  74. output((("Hero: " + hero_tile) + " -- Goal: ") + goal_tile)
  75. hero_tile = random_choice(getConnectedTiles(model, hero_tile))
  76. setHeroTile(model, hero, hero_tile)
  77. output("Hero reached goal!")
  78. return!