persistent map

This commit is contained in:
quenousimporte 2025-04-23 12:21:43 +01:00
parent 02760ec80b
commit becd1b6f60
1 changed files with 101 additions and 106 deletions

207
w/w.c
View File

@ -80,6 +80,7 @@ typedef struct {
Position position;
Item inventory[INVENTORY_SIZE];
char screen;
char map[MAP_SIZE * MAP_SIZE];
} Gamestate;
const char* statepath = "w.state";
@ -91,8 +92,6 @@ Character currentmonster;
Round* firstround = NULL;
Round* currentround = NULL;
char map[MAP_SIZE * MAP_SIZE];
Character monsters[2] = {
{ "alien bleu", 2, 2, 1, 1 },
{ "vilaine araignée", 5, 8, 2, 1 }
@ -130,7 +129,7 @@ char tileonmap(char tile)
char charatpos(int x, int y)
{
return map[y * MAP_SIZE + x];
return state.map[y * MAP_SIZE + x];
}
void setatpos(int x, int y, char c)
@ -139,7 +138,7 @@ void setatpos(int x, int y, char c)
{
return;
}
map[y * MAP_SIZE + x] = c;
state.map[y * MAP_SIZE + x] = c;
}
void drawmap()
@ -434,10 +433,106 @@ void clearscreen()
#endif
}
void dfs(int x, int y)
{
if (charatpos(x,y) != TILE_START)
{
setatpos(x, y, TILE_ROOM);
}
bool nok = false;
bool eok = false;
bool sok = false;
bool wok = false;
while (! (nok && eok && sok && wok))
{
char orientation = rand() % 4;
switch (orientation)
{
case NORTH:
nok = true;
if (charatpos(x, y - 2) == TILE_INIT)
{
setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x, y - 2);
}
break;
case SOUTH:
sok = true;
if (charatpos(x, y + 2) == TILE_INIT)
{
setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x, y + 2);
}
break;
case EAST:
eok = true;
if (charatpos(x - 2, y) == TILE_INIT)
{
setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x - 2, y);
}
break;
case WEST:
wok = true;
if (charatpos(x + 2, y) == TILE_INIT)
{
setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x + 2, y);
}
break;
}
}
}
void initmap()
{
memset(&state.map, TILE_INIT, MAP_SIZE * MAP_SIZE);
for (int x = 0; x < MAP_SIZE; x ++)
{
for (int y = 0; y < MAP_SIZE; y++)
{
if (x == 0 || y == 0 || x == MAP_SIZE - 1 || y == MAP_SIZE - 1)
{
// boundaries
setatpos(x, y, TILE_WALL);
}
else if (x % 2 && y % 2)
{
// free tiles not yet initialized by dfs
setatpos(x, y, TILE_INIT);
}
else if (!(x % 2 ) && !(y % 2))
{
// unused tiles aka "poles"
setatpos(x, y, TILE_POLE);
}
else
{
// init all intersections with walls
setatpos(x, y, TILE_WALL);
//... and put a few free tiles here and there to create bigger rooms
if (dice() == 1)
{
setatpos(x, y, TILE_FREE);
}
}
}
}
// starting point
setatpos(1, 13, TILE_START);
// create map
dfs(1, 13);
}
void init()
{
printf("Démarrage...\n");
map[0] = 0;
srand(time(NULL));
FILE* file = fopen(statepath, "r");
if (file)
@ -449,6 +544,7 @@ void init()
else
{
state.screen = SC_TITLE;
initmap();
}
printf("Début de la boucle principale...\n");
}
@ -562,103 +658,6 @@ void drawrounds()
}
}
void dfs(int x, int y)
{
if (charatpos(x,y) != TILE_START)
{
setatpos(x, y, TILE_ROOM);
}
bool nok = false;
bool eok = false;
bool sok = false;
bool wok = false;
while (! (nok && eok && sok && wok))
{
char orientation = rand() % 4;
switch (orientation)
{
case NORTH:
nok = true;
if (charatpos(x, y - 2) == TILE_INIT)
{
setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x, y - 2);
}
break;
case SOUTH:
sok = true;
if (charatpos(x, y + 2) == TILE_INIT)
{
setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x, y + 2);
}
break;
case EAST:
eok = true;
if (charatpos(x - 2, y) == TILE_INIT)
{
setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x - 2, y);
}
break;
case WEST:
wok = true;
if (charatpos(x + 2, y) == TILE_INIT)
{
setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x + 2, y);
}
break;
}
}
}
void initmap()
{
memset(&map, TILE_INIT, MAP_SIZE * MAP_SIZE);
for (int x = 0; x < MAP_SIZE; x ++)
{
for (int y = 0; y < MAP_SIZE; y++)
{
if (x == 0 || y == 0 || x == MAP_SIZE - 1 || y == MAP_SIZE - 1)
{
// boundaries
setatpos(x, y, TILE_WALL);
}
else if (x % 2 && y % 2)
{
// free tiles not yet initialized by dfs
setatpos(x, y, TILE_INIT);
}
else if (!(x % 2 ) && !(y % 2))
{
// unused tiles aka "poles"
setatpos(x, y, TILE_POLE);
}
else
{
// init all intersections with walls
setatpos(x, y, TILE_WALL);
//... and put a few free tiles here and there to create bigger rooms
if (dice() == 1)
{
setatpos(x, y, TILE_FREE);
}
}
}
}
// starting point
setatpos(1, 13, TILE_START);
// create map
dfs(1, 13);
}
void update(char* command)
{
char c = command[0];
@ -720,10 +719,6 @@ void update(char* command)
state.position.y = 13;
state.position.orientation = NORTH;
if (map[0] == 0)
{
initmap();
}
updatepov();
}
else if (c == CMD_GOTOMARKET)