home · contact · privacy
Server, plugin: Refactor ai (plugin hooks).
authorChristian Heller <c.heller@plomlompom.de>
Sat, 27 Feb 2016 11:03:31 +0000 (12:03 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 27 Feb 2016 11:03:31 +0000 (12:03 +0100)
plugins/server/PleaseTheIslandGod.py
server/ai.py
server/commands.py
server/config/actions.py
server/world.py

index e4ca0b3ad1054073cd2ea4f5128a132f6836dfa4..4b8a7d73db1c4ba268cbe80ff1decec3ac8aedd8 100644 (file)
@@ -65,35 +65,9 @@ def thingprol_plugin_post_create_hook(t):
         log("The Island God SMILES upon a new-born bear baby.")
         world_db["GOD_FAVOR"] += 750
 
-def ai(t):
-    from server.ai import get_dir_to_target, get_inventory_slot_to_consume, \
-        standing_on_food
-    t["T_COMMAND"] = [id for id in world_db["ThingActions"]
-                      if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
-    eating = len([id for id in world_db["ThingActions"]
-                  if world_db["ThingActions"][id]["TA_NAME"] == "use"]) > 0
-    picking = len([id for id in world_db["ThingActions"]
-                   if world_db["ThingActions"][id]["TA_NAME"] == "pickup"]) > 0
-    if eating and picking:
-        if get_dir_to_target(t, "f"):
-            return
-        sel = get_inventory_slot_to_consume(t)
-        if -1 != sel:
-            t["T_COMMAND"] = [id for id in world_db["ThingActions"]
-                              if world_db["ThingActions"][id]["TA_NAME"]
-                                 == "use"][0]
-            t["T_ARGUMENT"] = sel
-        elif standing_on_food(t) and (len(t["T_CARRIES"]) <
-                world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]):
-                t["T_COMMAND"] = [id for id in world_db["ThingActions"]
-                                  if world_db["ThingActions"][id]["TA_NAME"]
-                                  == "pickup"][0]
-        else:
-            going_to_known_food_spot = get_dir_to_target(t, "c")
-            if not going_to_known_food_spot:
-                aiming_for_walking_food = get_dir_to_target(t, "a")
-                if not aiming_for_walking_food:
-                    get_dir_to_target(t, "s")
+def ai_hook_pickup_test(t):
+    return len(t["T_CARRIES"]) < \
+        world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]
 
 def actor_pickup(t):
     from server.ai import eat_vs_hunger_threshold
@@ -703,7 +677,6 @@ server.config.actions.action_db["actor_move"] = actor_move
 server.config.actions.action_db["actor_pickup"] = actor_pickup
 server.config.actions.action_db["actor_drop"] = actor_drop
 server.config.actions.action_db["actor_use"] = actor_use
-server.config.actions.ai_func = ai
 
 from server.config.commands import commands_db
 commands_db["TT_ID"] = (1, False, command_ttid)
@@ -741,3 +714,6 @@ server.config.thingproliferation.thingprol_plugin_conditions = \
     thingprol_plugin_conditions
 server.config.thingproliferation.thingprol_plugin_post_create_hook = \
     thingprol_plugin_post_create_hook
+
+import server.config.ai
+server.config.ai.ai_hook_pickup = ai_hook_pickup_test
index a243aa4dfc9e49d20c096162089185576fd21e7f..0d1effce2262a6f0473d270e4a37ad3128cc19de 100644 (file)
@@ -285,12 +285,13 @@ def ai(t):
         if get_dir_to_target(t, "f"):
             return
         sel = get_inventory_slot_to_consume(t)
+        from server.config.ai import ai_hook_pickup_test
         if -1 != sel:
             t["T_COMMAND"] = [id for id in world_db["ThingActions"]
                               if world_db["ThingActions"][id]["TA_NAME"]
                                  == "use"][0]
             t["T_ARGUMENT"] = sel
-        elif standing_on_food(t):
+        elif standing_on_food(t) and ai_hook_pickup_test(t):
                 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
                                   if world_db["ThingActions"][id]["TA_NAME"]
                                   == "pickup"][0]
index 72018be4006a93a58a94d8e69144fed3004d9466..fc384528704cf88e8cfec3b975a857d1433594f2 100644 (file)
@@ -513,6 +513,6 @@ def play_move(str_arg):
 
 def command_ai():
     """Call ai() on player Thing, then turn_over()."""
-    from server.config.actions import ai_func
-    ai_func(world_db["Things"][0])
+    from server.ai import ai
+    ai(world_db["Things"][0])
     turn_over()
index 66be0e20d9370e17208131852a6b527bd32a4df5..7f3af9a1116b3364552d78a429b002eb147a3632 100644 (file)
@@ -12,6 +12,3 @@ action_db = {
     "actor_drop": actor_drop,
     "actor_use": actor_use
 }
-
-from server.ai import ai
-ai_func = ai
index b9f9114630ef51233883d5c8fa5d67e729b1e0ef..c00b5f14374bfb1fb2ca45c2b3af2e5c76255591 100644 (file)
@@ -51,7 +51,8 @@ def set_world_inactive():
 
 def turn_over():
     """Run game world and its inhabitants until new player input expected."""
-    from server.config.actions import action_db, ai_func
+    from server.ai import ai
+    from server.config.actions import action_db
     from server.config.misc import calc_effort_func
     from server.update_map_memory import update_map_memory
     from server.thingproliferation import thingproliferation
@@ -73,7 +74,7 @@ def turn_over():
                     update_map_memory(Thing)
                     if 0 == id:
                         return
-                    ai_func(Thing)
+                    ai(Thing)
                 try_healing(Thing)
                 hunger(Thing)
                 if Thing["T_LIFEPOINTS"]: