timesynk/tile.c

110 lines
3.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h> // memcpy
#include "tile.h"
#include "wall.h"
#include "common.h"
#include "npc.h"
#include "message.h"
#include "player.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);
}
int activateTile(struct Tile *target_tile, struct Tile *activator_tile) {
char string[64];
switch (target_tile->tid) {
case DOOR:
switch (((struct DoorTile*)target_tile->data)->state) {
case STATE_OPEN:
((struct DoorTile*)target_tile->data)->state = STATE_CLOSED;
sprintf(string, MESSAGE_CLOSE, ((struct BasicTile*)target_tile->data)->name);
break;
case STATE_CLOSED:
((struct DoorTile*)target_tile->data)->state = STATE_OPEN;
sprintf(string, MESSAGE_OPEN, ((struct BasicTile*)target_tile->data)->name);
break;
}
messageTile(target_tile, activator_tile, string);
return ((struct DoorTile*)target_tile->data)->state;
break;
case NPC:
sprintf(string, "You turn on the %s - how naughty.", ((struct BasicTile*)target_tile->data)->name);
break;
default:
sprintf(string, "You see nothing to activate.");
break;
}
messageTile(target_tile, activator_tile, string);
return 0;
}
struct DoorTile doors[] = {
{ NO_PASS, "wooden door", STATE_CLOSED } // 0
};
struct PlayerTile players[] = {
{ NO_PASS, "selfie", 16}
};
//{ collision, name, target tile, behavior, basevision}
struct NpcTile npcs[] = {
{ NO_PASS, "nupi", 0, BEHAVE_WANDER, 4}
};