Mentions légales du service

Skip to content
Snippets Groups Projects
events_player.c 1.64 KiB
/*
 * Triggers and resolutions related to items.
*/

#include "events_player.h"

int walks_on_item() {
    int x = player_x;
    int y = player_y;
    map * cmap = current_map();
    // quick check to avoid useless looping
    if (item(y, x) == NO_ITEM) {
        return 0;
    }

    /* we loop over the possible items until we see one for which the character
     corresponds to where the player is standing.
    */
    int i = 0;
    while (i < MAX_POSSIBLE_ITEMS) {
        if (list[i]->code == item(y, x)) {
            /* we found an item.
             My first algo teacher would say it's very bad to exist a loop
             like this, but this whole code is for educational purposes,
             isn't it?
            */
            return 1;
        }
        i++;
    }
    // nothing found
    return 0;
}

void pick_item(int trig) {
    // here the trig parameter is not used

    /* How to add an item to the inventory, knowing some are stackable?
     1. if item of this type and space to stack = stack
     2. if there is a free space = new instance
     3. if no item of this type and no place in inventory = exit
     Goal: we shouldn't have two instances with stack space left
    */
    int x = player_x;
    int y = player_y;
    map * cmap = current_map();
    /* because the trigger function was called before, we are guaranteed
     that the player is currently on an item
    */
    itemtype item_code = item(y, x);

    if (add_item_to_inventory(item_code)) {
        cmap->items[y * cmap->width + x] = NO_ITEM;
    }    
}

int death_imminent() {
    return (health_points < 1);
}

void death(int trig) {
    printf("You die.\n");
    level_failed();
}