home · contact · privacy
Removed if-redundancy in get_drawfunc_by_char().
[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(),
6                         * yx_uint16_cmp()
7                         */
8 #include "map_objects.h" /* for MapObj, MapObjDef structs, get_player(),
9                           * set_object_position(), own_map_object()
10                           */
11 #include "misc.h" /* for update_log(), turn_over() */
12 #include "map.h" /* for Map struct */
13 #include "main.h" /* for world global */
14 #include "command_db.h" /* for get_command_id() */
15
16
17
18 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
19  * reach zero in the process, killing it. Generates appropriate log message.
20  */
21 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
22
23
24
25 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
26 {
27     struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
28     struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
29     struct MapObj * player = get_player();
30     char * msg1 = "You";
31     char * msg2 = "wound";
32     char * msg3 = "you";
33     if      (player != hitter)
34     {
35         msg1 = mod_hitter->name;
36         msg2 = "wounds";
37     }
38     if (player != hitted)
39     {
40         msg3 = mod_hitted->name;
41     }
42     uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
43     char msg[len];
44     sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
45     update_log(msg);
46     hitted->lifepoints--;
47     if (0 == hitted->lifepoints)
48     {
49         hitted->type = mod_hitted->corpse_id;
50         if (player == hitted)
51         {
52             update_log(" You die.");
53         }
54         else
55         {
56             update_log(" It dies.");
57             if (player == hitter)
58             {
59                 world.score = world.score + mod_hitted->lifepoints;
60             }
61         }
62     }
63 }
64
65
66
67 extern uint8_t move_actor(struct MapObj * actor, char d)
68 {
69     struct yx_uint16 target = mv_yx_in_dir(d, actor->pos);
70     struct MapObj * other_actor;
71     for (other_actor = world.map_objs;
72          other_actor != 0;
73          other_actor = other_actor->next)
74     {
75         if (0 == other_actor->lifepoints || other_actor == actor)
76         {
77             continue;
78         }
79         if (yx_uint16_cmp(&target, &other_actor->pos))
80         {
81             actor_hits_actor(actor, other_actor);
82             return 2;
83         }
84     }
85     if (is_passable(world.map, target))
86     {
87         set_object_position(actor, target);
88         return 0;
89     }
90     return 1;
91 }
92
93
94
95 extern void move_player(char d)
96 {
97     char * dsc_dir;
98     char * action_dsc_prototype = "player_";
99     uint8_t len_action_dsc_prototype = strlen(action_dsc_prototype);
100     char action_dsc[len_action_dsc_prototype + 2];
101     memcpy(action_dsc, action_dsc_prototype, len_action_dsc_prototype);
102     if      ('N' == d)
103     {
104         dsc_dir = "north";
105         action_dsc[len_action_dsc_prototype] = 'u';
106     }
107     else if ('E' == d)
108     {
109         dsc_dir = "east" ;
110         action_dsc[len_action_dsc_prototype] = 'r';
111     }
112     else if ('S' == d)
113     {
114         dsc_dir = "south";
115         action_dsc[len_action_dsc_prototype] = 'd';
116     }
117     else if ('W' == d)
118     {
119         dsc_dir = "west" ;
120         action_dsc[len_action_dsc_prototype] = 'l';
121     }
122     action_dsc[len_action_dsc_prototype + 1] = '\0';
123     uint8_t res = move_actor(get_player(), d);
124     if (1 >= res)
125     {
126         char * dsc_move = "You fail to move ";
127         if   (0 == res)
128         {
129             dsc_move = "You move ";
130         }
131         char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
132         sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
133         update_log(msg);
134     }
135     turn_over(get_command_id(action_dsc));
136 }
137
138
139
140 extern void player_wait()
141 {
142     update_log("\nYou wait.");
143     turn_over(get_command_id("wait"));
144 }
145
146
147
148 extern char is_passable(struct Map * map, struct yx_uint16 pos)
149 {
150     uint8_t passable = 0;
151     if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
152     {
153         passable = (('.' == map->cells[pos.y * map->size.x + pos.x]));
154     }
155     return passable;
156 }
157
158
159
160 extern void player_drop()
161 {
162     struct MapObj * player = get_player();
163     if (NULL == player->owns)
164     {
165         update_log("\nYou try to drop an object, but you own none.");
166         world.old_inventory_select = 0;
167     }
168     else
169     {
170         world.old_inventory_select = world.inventory_select;
171         struct MapObj * owned = player->owns;
172         uint8_t i = 0;
173         for (; i != world.inventory_select; i++, owned = owned->next);
174         if (0 < world.inventory_select)
175         {
176             world.inventory_select--;
177         }
178         own_map_object(&world.map_objs, &player->owns, owned->id);
179         update_log("\nYou drop an object.");
180     }
181     turn_over(get_command_id("drop"));
182 }
183
184
185
186 extern void player_pick()
187 {
188     struct MapObj * player = get_player();
189     struct MapObj * picked;
190     for (picked = world.map_objs; NULL != picked; picked = picked->next)
191     {
192         if (picked != player && yx_uint16_cmp(&picked->pos, &player->pos))
193         {
194             break;
195         }
196     }
197     if (NULL == picked)
198     {
199         update_log("\nYou try to pick up an object, but there is none.");
200     }
201     else
202     {
203         own_map_object(&player->owns, &world.map_objs, picked->id);
204         set_object_position(picked, player->pos);
205         update_log("\nYou pick up an object.");
206     }
207     turn_over(get_command_id("pick"));
208 }