home · contact · privacy
Fixed README typo.
[plomrogue] / src / map_object_actions.c
index f7667150b5e0104d18f9ce9504abeadd99c51b15..387b18a3b5d0d8b31323d34de78120fa4e7739cf 100644 (file)
 /* map_object_actions.c */
 
 #include "map_object_actions.h"
-#include <stdlib.h> /* for malloc(), calloc(), free() */
+#include <string.h> /* for strlen() */
 #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_objects.h" /* for MapObj, MapObjDef structs, get_player() */
+#include "misc.h" /* for 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 "command_db.h" /* for get_command_id() */
 
 
 
-extern void move_monster(struct World * world, struct Monster * monster)
+/* 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 World * world, struct MapObj * hitter,
+                             struct MapObj * hitted);
+
+
+
+static void actor_hits_actor(struct World * world, 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))
+    struct MapObjDef * mod_hitter = get_map_object_def(world, hitter->type);
+    struct MapObjDef * mod_hitted = get_map_object_def(world, hitted->type);
+    struct MapObj * player = get_player(world);
+    char * msg1 = "You";
+    char * msg2 = "wound";
+    char * msg3 = "you";
+    if      (player != hitter)
     {
-        sprintf(msg, "\nThe %s hits you.", desc);
-        update_log(world, msg);
-        world->player->hitpoints--;
-        if (0 == world->player->hitpoints)
+        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(world, msg);
+    hitted->lifepoints--;
+    if (0 == hitted->lifepoints)
+    {
+        hitted->type = mod_hitted->corpse_id;
+        if (player == hitted)
         {
-            update_log(world, "\nYou are dead.");
+            update_log(world, " You die.");
+        }
+        else
+        {
+            update_log(world, " It dies.");
+            if (player == hitter)
+            {
+                world->score = world->score + mod_hitted->lifepoints;
+            }
         }
-        return;
     }
-    struct Monster * other_monster;
-    for (other_monster = world->monster;
-         other_monster != 0;
-         other_monster = other_monster->map_obj.next)
+}
+
+
+
+extern uint8_t move_actor(struct World * world, struct MapObj * actor,
+                          enum dir d)
+{
+    struct yx_uint16 target = mv_yx_in_dir(d, actor->pos);
+    struct MapObj * other_actor;
+    for (other_actor = world->map_objs;
+         other_actor != 0;
+         other_actor = other_actor->next)
     {
-        if (other_monster == monster)
+        if (0 == other_actor->lifepoints || other_actor == actor)
         {
             continue;
         }
-        if (yx_uint16_cmp(t, other_monster->map_obj.pos))
+        if (yx_uint16_cmp(&target, &other_actor->pos))
         {
-            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;
+            actor_hits_actor(world, actor, other_actor);
+            return 2;
         }
     }
-    free(msg);
-    if (is_passable(world->map, t))
+    if (is_passable(world->map, target))
     {
-        monster->map_obj.pos = t;
+        actor->pos = target;
+        return 0;
     }
+    return 1;
 }
 
 
 
-extern void move_player (struct World * world, enum dir d)
+extern void move_player(struct World * world, enum dir d)
 {
-    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)
+    char * dsc_dir;
+    char * action_dsc_prototype = "player_";
+    uint8_t len_action_dsc_prototype = strlen(action_dsc_prototype);
+    char action_dsc[len_action_dsc_prototype + 2];
+    memcpy(action_dsc, action_dsc_prototype, len_action_dsc_prototype);
+    if      (NORTH == d)
+    {
+        dsc_dir = "north";
+        action_dsc[len_action_dsc_prototype] = 'u';
+    }
+    else if (EAST  == d)
+    {
+        dsc_dir = "east" ;
+        action_dsc[len_action_dsc_prototype] = 'r';
+    }
+    else if (SOUTH == d)
+    {
+        dsc_dir = "south";
+        action_dsc[len_action_dsc_prototype] = 'd';
+    }
+    else if (WEST  == d)
     {
-        if (yx_uint16_cmp(t, monster->map_obj.pos))
+        dsc_dir = "west" ;
+        action_dsc[len_action_dsc_prototype] = 'l';
+    }
+    action_dsc[len_action_dsc_prototype + 1] = '\0';
+    uint8_t res = move_actor(world, get_player(world), d);
+    if (1 >= res)
+    {
+        char * dsc_move = "You fail to move ";
+        if   (0 == res)
         {
-            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);
-            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);
+            dsc_move = "You move ";
+        }
+        char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
+        sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
+        update_log(world, msg);
+    }
+    turn_over(world, get_command_id(world, action_dsc));
 }
 
 
 
-extern void player_wait (struct World * world)
+extern void player_wait(struct World * world)
 {
     update_log(world, "\nYou wait.");
-    turn_over(world, 0);
+    turn_over(world, get_command_id(world, "wait"));
 }
 
 
 
-extern char is_passable (struct Map * map, struct yx_uint16 pos)
+extern char is_passable(struct Map * map, struct yx_uint16 pos)
 {
     char passable = 0;
     if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
@@ -147,4 +156,3 @@ extern char is_passable (struct Map * map, struct yx_uint16 pos)
     }
     return passable;
 }
-