From 81f8bee1a21642c56b1fead9ba79afdad0c8b451 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 27 Feb 2016 12:03:31 +0100
Subject: [PATCH] Server, plugin: Refactor ai (plugin hooks).

---
 plugins/server/PleaseTheIslandGod.py | 36 +++++-----------------------
 server/ai.py                         |  3 ++-
 server/commands.py                   |  4 ++--
 server/config/actions.py             |  3 ---
 server/world.py                      |  5 ++--
 5 files changed, 13 insertions(+), 38 deletions(-)

diff --git a/plugins/server/PleaseTheIslandGod.py b/plugins/server/PleaseTheIslandGod.py
index e4ca0b3..4b8a7d7 100644
--- a/plugins/server/PleaseTheIslandGod.py
+++ b/plugins/server/PleaseTheIslandGod.py
@@ -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
diff --git a/server/ai.py b/server/ai.py
index a243aa4..0d1effc 100644
--- a/server/ai.py
+++ b/server/ai.py
@@ -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]
diff --git a/server/commands.py b/server/commands.py
index 72018be..fc38452 100644
--- a/server/commands.py
+++ b/server/commands.py
@@ -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()
diff --git a/server/config/actions.py b/server/config/actions.py
index 66be0e2..7f3af9a 100644
--- a/server/config/actions.py
+++ b/server/config/actions.py
@@ -12,6 +12,3 @@ action_db = {
     "actor_drop": actor_drop,
     "actor_use": actor_use
 }
-
-from server.ai import ai
-ai_func = ai
diff --git a/server/world.py b/server/world.py
index b9f9114..c00b5f1 100644
--- a/server/world.py
+++ b/server/world.py
@@ -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"]:
-- 
2.30.2