parent
853921b79b
commit
ffdd7ec2da
232
w/w.c
232
w/w.c
|
@ -1,28 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define CMD_LEN 10
|
||||
#define NAME_LEN 10
|
||||
#define POV_3D_SIZE 10
|
||||
#define POV_SIZE 3
|
||||
#define NULL_CMD "-"
|
||||
|
||||
#define PLACE_TITLE 0
|
||||
#define PLACE_SETNAME 1
|
||||
#define PLACE_STATION 2
|
||||
#define PLACE_MAP 3
|
||||
#define CMD_NULL "-"
|
||||
#define CMD_FWD 's'
|
||||
|
||||
#define SC_TITLE 0
|
||||
#define SC_SETNAME 1
|
||||
#define SC_STATION 2
|
||||
#define SC_MAP 3
|
||||
|
||||
#define NORTH 0
|
||||
#define EAST 1
|
||||
#define WEST 2
|
||||
#define SOUTH 3
|
||||
|
||||
#define TILE_WALL 'w'
|
||||
#define TILE_FREE ' '
|
||||
#define TILE_DOOR 'd'
|
||||
#define TILE_START 's'
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
char orientation;
|
||||
} Location; // rename position
|
||||
} Position;
|
||||
|
||||
typedef struct {
|
||||
char name[NAME_LEN];
|
||||
Location location;
|
||||
char place; // rename screen
|
||||
int hp;
|
||||
int gold;
|
||||
int atk;
|
||||
int def;
|
||||
} Character;
|
||||
|
||||
typedef struct {
|
||||
Character hero;
|
||||
Position position;
|
||||
char screen;
|
||||
} Gamestate;
|
||||
|
||||
const char* statepath = "w.state";
|
||||
|
@ -33,6 +54,15 @@ Gamestate state;
|
|||
int mapsize = 0;
|
||||
char* map = NULL;
|
||||
|
||||
Character monsters[1] = {
|
||||
{ "Alien", 1, 2, 1, 1 }
|
||||
};
|
||||
|
||||
int dice()
|
||||
{
|
||||
return 1 + (rand() % 6);
|
||||
}
|
||||
|
||||
char charatpos(int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= mapsize || y >= mapsize)
|
||||
|
@ -72,12 +102,12 @@ void drawmap()
|
|||
int y = mapsize - i - 1;
|
||||
for (int j = 0; j < mapsize; j++) {
|
||||
char toprint = map[i * mapsize + j];
|
||||
if (y == state.location.y && j == state.location.x)
|
||||
if (y == state.position.y && j == state.position.x)
|
||||
{
|
||||
if (state.location.orientation == 'n') toprint = '^';
|
||||
if (state.location.orientation == 's') toprint = 'v';
|
||||
if (state.location.orientation == 'e') toprint = '>';
|
||||
if (state.location.orientation == 'w') toprint = '<';
|
||||
if (state.position.orientation == NORTH) toprint = '^';
|
||||
if (state.position.orientation == SOUTH) toprint = 'v';
|
||||
if (state.position.orientation == EAST) toprint = '>';
|
||||
if (state.position.orientation == WEST) toprint = '<';
|
||||
}
|
||||
printf("%c", toprint);
|
||||
}
|
||||
|
@ -94,7 +124,7 @@ void savestate()
|
|||
|
||||
char currentchar()
|
||||
{
|
||||
return charatpos(state.location.x, state.location.y);
|
||||
return charatpos(state.position.x, state.position.y);
|
||||
}
|
||||
|
||||
void update3dpov()
|
||||
|
@ -111,7 +141,7 @@ void update3dpov()
|
|||
pov3d[7][i] = '_';
|
||||
}
|
||||
|
||||
if (pov[2][0] == ' ')
|
||||
if (pov[2][0] == TILE_FREE)
|
||||
{
|
||||
// front left free
|
||||
pov3d[1][0] = '_';
|
||||
|
@ -129,7 +159,7 @@ void update3dpov()
|
|||
for (int i = 2; i < 8; i++) pov3d[i][1] = '|';
|
||||
}
|
||||
|
||||
if (pov[2][2] == ' ')
|
||||
if (pov[2][2] == TILE_FREE)
|
||||
{
|
||||
// front right free
|
||||
pov3d[1][8] = '_';
|
||||
|
@ -147,7 +177,7 @@ void update3dpov()
|
|||
for (int i = 2; i < 8; i++) pov3d[i][7+1] = '|';
|
||||
}
|
||||
|
||||
if (pov[1][1] == ' ')
|
||||
if (pov[1][1] == TILE_FREE)
|
||||
{
|
||||
// far constants
|
||||
pov3d[3][4] = '_';
|
||||
|
@ -155,7 +185,7 @@ void update3dpov()
|
|||
pov3d[5][4] = '_';
|
||||
pov3d[5][5] = '_';
|
||||
|
||||
if (pov[0][0] == ' ')
|
||||
if (pov[0][0] == TILE_FREE)
|
||||
{
|
||||
// far left free
|
||||
pov3d[3][2] = '_';
|
||||
|
@ -164,7 +194,7 @@ void update3dpov()
|
|||
pov3d[5][3] = '_';
|
||||
|
||||
// if front is free as well
|
||||
if (pov[2][0] == ' ')
|
||||
if (pov[2][0] == TILE_FREE)
|
||||
{
|
||||
pov3d[3][0] = '_';
|
||||
pov3d[3][1] = '_';
|
||||
|
@ -186,7 +216,7 @@ void update3dpov()
|
|||
for (int i = 2; i < 8; i++) pov3d[i][1] = '|';
|
||||
}
|
||||
|
||||
if (pov[0][2] == ' ')
|
||||
if (pov[0][2] == TILE_FREE)
|
||||
{
|
||||
// far right free
|
||||
pov3d[3][6] = '_';
|
||||
|
@ -195,7 +225,7 @@ void update3dpov()
|
|||
pov3d[5][7] = '_';
|
||||
|
||||
// if front is free as well
|
||||
if (pov[2][2] == ' ')
|
||||
if (pov[2][2] == TILE_FREE)
|
||||
{
|
||||
pov3d[3][8] = '_';
|
||||
pov3d[3][9] = '_';
|
||||
|
@ -219,7 +249,7 @@ void update3dpov()
|
|||
}
|
||||
|
||||
const char doorchar = '#';
|
||||
if (pov[1][1] == 'd')
|
||||
if (pov[1][1] == TILE_DOOR)
|
||||
{
|
||||
// front door
|
||||
for (int i = 4; i <= 7; i++)
|
||||
|
@ -228,27 +258,27 @@ void update3dpov()
|
|||
pov3d[i][j] = doorchar;
|
||||
}
|
||||
}
|
||||
if (pov[2][0] == 'd')
|
||||
if (pov[2][0] == TILE_DOOR)
|
||||
{
|
||||
for (int i = 5; i < 9; i++) pov3d[i][0] = doorchar;
|
||||
}
|
||||
if (pov[2][2] == 'd')
|
||||
if (pov[2][2] == TILE_DOOR)
|
||||
{
|
||||
for (int i = 5; i < 9; i++) pov3d[i][9] = doorchar;
|
||||
}
|
||||
|
||||
if (currentchar() == 's')
|
||||
if (currentchar() == TILE_START)
|
||||
{
|
||||
pov3d[0][4] = '[';
|
||||
pov3d[0][5] = ']';
|
||||
pov3d[0][5] = ']';
|
||||
}
|
||||
}
|
||||
|
||||
void updatepov()
|
||||
{
|
||||
int x = state.location.x;
|
||||
int y = state.location.y;
|
||||
char orientation = state.location.orientation;
|
||||
int x = state.position.x;
|
||||
int y = state.position.y;
|
||||
char orientation = state.position.orientation;
|
||||
|
||||
for (int frontoffset = 2; frontoffset >= 0; frontoffset--)
|
||||
{
|
||||
|
@ -257,19 +287,19 @@ void updatepov()
|
|||
int newX = x, newY = y;
|
||||
switch (orientation)
|
||||
{
|
||||
case 'n':
|
||||
case NORTH:
|
||||
newX += rtoloffset;
|
||||
newY += frontoffset;
|
||||
break;
|
||||
case 's':
|
||||
case SOUTH:
|
||||
newX -= rtoloffset;
|
||||
newY -= frontoffset;
|
||||
break;
|
||||
case 'e':
|
||||
case EAST:
|
||||
newX += frontoffset;
|
||||
newY -= rtoloffset;
|
||||
break;
|
||||
case 'w':
|
||||
case WEST:
|
||||
newX -= frontoffset;
|
||||
newY += rtoloffset;
|
||||
break;
|
||||
|
@ -277,27 +307,25 @@ void updatepov()
|
|||
pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY);
|
||||
}
|
||||
}
|
||||
|
||||
update3dpov();
|
||||
}
|
||||
|
||||
void forward()
|
||||
{
|
||||
if (pov[1][1] != 'w')
|
||||
if (pov[1][1] != TILE_WALL)
|
||||
{
|
||||
switch (state.location.orientation)
|
||||
switch (state.position.orientation)
|
||||
{
|
||||
case 'n':
|
||||
state.location.y += 2;
|
||||
case NORTH:
|
||||
state.position.y += 2;
|
||||
break;
|
||||
case 's':
|
||||
state.location.y -= 2;
|
||||
case SOUTH:
|
||||
state.position.y -= 2;
|
||||
break;
|
||||
case 'e':
|
||||
state.location.x += 2;
|
||||
case EAST:
|
||||
state.position.x += 2;
|
||||
break;
|
||||
case 'w':
|
||||
state.location.x -= 2;
|
||||
case WEST:
|
||||
state.position.x -= 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -305,38 +333,38 @@ void forward()
|
|||
|
||||
void right()
|
||||
{
|
||||
switch (state.location.orientation)
|
||||
switch (state.position.orientation)
|
||||
{
|
||||
case 'n':
|
||||
state.location.orientation = 'e';
|
||||
case NORTH:
|
||||
state.position.orientation = EAST;
|
||||
break;
|
||||
case 's':
|
||||
state.location.orientation = 'w';
|
||||
case SOUTH:
|
||||
state.position.orientation = WEST;
|
||||
break;
|
||||
case 'e':
|
||||
state.location.orientation = 's';
|
||||
case EAST:
|
||||
state.position.orientation = SOUTH;
|
||||
break;
|
||||
case 'w':
|
||||
state.location.orientation = 'n';
|
||||
case WEST:
|
||||
state.position.orientation = NORTH;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void left()
|
||||
{
|
||||
switch (state.location.orientation)
|
||||
switch (state.position.orientation)
|
||||
{
|
||||
case 'n':
|
||||
state.location.orientation = 'w';
|
||||
case NORTH:
|
||||
state.position.orientation = WEST;
|
||||
break;
|
||||
case 's':
|
||||
state.location.orientation = 'e';
|
||||
case SOUTH:
|
||||
state.position.orientation = EAST;
|
||||
break;
|
||||
case 'e':
|
||||
state.location.orientation = 'n';
|
||||
case EAST:
|
||||
state.position.orientation = NORTH;
|
||||
break;
|
||||
case 'w':
|
||||
state.location.orientation = 's';
|
||||
case WEST:
|
||||
state.position.orientation = SOUTH;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -344,10 +372,10 @@ void left()
|
|||
void dumpstate()
|
||||
{
|
||||
printf("Name:%s\nx:%d\ny:%d\norientation:%c\n",
|
||||
state.name,
|
||||
state.location.x,
|
||||
state.location.y,
|
||||
state.location.orientation);
|
||||
state.hero.name,
|
||||
state.position.x,
|
||||
state.position.y,
|
||||
state.position.orientation);
|
||||
}
|
||||
|
||||
void clearscreen()
|
||||
|
@ -361,6 +389,7 @@ void clearscreen()
|
|||
|
||||
void init()
|
||||
{
|
||||
srand(time(NULL));
|
||||
FILE* file = fopen(statepath, "r");
|
||||
if (file)
|
||||
{
|
||||
|
@ -369,7 +398,7 @@ void init()
|
|||
}
|
||||
else
|
||||
{
|
||||
state.place = PLACE_TITLE;
|
||||
state.screen = SC_TITLE;
|
||||
}
|
||||
loadmap();
|
||||
}
|
||||
|
@ -377,11 +406,11 @@ void init()
|
|||
void update(char command[CMD_LEN])
|
||||
{
|
||||
char c = command[0];
|
||||
if (state.place == PLACE_MAP)
|
||||
if (state.screen == SC_MAP)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 's':
|
||||
case CMD_FWD:
|
||||
case '\0':
|
||||
forward();
|
||||
break;
|
||||
|
@ -392,9 +421,9 @@ void update(char command[CMD_LEN])
|
|||
left();
|
||||
break;
|
||||
case 'r':
|
||||
if (currentchar() == 's')
|
||||
if (currentchar() == TILE_START)
|
||||
{
|
||||
state.place = PLACE_STATION;
|
||||
state.screen = SC_STATION;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -402,43 +431,52 @@ void update(char command[CMD_LEN])
|
|||
}
|
||||
updatepov();
|
||||
}
|
||||
else if (state.place == PLACE_TITLE)
|
||||
else if (state.screen == SC_TITLE)
|
||||
{
|
||||
if (strcmp(command, NULL_CMD) != 0)
|
||||
if (strcmp(command, CMD_NULL) != 0)
|
||||
{
|
||||
state.place = PLACE_SETNAME;
|
||||
state.screen = SC_SETNAME;
|
||||
}
|
||||
}
|
||||
else if (state.place == PLACE_SETNAME)
|
||||
else if (state.screen == SC_SETNAME)
|
||||
{
|
||||
if (strlen(command) > 0)
|
||||
{
|
||||
strcpy(state.name, command);
|
||||
state.place = PLACE_STATION;
|
||||
strcpy(state.hero.name, command);
|
||||
state.hero.hp = 10;
|
||||
state.hero.gold = 0;
|
||||
state.hero.atk = 1;
|
||||
state.hero.def = 1;
|
||||
state.screen = SC_STATION;
|
||||
}
|
||||
}
|
||||
else if (state.place == PLACE_STATION)
|
||||
else if (state.screen == SC_STATION)
|
||||
{
|
||||
if (c == 'n')
|
||||
{
|
||||
state.place = PLACE_MAP;
|
||||
state.location.x = 1;
|
||||
state.location.y = 7;
|
||||
state.location.orientation = 'n';
|
||||
state.screen = SC_MAP;
|
||||
state.position.x = 1;
|
||||
state.position.y = 5;
|
||||
state.position.orientation = NORTH;
|
||||
updatepov();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void debugoutput()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
clearscreen();
|
||||
|
||||
|
||||
int screenwidth = 30;
|
||||
for (int i = 0; i < screenwidth; i++) printf("-");
|
||||
printf("\n");
|
||||
|
||||
if (state.place == PLACE_TITLE)
|
||||
|
||||
if (state.screen == SC_TITLE)
|
||||
{
|
||||
printf(" _ _\n");
|
||||
printf(" / / / /\n");
|
||||
|
@ -458,15 +496,16 @@ void draw()
|
|||
printf(" / /___space\n");
|
||||
printf(" /_____/\n");
|
||||
}
|
||||
else if (state.place == PLACE_SETNAME)
|
||||
else if (state.screen == SC_SETNAME)
|
||||
{
|
||||
printf("Bonjour.\n\n");
|
||||
printf("Bienvenue à la station\n");
|
||||
printf("spaciale intergalactique.\n\n");
|
||||
printf("Veuillez saisir votre nom.\n");
|
||||
}
|
||||
else if (state.place == PLACE_MAP)
|
||||
else if (state.screen == SC_MAP)
|
||||
{
|
||||
update3dpov();
|
||||
int shift = 9;
|
||||
for (int i = 0; i < shift; i++) printf(" ");
|
||||
printf("o----------o\n");
|
||||
|
@ -484,19 +523,18 @@ void draw()
|
|||
for (int i = 0; i < shift; i++) printf(" ");
|
||||
printf("o----------o");
|
||||
|
||||
if (charatpos(state.location.x, state.location.y) == 's')
|
||||
if (charatpos(state.position.x, state.position.y) == 's')
|
||||
{
|
||||
// dessiner un hublot au plafond (dans updatepov3d)
|
||||
printf("\n\nr: Retour à la station\n");
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
printf("\n ^\n");
|
||||
printf(" <w s x>\n");
|
||||
}
|
||||
//drawmap();
|
||||
}
|
||||
else if (state.place == PLACE_STATION)
|
||||
else if (state.screen == SC_STATION)
|
||||
{
|
||||
printf("\n");
|
||||
printf(" STATION SPACIALE\n");
|
||||
|
@ -506,19 +544,21 @@ void draw()
|
|||
printf("n: Prendre la navette\n");
|
||||
}
|
||||
|
||||
if (state.place == PLACE_STATION || state.place == PLACE_MAP)
|
||||
if (state.screen == SC_STATION || state.screen == SC_MAP)
|
||||
{
|
||||
printf("\n");
|
||||
for (int i = 0; i < screenwidth; i++) printf("-");
|
||||
printf("\n");
|
||||
printf("%s | PV:10 | CG:0 | PSI:0", state.name);
|
||||
printf("%s | P.V. : %d | C.G. : %d", state.hero.name, state.hero.hp, state.hero.gold);
|
||||
|
||||
}
|
||||
|
||||
debugoutput();
|
||||
|
||||
printf("\n");
|
||||
for (int i = 0; i < screenwidth; i++) printf("-");
|
||||
printf("\n> ");
|
||||
|
||||
|
||||
}
|
||||
|
||||
void drawtopdownpov()
|
||||
|
@ -554,7 +594,7 @@ int main()
|
|||
{
|
||||
init();
|
||||
|
||||
char command[CMD_LEN] = NULL_CMD;
|
||||
char command[CMD_LEN] = CMD_NULL;
|
||||
|
||||
while (command[0] != 'q')
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue