start to handle different locations
This commit is contained in:
parent
f55731906a
commit
375509d58d
158
w/w.c
158
w/w.c
|
@ -1,10 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
char orientation;
|
||||
char place[2];
|
||||
} Location;
|
||||
|
||||
typedef struct {
|
||||
|
@ -12,24 +15,51 @@ typedef struct {
|
|||
Location location;
|
||||
} Gamestate;
|
||||
|
||||
const int MAP_SIZE = 9;
|
||||
const char* statepath = "w.state";
|
||||
char pov[3][3];
|
||||
unsigned char pov3d[10][10];
|
||||
Gamestate state;
|
||||
char map[9][9];
|
||||
|
||||
int MAP_SIZE = 0;
|
||||
char* map = NULL;
|
||||
|
||||
bool inmap()
|
||||
{
|
||||
return state.location.place[0] == 'f';
|
||||
}
|
||||
|
||||
char charatpos(int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= MAP_SIZE || y >= MAP_SIZE)
|
||||
{
|
||||
return ' ';
|
||||
}
|
||||
return map[ (MAP_SIZE - y - 1) * MAP_SIZE + x];
|
||||
}
|
||||
|
||||
void loadmap()
|
||||
{
|
||||
FILE* file = fopen("f1.map", "r");
|
||||
for (int i = 0; i < MAP_SIZE; i++)
|
||||
char filename[6];
|
||||
strcpy(filename, state.location.place);
|
||||
strcat(filename, ".map");
|
||||
FILE* file = fopen(filename, "r");
|
||||
while (fgetc(file) != '\n')
|
||||
{
|
||||
for (int j = 0; j < MAP_SIZE; j++)
|
||||
MAP_SIZE++;
|
||||
}
|
||||
rewind(file);
|
||||
|
||||
map = (char*)malloc(MAP_SIZE * MAP_SIZE * sizeof(char));
|
||||
|
||||
char c = 0;
|
||||
int i = 0;
|
||||
while (c != EOF)
|
||||
{
|
||||
c = fgetc(file);
|
||||
if (c != '\n')
|
||||
{
|
||||
map[i][j] = fgetc(file);
|
||||
map[i++] = c;
|
||||
}
|
||||
// skip lf
|
||||
fgetc(file);
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
|
@ -38,9 +68,9 @@ void drawmap()
|
|||
{
|
||||
for (int i = 0; i < MAP_SIZE; i++) {
|
||||
int y = MAP_SIZE - i - 1;
|
||||
printf("%d", y);
|
||||
//printf("%d", y);
|
||||
for (int j = 0; j < MAP_SIZE; j++) {
|
||||
char toprint = map[i][j];
|
||||
char toprint = map[i * MAP_SIZE + j];
|
||||
if (y == state.location.y && j == state.location.x)
|
||||
{
|
||||
if (state.location.orientation == 'n') toprint = '^';
|
||||
|
@ -52,7 +82,7 @@ void drawmap()
|
|||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf(" 012345678\n");
|
||||
//printf(" 012345678\n");
|
||||
}
|
||||
|
||||
void loadstate()
|
||||
|
@ -73,15 +103,6 @@ void savestate()
|
|||
fclose(file);
|
||||
}
|
||||
|
||||
char charatpos(int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= MAP_SIZE || y >= MAP_SIZE)
|
||||
{
|
||||
return ' ';
|
||||
}
|
||||
return map[8-y][x];
|
||||
}
|
||||
|
||||
void update3dpov()
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
|
@ -329,39 +350,53 @@ void dumpstate()
|
|||
state.location.orientation);
|
||||
}
|
||||
|
||||
void firstgame()
|
||||
{
|
||||
printf("Welcome to space dungeons.\n\nEnter your name: ");
|
||||
fgets(state.name, sizeof(state.name), stdin);
|
||||
state.name[strcspn(state.name, "\n")] = 0; // remove last lf
|
||||
|
||||
// start at station
|
||||
strcpy(state.location.place, "st");
|
||||
state.location.x = 1;
|
||||
state.location.y = 1;
|
||||
state.location.orientation = 'n';
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
loadmap();
|
||||
loadstate();
|
||||
if (strlen(state.name) == 0)
|
||||
{
|
||||
printf("Welcome.\nEnter your name: ");
|
||||
fgets(state.name, sizeof(state.name), stdin);
|
||||
state.name[strcspn(state.name, "\n")] = 0;
|
||||
state.location.x = 1;
|
||||
state.location.y = 1;
|
||||
state.location.orientation = 'n';
|
||||
firstgame();
|
||||
}
|
||||
updatepov();
|
||||
}
|
||||
|
||||
void update(char command)
|
||||
{
|
||||
switch (command)
|
||||
if (inmap())
|
||||
{
|
||||
case 's':
|
||||
forward();
|
||||
break;
|
||||
case 'x':
|
||||
right();
|
||||
break;
|
||||
case 'w':
|
||||
left();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (map == NULL)
|
||||
{
|
||||
loadmap();
|
||||
}
|
||||
switch (command)
|
||||
{
|
||||
case 's':
|
||||
forward();
|
||||
break;
|
||||
case 'x':
|
||||
right();
|
||||
break;
|
||||
case 'w':
|
||||
left();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
updatepov();
|
||||
}
|
||||
updatepov();
|
||||
|
||||
}
|
||||
|
||||
void draw()
|
||||
|
@ -373,24 +408,33 @@ void draw()
|
|||
system("clear");
|
||||
#endif
|
||||
|
||||
int shift = 10;
|
||||
for (int i = 0; i < shift; i++) printf("\n");
|
||||
|
||||
for (int i = 0; i < shift; i++) printf(" ");
|
||||
printf("o----------o\n");
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
if (inmap())
|
||||
{
|
||||
for (int s = 0; s < shift; s++) printf(" ");
|
||||
printf("|");
|
||||
for (int j = 0; j < 10; j++)
|
||||
int shift = 10;
|
||||
for (int i = 0; i < shift; i++) printf("\n");
|
||||
|
||||
for (int i = 0; i < shift; i++) printf(" ");
|
||||
printf("o----------o\n");
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
printf("%c", pov3d[i][j]);
|
||||
for (int s = 0; s < shift; s++) printf(" ");
|
||||
printf("|");
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
printf("%c", pov3d[i][j]);
|
||||
}
|
||||
printf("|\n");
|
||||
}
|
||||
printf("|\n");
|
||||
for (int i = 0; i < shift; i++) printf(" ");
|
||||
printf("o----------o\n");
|
||||
|
||||
drawmap();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s's location is %s", state.name, state.location.place);
|
||||
}
|
||||
for (int i = 0; i < shift; i++) printf(" ");
|
||||
printf("o----------o\n");
|
||||
}
|
||||
|
||||
|
||||
|
@ -421,14 +465,12 @@ void drawtopdownpov()
|
|||
int main()
|
||||
{
|
||||
init();
|
||||
|
||||
char command = ' ';
|
||||
while (command != 'q')
|
||||
{
|
||||
update(command);
|
||||
draw();
|
||||
dumpstate();
|
||||
drawmap();
|
||||
drawtopdownpov();
|
||||
command = getchar();
|
||||
}
|
||||
savestate();
|
||||
|
|
Loading…
Reference in New Issue