timesynk/old/game.c

195 lines
5.8 KiB
C

/****
***/
#include <stdlib.h>
#include "main.h"
#include "common.h"
#include "game.h"
#include "map.h"
#include "wall.h"
#include "display.h"
#include "tile.h"
#include "context.h"
#include "player.h"
#include "npc.h"
#include "console.h"
#include "controller.h"
#include "inventory.h"
int gameInit() {
null_tile.tid = 0;
player = newTile(PLAYER, 0, 2, 2);
player->tid = 1;
player->id = 12;
((struct PlayerTile*)player->data)->inventory.max_slots = 8;
((struct PlayerTile*)player->data)->inventory.slots_per_row = 4;
((struct PlayerTile*)player->data)->inventory.owner = player;
((struct PlayerTile*)player->data)->equipment.max_slots = 99;
((struct PlayerTile*)player->data)->equipment.slots_per_row = 4;
((struct PlayerTile*)player->data)->equipment.owner = player;
addToInventory( &((struct PlayerTile*)player->data)->inventory, newTile(EQUIP, 1, 0, 0));
addToInventory( &((struct PlayerTile*)player->data)->inventory, newTile(EQUIP, 0, 0, 0));
//gameMoveTile(newTile(ITEM, 0, 4, 4), 4, 4);
current_context = &walkContext;
allocateMap(&current_map, 64, 64);
floodMap(&current_map, TILE_REPLACE, FLOOR, STONE);
gameMoveTile(player, 2, 2);
appendTile(&(current_map->matrix[4][4]), EQUIP, 0);
appendTile(&(current_map->matrix[3][4]), EQUIP, 1);
appendTile(&(current_map->matrix[2][4]), EQUIP, 0);
appendTile(&(current_map->matrix[6][4]), EQUIP, 1);
appendTile(&(current_map->matrix[5][11]), ITEM, 0);
drawPath(&current_map, TILE_APPEND, WALL, STONE, 6, 1, 8, 12);
drawPath(&current_map, TILE_APPEND, WALL, STONE, 12, 4, 7, 13);
drawLine(&current_map, TILE_APPEND, WALL, WOOD, 18, 0, 18, 15);
replaceTile((current_map->matrix[18][10]).next, DOOR, 0);
drawLine(&current_map, TILE_APPEND, WALL, WOOD, 8, 11, 17, 11);
replaceTile((current_map->matrix[15][11]).next, DOOR, 0);
replaceTile((current_map->matrix[13][11]).next, DOOR, 0);
replaceTile((current_map->matrix[10][11]).next, DOOR, 0);
drawLine(&current_map, TILE_APPEND, WALL, STONE, 0, 0, 31, 0);
drawLine(&current_map, TILE_APPEND, WALL, STONE, 0, 15, 15, 15);
drawLine(&current_map, TILE_APPEND, WALL, STONE, 17, 15, 31, 15);
drawLine(&current_map, TILE_APPEND, WALL, STONE, 0, 0, 0, 15);
drawLine(&current_map, TILE_APPEND, WALL, STONE, 31, 0, 31, 15);
drawLine(&current_map, TILE_APPEND, WALL, STONE, 16, 23, 16, 62);
appendTile(&current_map->matrix[2][5], NPC, NUPI_SCRAPPER);
appendTile(&current_map->matrix[25][9], NPC, NUPI_SCRAPPER);
return SUCCESS;
}
void gameLoop() {
int step_x = player->x - ((struct PlayerTile*)player->data)->vision+8;
int step_y = player->y - ((struct PlayerTile*)player->data)->vision+8;
int end_x = player->x + ((struct PlayerTile*)player->data)->vision+8;
int end_y = player->y + ((struct PlayerTile*)player->data)->vision+8;
while (step_x < end_x) {
step_y = player->y - ((struct PlayerTile*)player->data)->vision+8;
while (step_y < end_y) {
//if (visible_matrix[step_x][step_y] & TILE_VISIBLE) {
if (step_x >= 0 && step_y >= 0 && step_x < current_map->width && step_y < current_map->height) {
struct Tile *current_tile;
current_tile = &(current_map)->matrix[step_x][step_y];
while(current_tile) {
switch(current_tile->tid) {
case NPC:
npcThink(current_tile);
break;
}
current_tile = current_tile->next;
}
}
step_y++;
}
step_x++;
}
}
void gameClose() {
consoleLog("gameClose()");
freeMap(&current_map);
}
int gameCollision(target_x, target_y) {
if (target_x < 0 || target_y < 0 || target_y > current_map->height-1 || target_x > current_map->width-1)
return 1; // don't go off the map, bb
struct Tile *debug_tile;
debug_tile = &(current_map->matrix[target_x][target_y]);
// TODO: call collision functions for each tile type
while(debug_tile) {
switch(debug_tile->tid) {
case WALL:
return 1;
break;
case FLOOR:
// return 0;
break;
case DOOR:
switch(((struct DoorTile*)debug_tile->data)->state) {
case STATE_CLOSED:
return 1;
break;
}
break;
case PLAYER:
return PLAYER;
break;
case NPC:
return NPC;
break;
}
debug_tile = debug_tile->next;
}
return 0;
}
int isCellVisible(int target_x, int target_y) {
if (target_x >= 0 && target_y >= 0 && target_x < current_map->width && target_y < current_map->height) {
struct Tile *current_tile;
current_tile = &(current_map)->matrix[target_x][target_y];
while(current_tile) {
switch (current_tile->tid) {
case WALL:
return 1;
break;
}
current_tile = current_tile->next;
}
}
return 0;
}
void gameMoveTile(struct Tile *tile, int target_x, int target_y) {
// remove tile from chain and connect tile's neighbors to each other
if (tile->prev) {
if (tile->next) {
tile->prev->next = tile->next;
} else {
tile->prev->next = NULL;
}
}
if (tile->next != NULL) {
if(tile->prev)
tile->next->prev = tile->prev;
}
// now add tile to the end of the target chain
struct Tile *target_loc;
target_loc = &(current_map->matrix[target_x][target_y]);
while (target_loc) {
if (!target_loc->next) { // last element
target_loc->next = tile;
tile->prev = target_loc;
tile->next = NULL;
break;
}
target_loc = target_loc->next;
}
// mark the tiles to be visually updated
gameUpdateTile(tile->x, tile->y);
gameUpdateTile(target_x, target_y);
// finally, update tile's x and y props
tile->x = target_x;
tile->y = target_y;
}
void gameUpdateTile(int x, int y) {
display_map[x][y] = 2;
}
int roll(int min, int max) {
return ((rand() % (max+1-min))+min);
}
int sign(int number) {
return (number < 0 ? -1 : 1);
}