home · contact · privacy
Enhance sanity of get_inventory_slot_to_consume.
[plomrogue] / roguelike-server
index 9bc55ad815bb6fe788720016b05c1e6c1979ef02..29fe11994b79c15bca1ac95536f788cb16345e21 100755 (executable)
@@ -360,22 +360,52 @@ def try_worldstate_update():
              mem[(mt[1] * length) + mt[2]] = ord(c)
         string = write_map(string, mem)
 
-        stacksmap = bytearray(b'0' * (length ** 2))  # #
+        metamapA = bytearray(b'0' * (length ** 2))  # #
         for id in [id for id in world_db["Things"]  # #
                       if not world_db["Things"][id]["carried"]  # #
+                      if world_db["Things"][id]["T_LIFEPOINTS"]  # #
                       if world_db["Things"][0]["fovmap"][  # #
                            world_db["Things"][id]["T_POSY"] * length  # #
                            + world_db["Things"][id]["T_POSX"]] == ord_v]:  # #
             pos = (world_db["Things"][id]["T_POSY"] * length  # #
                   + world_db["Things"][id]["T_POSX"])  # #
-            if stacksmap[pos] < ord('2'):  # #
-                stacksmap[pos] += 1  # #
+            if id == 0 or world_db["EMPATHY"]:  # #
+                type = world_db["Things"][id]["T_TYPE"]  # #
+                max_hp = world_db["ThingTypes"][type]["TT_LIFEPOINTS"]  # #
+                third_of_hp = max_hp / 3  # #
+                hp = world_db["Things"][id]["T_LIFEPOINTS"]  # #
+                add = 0  # #
+                if hp > 2 * third_of_hp:  # #
+                     add = 2  # #
+                elif hp > third_of_hp:  # #
+                    add = 1  # #
+                metamapA[pos] = ord('a') + add # #
+            else:  # #
+                metamapA[pos] = ord('X')  # #
         for mt in world_db["Things"][0]["T_MEMTHING"]:  # #
             pos = mt[1] * length + mt[2]  # #
-            if stacksmap[pos] < ord('2'):  # #
-                stacksmap[pos] += 1  # #
-        string = write_map(string, stacksmap)  # #
+            if metamapA[pos] < ord('2'):  # #
+                metamapA[pos] += 1  # #
+        string = write_map(string, metamapA)  # #
         
+        metamapB = bytearray(b' ' * (length ** 2))  # #
+        for id in [id for id in world_db["Things"]  # #
+                      if not world_db["Things"][id]["carried"]  # #
+                      if world_db["Things"][id]["T_LIFEPOINTS"]  # #
+                      if world_db["Things"][0]["fovmap"][  # #
+                           world_db["Things"][id]["T_POSY"] * length  # #
+                           + world_db["Things"][id]["T_POSX"]] == ord_v]:  # #
+            pos = (world_db["Things"][id]["T_POSY"] * length  # #
+                  + world_db["Things"][id]["T_POSX"])  # #
+            if id == 0 or world_db["EMPATHY"]:  # #
+                action = world_db["Things"][id]["T_COMMAND"]  # #
+                if 0 != action:  # #
+                    name = world_db["ThingActions"][action]["TA_NAME"]  # #
+                else:  # #
+                    name = " "  # #
+                metamapB[pos] = ord(name[0]) # #
+        string = write_map(string, metamapB)  # #
+
         atomic_write(io_db["path_worldstate"], string, delete=False)
         strong_write(io_db["file_out"], "WORLD_UPDATED\n")
         io_db["worldstate_updateable"] = False
@@ -1039,36 +1069,34 @@ def thingproliferation(t, prol_map):
 
 
 def try_healing(t):
-    """Grow t's HP to a 1/32 chance if < HP max, satiation > 0, and waiting.
-
-    On success, decrease satiation score by 32.
-    """
+    """If t's HP < max, increment them if well-nourished, maybe waiting."""
     # 7DRL: Successful heals increment God's mood.
-    if t["T_SATIATION"] > 0 \
-       and t["T_LIFEPOINTS"] < \
-           world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"] \
-       and 0 == (rand.next() % 31) \
-       and t["T_COMMAND"] == [id for id in world_db["ThingActions"]
-                              if world_db["ThingActions"][id]["TA_NAME"] ==
-                                 "wait"][0]:
-        t["T_LIFEPOINTS"] += 1
-        world_db["GOD_MOOD"] += 1  # #
-        t["T_SATIATION"] -= 32
-        if t == world_db["Things"][0]:
-            strong_write(io_db["file_out"], "LOG You heal.\n")
+    if t["T_LIFEPOINTS"] < \
+       world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]:
+        wait_id = [id for id in world_db["ThingActions"]
+                      if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
+        wait_divider = 8 if t["T_COMMAND"] == wait_id else 1
+        testval = int(abs(t["T_SATIATION"]) / wait_divider)
+        if (testval <= 1 or 1 == (rand.next() % testval)):
+            t["T_LIFEPOINTS"] += 1
+            if t != world_db["Things"][0]: # #
+                 world_db["GOD_MOOD"] += 1  # #
+            if t == world_db["Things"][0]:
+                strong_write(io_db["file_out"], "LOG You heal.\n")
 
 
 def hunger(t):
     """Decrement t's satiation,dependent on it trigger lifepoint dec chance."""
     if t["T_SATIATION"] > -32768:
-        t["T_SATIATION"] -= 1
-    testbase = t["T_SATIATION"] if t["T_SATIATION"] >= 0 else -t["T_SATIATION"]
-    if not world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]:
-        raise RuntimeError("A thing that should not hunger is hungering.")
-    stomach = int(32767 / world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"])
-    if int(int(testbase / stomach) / ((rand.next() % stomach) + 1)):
+        max_hp = world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
+        t["T_SATIATION"] -= int(math.sqrt(max_hp))
+    if 0 != t["T_SATIATION"] and 0 == int(rand.next() / abs(t["T_SATIATION"])):
         if t == world_db["Things"][0]:
-            strong_write(io_db["file_out"], "LOG You suffer from hunger.\n")
+            if t["T_SATIATION"] < 0:
+                strong_write(io_db["file_out"], "LOG You suffer from hunger.\n")
+            else:
+                strong_write(io_db["file_out"],
+                             "LOG You suffer from over-eating.\n")
         decrement_lifepoints(t)
 
 
@@ -1272,6 +1300,7 @@ def get_dir_to_target(t, filter):
 def standing_on_food(t):
     """Return True/False whether t is standing on a consumable."""
     for id in [id for id in world_db["Things"] if world_db["Things"][id] != t
+               if not world_db["Things"][id]["carried"]
                if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
                if world_db["Things"][id]["T_POSX"] == t["T_POSX"]
                if world_db["ThingTypes"][world_db["Things"][id]["T_TYPE"]]
@@ -1281,16 +1310,20 @@ def standing_on_food(t):
 
 
 def get_inventory_slot_to_consume(t):
-    """Return slot Id of strongest consumable in t's inventory, else -1."""
-    cmp_food = 0
+    """Return slot Id of healthiest consumable in t's inventory, else -1."""
+    cmp_food = -1
     selection = -1
     i = 0
     for id in t["T_CARRIES"]:
         type = world_db["Things"][id]["T_TYPE"]
         if world_db["ThingTypes"][type]["TT_TOOL"] == "food" \
-           and world_db["ThingTypes"][type]["TT_TOOLPOWER"] > cmp_food:
-            cmp_food = world_db["ThingTypes"][type]["TT_TOOLPOWER"]
-            selection = i
+           and world_db["ThingTypes"][type]["TT_TOOLPOWER"]:
+            nutvalue = world_db["ThingTypes"][type]["TT_TOOLPOWER"]
+            tmp_cmp = abs(t["T_SATIATION"] + nutvalue)
+            if (cmp_food < 0 and tmp_cmp < abs(t["T_SATIATION"])) \
+            or tmp_cmp < cmp_food:
+                cmp_food = tmp_cmp
+                selection = i
         i += 1
     return selection
 
@@ -1975,6 +2008,7 @@ commands_db = {
     "PLANT_1": (1, False, specialtypesetter("PLANT_1")),  # #
     "LUMBER": (1, False, specialtypesetter("LUMBER")),  # #
     "TOOL_0": (1, False, specialtypesetter("TOOL_0")),  # #
+    "EMPATHY": (1, False, setter(None, "EMPATHY", 0, 1)),  # #
     "TA_ID": (1, False, command_taid),
     "TA_EFFORT": (1, False, setter("ThingAction", "TA_EFFORT", 0, 255)),
     "TA_NAME": (1, False, command_taname),
@@ -2029,6 +2063,7 @@ world_db = {
     "PLANT_1": 0,  # #
     "LUMBER": 0,  # #
     "TOOL_0": 0,  # #
+    "EMPATHY": 1,  # #
     "ThingActions": {},
     "ThingTypes": {},
     "Things": {}