home · contact · privacy
New animate map objects are never placed on a square with other animated map objects...
[plomrogue] / src / map_object_actions.c
1 /* map_object_actions.c */
2
3 #include "map_object_actions.h"
4 #include <string.h> /* for strlen() */
5 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
6 #include "map_objects.h" /* for MapObj, MapObjDef structs, get_player() */
7 #include "misc.h" /* for update_log(), turn_over() */
8 #include "map.h" /* for Map struct */
9 #include "main.h" /* for World struct */
10 #include "command_db.h" /* for get_command_id() */
11
12
13
14 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
15  * reach zero in the process, killing it. Generates appropriate log message.
16  */
17 static void actor_hits_actor(struct World * world, struct MapObj * hitter,
18                              struct MapObj * hitted);
19
20
21
22 static void actor_hits_actor(struct World * world, struct MapObj * hitter,
23                              struct MapObj * hitted)
24 {
25     struct MapObjDef * mod_hitter = get_map_object_def(world, hitter->type);
26     struct MapObjDef * mod_hitted = get_map_object_def(world, hitted->type);
27     struct MapObj * player = get_player(world);
28     char * msg1 = "You";
29     char * msg2 = "wound";
30     char * msg3 = "you";
31     if      (player != hitter)
32     {
33         msg1 = mod_hitter->name;
34         msg2 = "wounds";
35     }
36     if (player != hitted)
37     {
38         msg3 = mod_hitted->name;
39     }
40     uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
41     char msg[len];
42     sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
43     update_log(world, msg);
44     hitted->lifepoints--;
45     if (0 == hitted->lifepoints)
46     {
47         hitted->type = mod_hitted->corpse_id;
48         if (player == hitted)
49         {
50             update_log(world, " You die.");
51         }
52         else
53         {
54             update_log(world, " It dies.");
55             if (player == hitter)
56             {
57                 world->score = world->score + mod_hitted->lifepoints;
58             }
59         }
60     }
61 }
62
63
64
65 extern uint8_t move_actor(struct World * world, struct MapObj * actor,
66                           enum dir d)
67 {
68     struct yx_uint16 target = mv_yx_in_dir(d, actor->pos);
69     struct MapObj * other_actor;
70     for (other_actor = world->map_objs;
71          other_actor != 0;
72          other_actor = other_actor->next)
73     {
74         if (0 == other_actor->lifepoints || other_actor == actor)
75         {
76             continue;
77         }
78         if (yx_uint16_cmp(&target, &other_actor->pos))
79         {
80             actor_hits_actor(world, actor, other_actor);
81             return 2;
82         }
83     }
84     if (is_passable(world->map, target))
85     {
86         actor->pos = target;
87         return 0;
88     }
89     return 1;
90 }
91
92
93
94 extern void move_player(struct World * world, enum dir d)
95 {
96     char * dsc_dir;
97     char * action_dsc_prototype = "player_";
98     uint8_t len_action_dsc_prototype = strlen(action_dsc_prototype);
99     char action_dsc[len_action_dsc_prototype + 2];
100     memcpy(action_dsc, action_dsc_prototype, len_action_dsc_prototype);
101     if      (NORTH == d)
102     {
103         dsc_dir = "north";
104         action_dsc[len_action_dsc_prototype] = 'u';
105     }
106     else if (EAST  == d)
107     {
108         dsc_dir = "east" ;
109         action_dsc[len_action_dsc_prototype] = 'r';
110     }
111     else if (SOUTH == d)
112     {
113         dsc_dir = "south";
114         action_dsc[len_action_dsc_prototype] = 'd';
115     }
116     else if (WEST  == d)
117     {
118         dsc_dir = "west" ;
119         action_dsc[len_action_dsc_prototype] = 'l';
120     }
121     action_dsc[len_action_dsc_prototype + 1] = '\0';
122     uint8_t res = move_actor(world, get_player(world), d);
123     if (1 >= res)
124     {
125         char * dsc_move = "You fail to move ";
126         if   (0 == res)
127         {
128             dsc_move = "You move ";
129         }
130         char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
131         sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
132         update_log(world, msg);
133     }
134     turn_over(world, get_command_id(world, action_dsc));
135 }
136
137
138
139 extern void player_wait(struct World * world)
140 {
141     update_log(world, "\nYou wait.");
142     turn_over(world, get_command_id(world, "wait"));
143 }
144
145
146
147 extern char is_passable(struct Map * map, struct yx_uint16 pos)
148 {
149     char passable = 0;
150     if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
151     {
152         if ('.' == map->cells[pos.y * map->size.x + pos.x])
153         {
154             passable = 1;
155         }
156     }
157     return passable;
158 }