rpgame_semantics.alc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. String function getHeroTile(model : Element, hero : String):
  27. return set_pop(followAssociation(model, hero, "Character_on_tile"))!
  28. String function getGoalTile(model : Element, goal : String):
  29. return set_pop(followAssociation(model, goal, "Item_on_tile"))!
  30. Void function setHeroTile(model : Element, hero : String, tile : String):
  31. Element assocs
  32. assocs = allOutgoingAssociationInstances(model, hero, "Character_on_tile")
  33. while (0 < list_len(assocs)):
  34. model_delete_element(model, set_pop(assocs))
  35. instantiate_link(model, "Character_on_tile", "", hero, tile)
  36. return!
  37. Element function getConnectedTiles(model : Element, tile : String):
  38. Element left
  39. Element right
  40. Element top
  41. Element bottom
  42. Element result
  43. result = create_node()
  44. left = followAssociation(model, tile, "tile_left")
  45. right = followAssociation(model, tile, "tile_right")
  46. top = followAssociation(model, tile, "tile_top")
  47. bottom = followAssociation(model, tile, "tile_bottom")
  48. while (dict_len(left) > 0):
  49. set_add(result, set_pop(left))
  50. while (dict_len(right) > 0):
  51. set_add(result, set_pop(right))
  52. while (dict_len(top) > 0):
  53. set_add(result, set_pop(top))
  54. while (dict_len(bottom) > 0):
  55. set_add(result, set_pop(bottom))
  56. return result!
  57. Void function execute_rpgame(model : Element):
  58. String hero
  59. String goal
  60. String hero_tile
  61. String goal_tile
  62. log("Executing model!")
  63. // Make the assumption that only a single hero and goal exist
  64. hero = set_pop(allInstances(model, "Hero"))
  65. goal = set_pop(allInstances(model, "Goal"))
  66. log("Got all tiles")
  67. goal_tile = getGoalTile(model, goal)
  68. log("Got goal tile: " + goal_tile)
  69. hero_tile = getHeroTile(model, hero)
  70. log("Got hero tile: " + hero_tile)
  71. log("Keep executing")
  72. while (hero_tile != goal_tile):
  73. output((("Hero: " + hero_tile) + " -- Goal: ") + goal_tile)
  74. hero_tile = random_choice(getConnectedTiles(model, hero_tile))
  75. setHeroTile(model, hero, hero_tile)
  76. output("Hero reached goal!")
  77. return!