#include "thing_actions.h"
#include <stddef.h> /* NULL */
-#include <stdint.h> /* uint8_t, INT16_MIN */
+#include <stdint.h> /* uint8_t, INT16_MIN, INT16_MAX */
#include <stdio.h> /* sprintf() */
#include <stdlib.h> /* free() */
#include <string.h> /* strlen() */
-#include "../common/rexit.h" /* exit_trouble() */
+#include "../common/rexit.h" /* exit_err(), exit_trouble() */
#include "../common/try_malloc.h" /* try_malloc() */
#include "../common/yx_uint8.h" /* yx_uint8 */
#include "field_of_view.h" /* build_fov_map() */
{
struct Thing * picked = NULL;
struct Thing * t_i;
+ uint8_t highest_id = 0;
for (t_i = world.things; t_i; t_i = t_i->next)
{
if (t_i != t && t_i->pos.y == t->pos.y && t_i->pos.x == t->pos.x)
{
- picked = t_i;
+ if (t_i->id >= highest_id) /* With several Things to pick, */
+ { /* pick the one with the highest ID. */
+ highest_id = t_i->id;
+ picked = t_i;
+ }
}
}
if (picked)
}
t->satiation = t->satiation + tt->consumable > INT16_MAX ?
INT16_MAX : t->satiation + tt->consumable;
- t->lifepoints = t->lifepoints + tt->consumable;
}
}
if (t == get_player())
-extern void hunger(struct Thing * t)
+extern void try_healing(struct Thing * t)
{
struct ThingType * tt = get_thing_type(t->type);
- if (!(tt->stomach))
+ if ( t->satiation > 0 && t->lifepoints < tt->lifepoints
+ && 0 == (rrand() % 31)
+ && get_thing_action_id_by_name(s[S_CMD_WAIT]) == t->command)
{
- return;
+ t->lifepoints++;
+ t->satiation = t->satiation - 32;
+ if (get_player() == t)
+ {
+ update_log("You heal.");
+ }
+ else
+ {
+ char * msg_part = " heals.";
+ uint8_t len = strlen(tt->name) + strlen(msg_part) + 1;
+ char * msg = try_malloc(len, __func__);
+ int test = sprintf(msg, "%s%s", tt->name, msg_part);
+ exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
+ update_log(msg);
+ free(msg);
+ }
}
+}
+
+
+
+extern void hunger(struct Thing * t)
+{
if (t->satiation > INT16_MIN)
{
t->satiation--;
}
+ struct ThingType * tt = get_thing_type(t->type);
uint16_t testbase = t->satiation < 0 ? -(t->satiation) : t->satiation;
- uint16_t endurance = tt->stomach;
+ exit_err(!(tt->lifepoints), "A thing that should not hunger is hungering.");
+ uint16_t endurance = INT16_MAX / tt->lifepoints;
if ((testbase / endurance) / ((rrand() % endurance) + 1))
{
if (get_player() == t)