Further generation updates...

This commit is contained in:
Ognjen Milan Robovic 2024-06-30 19:36:25 -04:00
parent 6b7f3246a6
commit e8cf8033e3

View File

@ -2,7 +2,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <termios.h> #include <termios.h>
#include <time.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
@ -13,6 +12,8 @@
#define members ( 6) #define members ( 6)
#define items ( 36) #define items ( 36)
#define random(from, to) (rand () % (to - from + 1) + from)
enum { normal = '0', bold = '1', italic = '2', blink = '5', reverse = '7' }; enum { normal = '0', bold = '1', italic = '2', blink = '5', reverse = '7' };
enum { grey = '0', red = '1', green = '2', yellow = '3', blue = '4', pink = '5', cyan = '6', white = '7' }; enum { grey = '0', red = '1', green = '2', yellow = '3', blue = '4', pink = '5', cyan = '6', white = '7' };
@ -88,7 +89,7 @@ static int level_width;
static int level_height; static int level_height;
static char * level; static char * level;
static void screen_add (char style, char colour, char symbol, int x, int y) { static void screen_put (char style, char colour, char symbol, int x, int y) {
char format [] = "\033[ ;3 m \033[0m"; char format [] = "\033[ ;3 m \033[0m";
format [2] = style; format [2] = style;
@ -98,6 +99,14 @@ static void screen_add (char style, char colour, char symbol, int x, int y) {
strncpy (& screen [(y * screen_width + x) * 12 + 3], format, sizeof (format) - 1); strncpy (& screen [(y * screen_width + x) * 12 + 3], format, sizeof (format) - 1);
} }
static void screen_print (char style, char colour, char * text, int x, int y, int limit) {
int i;
for (i = 0; (text [i] != '\0') && (i < limit); ++i) {
screen_put (style, colour, text [i], x + i, y);
}
}
static void initialize_screen (void) { static void initialize_screen (void) {
struct winsize screen_dimension; struct winsize screen_dimension;
@ -184,13 +193,13 @@ static void generate_party (void) {
} }
for (i = 0; i < members; ++i) { for (i = 0; i < members; ++i) {
player.weapon [i] = rand () % 3; player.weapon [i] = random (0, 3);
player.armour [i] = rand () % 3; player.armour [i] = random (0, 3);
for (j = 0; j < points; ++j) { for (j = 0; j < points; ++j) {
player.point [i] [j] = rand () % 12 + 12; player.point [i] [j] = random (12, 24);
} }
for (j = 0; j < attributes; ++j) { for (j = 0; j < attributes; ++j) {
player.attribute [i] [j] = rand () % 6 + 1; player.attribute [i] [j] = random (1, 4);
} }
player.skill [i] = rand () % skills; player.skill [i] = rand () % skills;
player.class [i] = rand () % classes; player.class [i] = rand () % classes;
@ -200,16 +209,16 @@ static void generate_party (void) {
static void generate_level (void) { static void generate_level (void) {
int room, i, j, x, y, width, height; int room, i, j, x, y, width, height;
level_width = rand () % level_width_limit / 2 + level_width_limit / 2; level_width = random (level_width_limit / 2, level_width_limit);
level_height = rand () % level_height_limit / 2 + level_height_limit / 2; level_height = random (level_height_limit / 2, level_height_limit);
level = calloc ((unsigned long int) (level_width * level_height), sizeof (* level)); level = calloc ((unsigned long int) (level_width * level_height), sizeof (* level));
for (room = rand () % 12 + 6; room > 1; --room) { for (room = random (6, 18); room > 1; --room) {
width = rand () % 24 + 12; width = random (12, 36);
height = rand () % 24 + 12; height = random (12, 36);
x = rand () % (level_width - width); x = random (1, level_width - width - 1);
y = rand () % (level_height - height); y = random (1, level_height - height - 1);
for (i = 1; i < height - 1; ++i) { for (i = 1; i < height - 1; ++i) {
for (j = 1; j < width - 1; ++j) { for (j = 1; j < width - 1; ++j) {
level [(i + y) * level_width + (j + x)] = 1; level [(i + y) * level_width + (j + x)] = 1;
@ -224,14 +233,23 @@ static void generate_level (void) {
level [(y + height - 1) * level_width + j + x] = 2; level [(y + height - 1) * level_width + j + x] = 2;
} }
} }
for (i = 1; i < level_height - 1; ++i) {
for (j = 1; j < level_width - 1; ++j) {
if (((level [i * level_width + (j - 1)] == 1) && (level [i * level_width + j] == 2) && (level [i * level_width + (j + 1)] == 1)) ||
((level [(i - 1) * level_width + j] == 1) && (level [i * level_width + j] == 2) && (level [(i + 1) * level_width + j] == 1))) {
level [i * level_width + j] = 1;
}
}
}
} }
int main (void) { int main (void) {
int i, x, y; int i, x, y;
initialize_screen (); srand (time (0));
srand ((unsigned long) time (NULL)); initialize_screen ();
generate_party (); generate_party ();
generate_level (); generate_level ();
@ -239,18 +257,20 @@ int main (void) {
while (signal != 'Q') { while (signal != 'Q') {
for (y = 0; y < screen_height; ++y) { for (y = 0; y < screen_height; ++y) {
for (x = 0; x < screen_width; ++x) { for (x = 0; x < screen_width; ++x) {
screen_add (tiles [0].style, tiles [0].colour, tiles [0].symbol, x, y); screen_put (tiles [0].style, tiles [0].colour, tiles [0].symbol, x, y);
} }
} }
for (y = 0; (y < level_height) && (y < screen_height); ++y) { for (y = 0; (y < level_height) && (y < screen_height); ++y) {
for (x = 0; (x < level_width) && (x < screen_width); ++x) { for (x = 0; (x < level_width) && (x < screen_width); ++x) {
i = (int) level [y * level_width + x]; i = (int) level [y * level_width + x];
screen_add (tiles [i].style, tiles [i].colour, tiles [i].symbol, x, y); screen_put (tiles [i].style, tiles [i].colour, tiles [i].symbol, x, y);
} }
} }
screen_add (bold, cyan, '@', player.x, player.y); screen_put (bold, cyan, '@', player.x, player.y);
screen_print (bold, grey, "Warrior", screen_width - 24, 0, 24);
synchronize_screen (); synchronize_screen ();
} }
@ -259,7 +279,5 @@ int main (void) {
deinitialize_screen (); deinitialize_screen ();
printf ("L: %i x %i\n", level_width, level_height);
return (0); return (0);
} }