some map rationalization

This commit is contained in:
quenousimporte 2025-04-21 22:26:58 +02:00
parent af11c8bbc1
commit f129882f50
1 changed files with 69 additions and 157 deletions

226
w/w.c
View File

@ -8,7 +8,7 @@
#define NAME_LEN 64 #define NAME_LEN 64
#define POV_3D_SIZE 10 #define POV_3D_SIZE 10
#define POV_SIZE 3 #define POV_SIZE 3
#define INVENTORY_SIZE 2 #define INVENTORY_SIZE 1
#define MAP_SIZE 15 #define MAP_SIZE 15
#define CMD_NULL "-" #define CMD_NULL "-"
@ -37,16 +37,16 @@
#define WEST 2 #define WEST 2
#define SOUTH 3 #define SOUTH 3
#define TILE_WALL 'w' #define TILE_WALL 0
#define TILE_FREE ' ' #define TILE_FREE 1
#define TILE_DOOR 'd' #define TILE_DOOR 2
#define TILE_START 's' #define TILE_START 3
#define TILE_UNKNOWN '?' #define TILE_UNUSED 4
#define TILE_UNUSED '.' #define TILE_UNVISITED 5
#define TILE_UNVISITED 'x' #define TILE_VISITED 6
#define TILE_INIT 7
#define ITEM_POTION '0' #define ITEM_POTION '0'
#define ITEM_TRUC '1'
typedef struct { typedef struct {
char id; char id;
@ -114,12 +114,23 @@ int dices(int count)
return result; return result;
} }
char tileonmap(char tile)
{
switch (tile)
{
case TILE_FREE: return ' '; // free passageway
case TILE_DOOR: return '+'; // door
case TILE_START: return 's'; // start point
case TILE_UNVISITED: return ' '; // not yet visited
case TILE_WALL: return '#'; // wall
case TILE_UNUSED: return '#'; // "poles"
case TILE_VISITED: return ' '; // already visited
case TILE_INIT: return '.'; // not yet initialized by dfs
}
}
char charatpos(int x, int y) char charatpos(int x, int y)
{ {
if (x < 0 || y < 0 || x >= MAP_SIZE || y >= MAP_SIZE)
{
return ' ';
}
return map[y * MAP_SIZE + x]; return map[y * MAP_SIZE + x];
} }
@ -137,7 +148,7 @@ void drawmap()
for (int y = 0; y < MAP_SIZE; y++) { for (int y = 0; y < MAP_SIZE; y++) {
printf("%02d ", y); printf("%02d ", y);
for (int x = 0; x < MAP_SIZE; x++) { for (int x = 0; x < MAP_SIZE; x++) {
char toprint = charatpos(x,y); char toprint = tileonmap(charatpos(x,y));
if (y == state.position.y && x == state.position.x) if (y == state.position.y && x == state.position.x)
{ {
if (state.position.orientation == NORTH) toprint = '^'; if (state.position.orientation == NORTH) toprint = '^';
@ -309,7 +320,7 @@ void update3dpov()
} }
} }
const char doorchar = '#'; const char doorchar = '+';
if (pov[1][1] == TILE_DOOR) if (pov[1][1] == TILE_DOOR)
{ {
// front door // front door
@ -365,7 +376,10 @@ void updatepov()
newY -= rtoloffset; newY -= rtoloffset;
break; break;
} }
pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY); if (newX >= 0 && newY >= 0 && newX < MAP_SIZE && newY < MAP_SIZE)
{
pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY);
}
} }
} }
update3dpov(); update3dpov();
@ -390,6 +404,7 @@ void forward()
state.position.x -= 2; state.position.x -= 2;
break; break;
} }
setatpos(state.position.x, state.position.y, TILE_VISITED);
} }
} }
@ -494,9 +509,7 @@ void createhero(char* name)
state.hero.def = 1; state.hero.def = 1;
Item potion = { ITEM_POTION, "potion", 20, 0 }; Item potion = { ITEM_POTION, "potion", 20, 0 };
Item truc = { ITEM_TRUC, "truc", 2000, 0 };
state.inventory[0] = potion; state.inventory[0] = potion;
state.inventory[1] = truc;
} }
void createround(char action, int herodmg, int monsterdmg) void createround(char action, int herodmg, int monsterdmg)
@ -579,112 +592,11 @@ void drawrounds()
} }
} }
char gentile(int x, int y)
{
char c = charatpos(x, y);
if (c == TILE_UNKNOWN)
{
int d = dice();
if (d == 1)
{
c = TILE_DOOR;
}
else if (d < 5)
{
c = TILE_WALL;
}
else
{
c = TILE_FREE;
}
setatpos(x, y, c);
return c;
}
return TILE_UNUSED;
}
int ddl = 0;
void updatemap()
{
//ddl--;
int x = state.position.x;
int y = state.position.y;
// north
char c = gentile(x, y - 1);
if (c == TILE_FREE)
{
ddl++;
gentile(x - 1, y - 2);
gentile(x + 1, y - 2);
}
else if (c == TILE_DOOR)
{
ddl++;
}
else if (c != TILE_UNUSED)
{
ddl--;
}
// south
c = gentile(x, y + 1);
if (c == TILE_FREE)
{
ddl++;
gentile(x - 1, y + 2);
gentile(x + 1, y + 2);
}
else if (c == TILE_DOOR)
{
ddl++;
}
else if (c != TILE_UNUSED)
{
ddl--;
}
// east
c = gentile(x + 1, y);
if (c == TILE_FREE)
{
ddl++;
gentile(x + 2, y - 1);
gentile(x + 2, y + 1);
}
else if (c == TILE_DOOR)
{
ddl++;
}
else if (c != TILE_UNUSED)
{
ddl--;
}
// west
c = gentile(x - 1, y);
if (c == TILE_FREE)
{
ddl++;
gentile(x - 2, y - 1);
gentile(x - 2, y + 1);
}
else if (c == TILE_DOOR)
{
ddl++;
}
else if (c != TILE_UNUSED)
{
ddl--;
}
}
void dfs(int x, int y) void dfs(int x, int y)
{ {
if (charatpos(x,y) != TILE_START) if (charatpos(x,y) != TILE_START)
{ {
setatpos(x, y, TILE_FREE); setatpos(x, y, TILE_UNVISITED);
} }
bool nok = false; bool nok = false;
@ -699,7 +611,7 @@ void dfs(int x, int y)
{ {
case NORTH: case NORTH:
nok = true; nok = true;
if (charatpos(x, y - 2) == TILE_UNVISITED) if (charatpos(x, y - 2) == TILE_INIT)
{ {
setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x, y - 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x, y - 2); dfs(x, y - 2);
@ -707,7 +619,7 @@ void dfs(int x, int y)
break; break;
case SOUTH: case SOUTH:
sok = true; sok = true;
if (charatpos(x, y + 2) == TILE_UNVISITED) if (charatpos(x, y + 2) == TILE_INIT)
{ {
setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x, y + 1, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x, y + 2); dfs(x, y + 2);
@ -715,7 +627,7 @@ void dfs(int x, int y)
break; break;
case EAST: case EAST:
eok = true; eok = true;
if (charatpos(x - 2, y) == TILE_UNVISITED) if (charatpos(x - 2, y) == TILE_INIT)
{ {
setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x - 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x - 2, y); dfs(x - 2, y);
@ -723,7 +635,7 @@ void dfs(int x, int y)
break; break;
case WEST: case WEST:
wok = true; wok = true;
if (charatpos(x + 2, y) == TILE_UNVISITED) if (charatpos(x + 2, y) == TILE_INIT)
{ {
setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE); setatpos(x + 1, y, dice() == 6 ? TILE_DOOR : TILE_FREE);
dfs(x + 2, y); dfs(x + 2, y);
@ -735,8 +647,7 @@ void dfs(int x, int y)
void initmap() void initmap()
{ {
//memset(&map, TILE_UNKNOWN, MAP_SIZE * MAP_SIZE); memset(&map, TILE_INIT, MAP_SIZE * MAP_SIZE);
memset(&map, TILE_UNKNOWN, MAP_SIZE * MAP_SIZE); // dfs: put walls everywhere
for (int x = 0; x < MAP_SIZE; x ++) for (int x = 0; x < MAP_SIZE; x ++)
{ {
@ -749,16 +660,13 @@ void initmap()
} }
else if (x % 2 && y % 2) else if (x % 2 && y % 2)
{ {
// free tiles (or unvisited for dfs) // free tiles not yet initialized by dfs
setatpos(x, y, TILE_UNVISITED); setatpos(x, y, TILE_INIT);
} }
else if (!(x % 2 ) && !(y % 2)) else if (!(x % 2 ) && !(y % 2))
{ {
// unused tiles aka "poles" // unused tiles aka "poles"
//setatpos(x, y, TILE_UNUSED); setatpos(x, y, TILE_UNUSED);
//... or walls for readibility on map
setatpos(x, y, TILE_WALL);
} }
else else
{ {
@ -778,7 +686,7 @@ void initmap()
setatpos(1, 13, TILE_START); setatpos(1, 13, TILE_START);
// create map // create map
dfs(1,13); dfs(1, 13);
} }
void update(char* command) void update(char* command)
@ -795,7 +703,7 @@ void update(char* command)
forward(); forward();
if ( (x != state.position.x || y != state.position.y) && dice() > 4) if ( (x != state.position.x || y != state.position.y) && dice() > 4)
{ {
gotomonster(); //gotomonster();
} }
break; break;
case CMD_RIGHT: case CMD_RIGHT:
@ -934,6 +842,29 @@ void update(char* command)
} }
} }
void drawtopdownpov()
{
for (int y = 0; y < POV_SIZE; y++)
{
for (int x = 0; x < POV_SIZE; x++)
{
if (y == 0 && pov[1][1] != TILE_FREE)
{
printf(" ");
}
else if (y == 2 && x == 1)
{
printf("^");
}
else
{
printf("%c", tileonmap(pov[y][x]));
}
}
printf("\n");
}
}
void draw() void draw()
{ {
clearscreen(); clearscreen();
@ -971,7 +902,11 @@ void draw()
} }
else if (state.screen == SC_MAP) else if (state.screen == SC_MAP)
{ {
drawpov3d(); drawpov3d();
//printf("\n");
//drawtopdownpov();
//drawmap();
// todo: would need a compass? // todo: would need a compass?
/*printf("\n\n"); /*printf("\n\n");
@ -1067,29 +1002,6 @@ void draw()
} }
void drawtopdownpov()
{
for (int y = 0; y < POV_SIZE; y++)
{
for (int x = 0; x < POV_SIZE; x++)
{
if (y == 0 && pov[1][1] != ' ')
{
printf(" ");
}
else if (y == 2 && x == 1)
{
printf("^");
}
else
{
printf("%c", pov[y][x]);
}
}
printf("\n");
}
}
void getcommand(char* command) void getcommand(char* command)
{ {
fgets(command, sizeof(char) * CMD_LEN, stdin); fgets(command, sizeof(char) * CMD_LEN, stdin);