#include <client_api.h>
#include "constants.h"
#include "ent.h"

#include <ALLOC_IMPORTS.h>
#include "rpg_imports.h"

bool in_door = false;

__EXPORT__ void level_entered_tile(ent_t* ent, int gx, int gy, grid_t g)
{
    if (g.tag)
    {
        if (is_player(ent))
        {
            grid_t* find = NULL;
            if (strcmp(g.tag, "d1") == 0)
            {
                if (!in_door && (find = get_tile_by_tag("d2")) != NULL)
                {
                    int target_gx, target_gy;
                    grid_pos(find, &target_gx, &target_gy);
                    ent->x = target_gx * GRID;
                    ent->y = target_gy * GRID;
                    in_door = true;
                }
            }
            if (strcmp(g.tag, "d2") == 0)
            {
                if (!in_door && (find = get_tile_by_tag("d1")) != NULL)
                {
                    int target_gx, target_gy;
                    grid_pos((grid_t*)find, &target_gx, &target_gy);
                    ent->x = target_gx * GRID;
                    ent->y = target_gy * GRID;
                    in_door = true;
                }
            }
        }
    }
}
__EXPORT__ void level_exited_tile(ent_t* ent, int gx, int gy, grid_t g)
{
}

__EXPORT__ void level_update(float ms)
{
    in_door = false;
}

__EXPORT__ int setup(int arg)
{
    LOAD_DYN(is_player)
    LOAD_DYN(get_tile_by_tag)
    LOAD_DYN(grid_pos)
}
