Files
Not-so-Rogue-like/src/in_game.c

320 lines
6.8 KiB
C
Raw Normal View History

/**
* Copyright (c) 2014, Miguel Angel Astor Romero. All rights reserved.
* See the file LICENSE for more details.
*/
#include <stdlib.h>
#include <time.h>
#include <ncursesw/ncurses.h>
#include <island.h>
#include "constants.h"
#include "in_game.h"
2014-03-05 20:54:24 -04:30
#include "map.h"
2014-03-03 21:54:15 -04:30
typedef struct PLAYER {
unsigned short x;
unsigned short y;
} player_t;
static const int I_SIZE = 513;
static int ** imap;
static bool ** wmap;
static bool w_mov = FALSE;
2014-03-03 21:54:15 -04:30
static bool uK, dK, lK, rK;
static clock_t then;
2014-03-03 21:54:15 -04:30
static player_t player;
2014-03-05 20:54:24 -04:30
static map_cell_t ** map;
2014-03-06 01:58:49 -04:30
game_obj_t objs[MAX_OBJECTS];
static int mW, mH, nO;
void input();
gsname_t update();
void render(int, int);
2014-03-03 21:54:15 -04:30
void drawGui(int, int);
void setPlayerStart();
2014-03-06 01:58:49 -04:30
void initObjects();
void initInGameState( gs_t * gs) {
2014-03-03 21:54:15 -04:30
int n, i, j;
2014-03-05 20:54:24 -04:30
float ** fmap;
2014-03-03 21:54:15 -04:30
gs->name = IN_GAME;
gs->input = &input;
gs->update = &update;
gs->render = &render;
n = I_SIZE;
srand(time(NULL));
2014-03-05 20:54:24 -04:30
fmap = ( float ** ) malloc ( sizeof ( float * ) * n);
2014-03-03 21:54:15 -04:30
for ( i = 0; i < n; ++i ) {
2014-03-05 20:54:24 -04:30
fmap[ i ] = ( float * ) calloc ( n, sizeof ( float ) );
2014-03-03 21:54:15 -04:30
}
imap = ( int ** ) malloc ( sizeof ( int * ) * n);
for ( i = 0; i < n; ++i ) {
imap[ i ] = ( int * ) calloc ( n, sizeof ( int ) );
}
wmap = ( bool ** ) malloc ( sizeof ( bool * ) * n);
for ( i = 0; i < n; ++i ) {
wmap[ i ] = ( bool * ) calloc ( n, sizeof ( bool ) );
for(j = 0; j < n; ++j){
wmap[i][j] = rand() % 2;
}
}
2014-03-05 20:54:24 -04:30
ds ( &fmap, n );
2014-03-03 21:54:15 -04:30
island ( &imap, n );
normInt ( &imap, n );
2014-03-05 20:54:24 -04:30
norm ( &fmap, n );
mult ( &fmap, &imap, n );
2014-03-03 21:54:15 -04:30
smooth( &imap, n );
normInt ( &imap, n );
for ( i = 0; i < n; ++i ) {
2014-03-05 20:54:24 -04:30
free(fmap[ i ]);
2014-03-03 21:54:15 -04:30
}
2014-03-05 20:54:24 -04:30
free(fmap);
2014-03-03 21:54:15 -04:30
setPlayerStart();
uK = dK = lK = rK = FALSE;
2014-03-05 20:54:24 -04:30
/*map = ( map_cell_t ** ) malloc ( sizeof ( map_cell_t * ) * 32);
for ( i = 0; i < 32; ++i ) {
map[ i ] = ( map_cell_t * ) calloc ( 32 , sizeof ( map_cell_t ) );
}
2014-03-06 01:58:49 -04:30
initObjects();
2014-03-05 20:54:24 -04:30
errcode_t rc = readMapData("map_file.map", &map, &mW, &mH);
fprintf(stderr, "\t%s: readMapData() returned %d\n", __FILE__, rc);
fprintf(stderr, "\t%s: Map size is (%d, %d).\n", __FILE__, mW, mH);
2014-03-06 01:58:49 -04:30
game_obj_t * objsP = objs;
rc = readMapObjects("map_file.map", &objsP, &nO);
fprintf(stderr, "\t%s: readMapObjects() returned %d\n", __FILE__, rc);
fprintf(stderr, "\t%s: Number of objects is %d.\n", __FILE__, nO);
for(i = 0; i < nO; i++){
fprintf(stderr, "\t%s: Object %d\n", __FILE__, i);
fprintf(stderr, "\t\t Type %d\n", (int)objs[i].type);
fprintf(stderr, "\t\t x: %d -- y: %d -- eX: %d -- eY: %d -- sX: %d -- sY: %d\n", objs[i].x, objs[i].x, objs[i].eX, objs[i].eY, objs[i].sX, objs[i].sY);
fprintf(stderr, "\t\t iD: %d -- dId %d\n", objs[i].id, objs[i].dId);
fprintf(stderr, "\t\t name: %s\n", objs[i].name);
fprintf(stderr, "\t\t target: %s\n", objs[i].target);
fprintf(stderr, "\t\t dialog: %s\n", objs[i].dialog);
fprintf(stderr, "\t\t unlocked: %d\n", objs[i].unlocked);
fprintf(stderr, "\n");
}
2014-03-05 20:54:24 -04:30
for ( i = 0; i < 32; ++i ) {
free(map[ i ]);
}
free(map);*/
}
void input(){
2014-03-03 21:54:15 -04:30
int key = 0;
2014-03-03 21:54:15 -04:30
key = getch();
2014-03-02 16:35:14 -04:30
2014-03-03 21:54:15 -04:30
if(key != ERR){
fprintf(stderr, "\t%s: Caught keycode %d\n", __FILE__, key);
if(key == KEY_UP) uK = TRUE;
if(key == KEY_DOWN) dK = TRUE;
if(key == KEY_LEFT) lK = TRUE;
if(key == KEY_RIGHT) rK = TRUE;
}
}
gsname_t update(){
2014-03-03 21:54:15 -04:30
if(uK){
if(terrainType( imap[player.x][player.y - 1] ) != DEEP_WATER && terrainType( imap[player.x][player.y - 1] ) != MOUNTAIN) player.y -= 1;
uK = FALSE;
}
if(dK){
if(terrainType( imap[player.x][player.y + 1]) != DEEP_WATER && terrainType( imap[player.x][player.y + 1]) != MOUNTAIN ) player.y += 1;
dK = FALSE;
}
if(lK){
if(terrainType( imap[player.x - 1][player.y]) != DEEP_WATER && terrainType( imap[player.x - 1][player.y]) != MOUNTAIN) player.x -= 1;
lK = FALSE;
}
if(rK){
if(terrainType( imap[player.x + 1][player.y]) != DEEP_WATER && terrainType( imap[player.x + 1][player.y]) != MOUNTAIN) player.x += 1;
rK = FALSE;
}
return IN_GAME;
}
void render(int w, int h){
2014-03-03 21:54:15 -04:30
clock_t now, delta;
int i, j, pi, pj, ioff, joff, di, dj;
now = clock();
delta = now - then;
if((float)delta / (float)CLOCKS_PER_SEC >= 0.25f){
then = now;
w_mov = TRUE;
}
pi = (((w - 1) - 27) / 2) + 27;
pj = (h - 2) / 2 + 1;
ioff = (w - 28) / 2;
joff = (h - 2) / 2;
for(i = 27; i < w - 1; i++){
for(j = 1; j < h - 1; j++){
move(j, i);
di = i - 27 + player.x - ioff;
dj = j - 1 + player.y - joff;
if( di < 0 || di >= I_SIZE){
printw(" ");
}else{
switch(terrainType( imap[di][dj] )){
case DEEP_WATER:
attron(COLOR_PAIR(DW_COLOR));
if(w_mov)
wmap[di][dj] = !wmap[di][dj];
if(wmap[di][dj])
printw("\u2248");
else
printw("~");
break;
case SHALLOW_WATER:
attron(COLOR_PAIR(SW_COLOR));
if(w_mov)
wmap[di][dj] = !wmap[di][dj];
if(wmap[di][dj])
printw("\u2248");
else
printw("~");
break;
case SAND:
attron(COLOR_PAIR(SN_COLOR));
printw(".");
break;
case GRASS:
attron(COLOR_PAIR(GR_COLOR));
printw("n");
break;
case FOREST:
attron(COLOR_PAIR(FR_COLOR));
printw("\u2660");
break;
case HILL:
attron(COLOR_PAIR(HL_COLOR));
printw("\u2302");
break;
case MOUNTAIN:
attron(COLOR_PAIR(MN_COLOR));
printw("\u25B2");
break;
}
}
}
}
w_mov = FALSE;
move(pj, pi);
attron(COLOR_PAIR(BSC_COLOR));
printw(/*"\u263A"*/ "@");
drawGui(w, h);
}
void drawGui(int w, int h){
int i, j;
attron(COLOR_PAIR(BSC_COLOR));
/* Clear the gui space. */
for(i = 1; i < 26; i++){
for(j = 1; j < h - 1; j++){
move(j, i);
printw(" ");
}
}
/* Upper horizontal bar. */
move(0, 0);
printw("\u2554");
for(i = 0; i < w - 2; i++){
if(i != 25){
printw("\u2550");
}else{
printw("\u2566");
}
}
printw("\u2557");
/* Lower horizontal bar. */
move(h - 1, 0);
printw("\u255A");
for(i = 0; i < w - 2; i++){
if(i != 25){
printw("\u2550");
}else{
printw("\u2569");
}
}
printw("\u255D");
/* Vertical bars. */
for(i = 1; i < h - 1; i++){
move(i, 0);
printw("\u2551");
move(i, 26);
printw("\u2551");
move(i, w-1);
printw("\u2551");
}
}
void setPlayerStart(){
int x, y;
bool posFound = false;
while(!posFound){
x = (I_SIZE / 4) + (rand() % (I_SIZE / 2));
y = (I_SIZE / 4) + (rand() % (I_SIZE / 2));
if(terrainType(imap[x][y]) == GRASS){
player.x = x;
player.y = y;
posFound = true;
}
}
}
2014-03-06 01:58:49 -04:30
void initObjects(){
int i;
for(i = 0; i < MAX_OBJECTS; ++i){
objs[i].type = NONE;
objs[i].x = 0;
objs[i].y = 0;
objs[i].eX = 0;
objs[i].eY = 0;
objs[i].sX = 0;
objs[i].sY = 0;
objs[i].id = 0;
objs[i].dId = 0;
objs[i].name[0] = '\0';
objs[i].target[0] = '\0';
objs[i].dialog[0] = '\0';
objs[i].unlocked = 0;
}
}