timesynk/tile.c

80 lines
2.1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h> // memcpy
#include "tile.h"
#include "wall.h"
#include "common.h"
#include "npc.h"
int allocateTile(struct Tile** tile, unsigned int tid, short id) {
*tile = (struct Tile *) malloc(sizeof(struct Tile));
if (tile == NULL) {
printf("ERROR: could not malloc Tile");
return ERROR;
}
(*tile)->tid = tid;
(*tile)->id = id;
return SUCCESS;
}
void freeTile(struct Tile* tile) {
// @@ FIXME
free(tile->data);
free(tile);
}
struct Tile *newTile(unsigned int type_id, short id, short x, short y) {
struct Tile *new_tile;
new_tile = malloc(sizeof(struct Tile));
if (new_tile == NULL) {
printf("ERROR: could not malloc Tile");
return NULL;
}
new_tile->tid = type_id;
new_tile->id = id;
new_tile->x = x;
new_tile->y = y;
new_tile->next = NULL;
new_tile->prev = NULL;
switch(type_id) {
case WALL:
new_tile->data = (WallTile *) malloc(sizeof(WallTile));
memcpy(new_tile->data, &walls[id], sizeof(WallTile));
break;
case FLOOR:
new_tile->data = (FloorTile *) malloc(sizeof(FloorTile));
memcpy(new_tile->data, &floors[id], sizeof(FloorTile));
break;
case DOOR:
new_tile->data = (struct DoorTile *) malloc(sizeof(struct DoorTile));
memcpy(new_tile->data, &doors[id], sizeof(struct DoorTile));
break;
case PLAYER:
new_tile->data = (struct PlayerTile *) malloc(sizeof(struct PlayerTile));
memcpy(new_tile->data, &players[id], sizeof(struct PlayerTile));
break;
case NPC:
new_tile->data = (struct NpcTile *) malloc(sizeof(struct NpcTile));
memcpy(new_tile->data, &npcs[id], sizeof(struct NpcTile));
break;
default:
new_tile->data = (struct BasicTile *) malloc(sizeof(struct BasicTile));
memcpy(new_tile->data, &walls[id], sizeof(struct BasicTile));
break;
}
return (new_tile);
}
struct DoorTile doors[] = {
{ NO_PASS, "wooden door", STATE_CLOSED } // 0
};
struct PlayerTile players[] = {
{ NO_PASS, "selfie"}
};
//{ collision, name, target tile, behavior, basevision}
struct NpcTile npcs[] = {
{ NO_PASS, "nupi", 0, BEHAVE_WANDER, 4}
};