home · contact · privacy
Fixed bug that led to endless loop in nearest_enemy_dir().
[plomrogue] / src / map_object_actions.c
index f7667150b5e0104d18f9ce9504abeadd99c51b15..5ba99c07f1afcf80ff7532ecb65e9b24ebe32dbf 100644 (file)
 /* map_object_actions.c */
 
 #include "map_object_actions.h"
-#include <stdlib.h> /* for malloc(), calloc(), free() */
-#include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
-#include "misc.h" /* for rrand(), update_log(), turn_over()*/
-#include "map.h" /* for Map struct */
-#include "main.h" /* for World struct */
-#include "map_objects.h" /* for map object (definition) structs */
+#include <stdint.h> /* for uint8_t, uint16_t */
+#include <string.h> /* for strlen(), strcmp() */
+#include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(),
+                        * yx_uint16_cmp()
+                        */
+#include "map_objects.h" /* for MapObj, MapObjDef structs, get_player(),
+                          * set_object_position(), own_map_object()
+                          */
+#include "misc.h" /* for update_log(), try_malloc() */
+#include "map.h" /* for is_passable() */
+#include "main.h" /* for world global */
+#include "readwrite.h" /* for try_fopen(), try_fclose(), textfile_sizes() */
+#include "rexit.h" /* for exit_err() */
 
 
 
-extern void move_monster(struct World * world, struct Monster * monster)
+/* If "name" fits "moa"->name, set "moa"->func to "func". */
+static uint8_t try_func_name(struct MapObjAct * moa,
+                             char * name, void (* func) (struct MapObj *));
+
+/* One actor "wounds" another actor, decrementing his lifepoints and, if they
+ * reach zero in the process, killing it. Generates appropriate log message.
+ */
+static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
+
+/* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
+ * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
+ */
+static void playerbonus_wait();
+static void playerbonus_move(char d, uint8_t passable);
+static void playerbonus_drop(uint8_t owns_none);
+static void playerbonus_pick(uint8_t picked);
+static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
+
+
+
+static uint8_t try_func_name(struct MapObjAct * moa,
+                             char * name, void (* func) (struct MapObj *))
+{
+    if (0 == strcmp(moa->name, name))
+    {
+        moa->func = func;
+        return 1;
+    }
+    return 0;
+}
+
+
+
+static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
 {
-    char d = rrand(0, 0) % 5;
-    struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
-    char * msg = malloc(100);
-    struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
-    char * desc = mod->desc;
-    char * desc_other;
-    if (yx_uint16_cmp(t, world->player->pos))
-    {
-        sprintf(msg, "\nThe %s hits you.", desc);
-        update_log(world, msg);
-        world->player->hitpoints--;
-        if (0 == world->player->hitpoints)
+    struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
+    struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
+    struct MapObj * player = get_player();
+    char * msg1 = "You";
+    char * msg2 = "wound";
+    char * msg3 = "you";
+    if      (player != hitter)
+    {
+        msg1 = mod_hitter->name;
+        msg2 = "wounds";
+    }
+    if (player != hitted)
+    {
+        msg3 = mod_hitted->name;
+    }
+    uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
+    char msg[len];
+    sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
+    update_log(msg);
+    hitted->lifepoints--;
+    if (0 == hitted->lifepoints)
+    {
+        hitted->type = mod_hitted->corpse_id;
+        if (player == hitted)
+        {
+            update_log(" You die.");
+            return;
+        }
+        update_log(" It dies.");
+        if (player == hitter)
         {
-            update_log(world, "\nYou are dead.");
+            world.score = world.score + mod_hitted->lifepoints;
         }
+    }
+}
+
+
+
+static void playerbonus_wait()
+{
+        update_log("\nYou wait.");
+}
+
+
+
+static void playerbonus_move(char d, uint8_t passable)
+{
+    char * dsc_dir = "north";
+    if      ('E' == d)
+    {
+        dsc_dir = "east" ;
+    }
+    else if ('S' == d)
+    {
+        dsc_dir = "south";
+    }
+    else if ('W' == d)
+    {
+        dsc_dir = "west" ;
+    }
+    char * dsc_move = "You move ";
+    if (0 == passable)
+    {
+        dsc_move = "You fail to move ";
+    }
+    char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
+    sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
+    update_log(msg);
+}
+
+
+
+static void playerbonus_drop(uint8_t owns_none)
+{
+    if (0 != owns_none)
+    {
+        update_log("\nYou try to drop an object, but you own none.");
+        return;
+    }
+    update_log("\nYou drop an object.");
+    world.inventory_sel = world.inventory_sel - (0 < world.inventory_sel);
+}
+
+
+
+static void playerbonus_pick(uint8_t picked)
+{
+    if (picked)
+    {
+        update_log("\nYou pick up an object.");
+        return;
+    }
+    update_log("\nYou try to pick up an object, but there is none.");
+}
+
+
+
+static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
+{
+    if      (no_object)
+    {
+        update_log("\nYou try to use an object, but you own none.");
         return;
     }
-    struct Monster * other_monster;
-    for (other_monster = world->monster;
-         other_monster != 0;
-         other_monster = other_monster->map_obj.next)
+    else if (wrong_object)
     {
-        if (other_monster == monster)
+        update_log("\nYou try to use this object, but fail.");
+        return;
+    }
+    update_log("\nYou consume MAGIC MEAT.");
+    world.inventory_sel = world.inventory_sel - (0 < world.inventory_sel);
+}
+
+
+
+extern void init_map_object_actions()
+{
+    char * f_name = "init_map_object_actions()";
+    char * path = "config/map_object_actions";
+    FILE * file = try_fopen(path, "r", f_name);
+    uint16_t linemax = textfile_sizes(file, NULL);
+    char line[linemax + 1];
+    struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
+    char * delim = " ";
+    while (fgets(line, linemax + 1, file))
+    {
+        if ('\n' == line[0] || 0 == line[0])
         {
-            continue;
+            break;
         }
-        if (yx_uint16_cmp(t, other_monster->map_obj.pos))
+        struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name);
+        moa->id = atoi(strtok(line, delim));
+        moa->effort = atoi(strtok(NULL, delim));
+        char * funcname = strtok(NULL, "\n");
+        uint8_t len_name = strlen(funcname) + 1;
+        moa->name = try_malloc(len_name, f_name);
+        memcpy(moa->name, funcname, len_name);
+        if (!(   try_func_name(moa, "move", actor_move)
+              || try_func_name(moa, "pick_up", actor_pick)
+              || try_func_name(moa, "drop", actor_drop)
+              || try_func_name(moa, "use", actor_use)))
         {
-            mod = get_map_obj_def(world, monster->map_obj.type);
-            desc_other = mod->desc;
-            sprintf(msg, "\n%s bumps into %s.", desc, desc_other);
-            update_log(world, msg);
-            return;
+            moa->func = actor_wait;
         }
+        moa->next = NULL;
+        * moa_ptr_ptr = moa;
+        moa_ptr_ptr = &moa->next;
     }
-    free(msg);
-    if (is_passable(world->map, t))
+    try_fclose(file, f_name);
+}
+
+
+
+extern void free_map_object_actions(struct MapObjAct * moa)
+{
+    if (NULL == moa)
     {
-        monster->map_obj.pos = t;
+        return;
     }
+    free(moa->name);
+    free_map_object_actions(moa->next);
+    free(moa);
 }
 
 
 
-extern void move_player (struct World * world, enum dir d)
+extern uint8_t get_moa_id_by_name(char * name)
 {
-    struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
-    struct Monster * monster;
-    struct MapObjDef * mod;
-    char * msg = calloc(100, sizeof(char));
-    char * desc;
-    for (monster = world->monster;
-         monster != 0;
-         monster = monster->map_obj.next)
-    {
-        if (yx_uint16_cmp(t, monster->map_obj.pos))
+    struct MapObjAct * moa = world.map_obj_acts;
+    while (NULL != moa)
+    {
+        if (0 == strcmp(moa->name, name))
         {
-            mod = get_map_obj_def(world, monster->map_obj.type);
-            desc = mod->desc;
-            sprintf(msg, "\nYou hit the %s.", desc);
-            update_log(world, msg);
-            monster->hitpoints--;
-            if (0 == monster->hitpoints)
-            {
-                sprintf(msg, "\nYou kill the %s.", desc);
-                update_log(world, msg);
-                if (world->monster == monster)
-                {
-                    world->monster = world->monster->map_obj.next;
-                }
-                else
-                {
-                    struct Monster * m_prev;
-                    for (m_prev = world->monster;
-                         m_prev->map_obj.next != monster;
-                         m_prev = m_prev->map_obj.next);
-                    m_prev->map_obj.next = monster->map_obj.next;
-                }
-                free(monster);
-            }
-            turn_over(world, d);
+            break;
+        }
+        moa = moa->next;
+    }
+    exit_err(NULL == moa, "get_moa_id_name() did not find map object action.");
+    return moa->id;
+}
+
+
+
+extern void actor_wait(struct MapObj * mo)
+{
+    if (mo == get_player())
+    {
+        playerbonus_wait();
+    }
+}
+
+
+
+extern void actor_move(struct MapObj * mo)
+{
+    char d = mo->arg;
+    struct yx_uint16 target = mv_yx_in_dir(d, mo->pos);
+    struct MapObj * other_mo;
+    for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
+    {
+        if (0 == other_mo->lifepoints || other_mo == mo)
+        {
+            continue;
+        }
+        if (yx_uint16_cmp(&target, &other_mo->pos))
+        {
+            actor_hits_actor(mo, other_mo);
             return;
-          }
-      }
-      char * msg_content = "You fail to move";
-      char * dir;
-      if      (NORTH == d)
-      {
-          dir = "north";
-      }
-      else if (EAST  == d)
-      {
-          dir = "east" ;
-      }
-      else if (SOUTH == d)
-      {
-          dir = "south";
-      }
-      else if (WEST  == d)
-      {
-          dir = "west" ;
-      }
-      if (is_passable(world->map, t))
-      {
-          msg_content = "You move";
-          world->player->pos = t;
-      }
-      sprintf(msg, "\n%s %s.", msg_content, dir);
-      update_log(world, msg);
-      free(msg);
-      turn_over(world, d);
+        }
+    }
+    uint8_t passable = is_passable(world.map, target);
+    if (passable)
+    {
+        set_object_position(mo, target);
+    }
+    if (mo == get_player())
+    {
+        playerbonus_move(d, passable);
+    }
 }
 
 
 
-extern void player_wait (struct World * world)
+extern void actor_drop(struct MapObj * mo)
 {
-    update_log(world, "\nYou wait.");
-    turn_over(world, 0);
+    uint8_t owns_none = (NULL == mo->owns);
+    if (!owns_none)
+    {
+        uint8_t select = mo->arg;
+        struct MapObj * owned = mo->owns;
+        uint8_t i = 0;
+        for (; i != select; i++, owned = owned->next);
+        own_map_object(&world.map_objs, &mo->owns, owned->id);
+    }
+    if (mo == get_player())
+    {
+        playerbonus_drop(owns_none);
+    }
 }
 
 
 
-extern char is_passable (struct Map * map, struct yx_uint16 pos)
+extern void actor_pick(struct MapObj * mo)
 {
-    char passable = 0;
-    if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
+    struct MapObj * picked;
+    for (picked = world.map_objs; NULL != picked; picked = picked->next)
     {
-        if ('.' == map->cells[pos.y * map->size.x + pos.x])
+        if (picked != mo && yx_uint16_cmp(&picked->pos, &mo->pos))
         {
-            passable = 1;
+            break;
         }
     }
-    return passable;
+    if (NULL != picked)
+    {
+        own_map_object(&mo->owns, &world.map_objs, picked->id);
+        set_object_position(picked, mo->pos);
+    }
+    if (mo == get_player())
+    {
+        playerbonus_pick(NULL != picked);
+    }
 }
 
+
+
+extern void actor_use(struct MapObj * mo)
+{
+    uint8_t wrong_object = 1;
+    uint8_t no_object = (NULL == mo->owns);
+    if (!no_object)
+    {
+        uint8_t select = mo->arg;
+        uint8_t i = 0;
+        struct MapObj * selected = mo->owns;
+        for (; i != select; i++, selected = selected->next);
+        struct MapObjDef * mod = get_map_object_def(selected->type);
+        if (!strcmp("MAGIC MEAT", mod->name))
+        {
+            wrong_object = 0;
+            struct MapObj * next = selected->next;
+            free(selected);
+            if (0 < select)
+            {
+                select--;
+                selected = mo->owns;
+                for (i = 0; i != select; i++, selected = selected->next);
+                selected->next = next;
+            }
+            else
+            {
+                mo->owns = next;
+            }
+            mo->lifepoints++;
+        }
+    }
+    if (mo == get_player())
+    {
+        playerbonus_use(no_object, wrong_object);
+    }
+}