X-Git-Url: https://plomlompom.com/repos/index.html?a=blobdiff_plain;f=src%2Froguelike.c;h=04da8a654799397ccdf031d499136ba21db36340;hb=867fe8e2358c8d71919b144f97e4ab272b8f4ebe;hp=0d1e96b60966338f4b5e7bba283cd4e64d49334d;hpb=d7c46fb1de25edd8782fbc7642be71e5e54deab2;p=plomrogue
diff --git a/src/roguelike.c b/src/roguelike.c
index 0d1e96b..04da8a6 100644
--- a/src/roguelike.c
+++ b/src/roguelike.c
@@ -20,14 +20,25 @@ uint16_t rrand(char use_seed, uint32_t new_seed) {
return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
void update_log (struct World * world, char * text) {
-// Update log with new text to be appended.
+// Update log by appending text, or by appending a "." if text is the same as the last one.
+ static char * last_msg;
+ if (0 == last_msg)
+ last_msg = calloc(1, sizeof(char));
char * new_text;
uint16_t len_old = strlen(world->log);
- uint16_t len_new = strlen(text);
- uint16_t len_whole = len_old + len_new + 1;
- new_text = calloc(len_whole, sizeof(char));
- memcpy(new_text, world->log, len_old);
- memcpy(new_text + len_old, text, len_new);
+ if (0 == strcmp(last_msg, text)) {
+ uint16_t len_whole = len_old + 1;
+ new_text = calloc(len_whole + 1, sizeof(char));
+ memcpy(new_text, world->log, len_old);
+ memcpy(new_text + len_old, ".", 1); }
+ else {
+ uint16_t len_new = strlen(text);
+ uint16_t len_whole = len_old + len_new + 1;
+ new_text = calloc(len_whole, sizeof(char));
+ memcpy(new_text, world->log, len_old);
+ memcpy(new_text + len_old, text, len_new);
+ last_msg = calloc(len_new + 1, sizeof(char));
+ memcpy(last_msg, text, len_new); }
free(world->log);
world->log = new_text; }
@@ -85,12 +96,14 @@ void save_game(struct World * world) {
write_uint32_bigendian(world->turn, file);
write_uint16_bigendian(world->player->pos.y, file);
write_uint16_bigendian(world->player->pos.x, file);
- write_uint16_bigendian(world->monster->pos.y, file);
- write_uint16_bigendian(world->monster->pos.x, file);
- write_uint16_bigendian(world->monster->next->pos.y, file);
- write_uint16_bigendian(world->monster->next->pos.x, file);
- write_uint16_bigendian(world->monster->next->next->pos.y, file);
- write_uint16_bigendian(world->monster->next->next->pos.x, file);
+ struct Monster * monster;
+ for (monster = world->monster; monster != 0; monster = monster->next) {
+ write_uint16_bigendian(monster->pos.y, file);
+ write_uint16_bigendian(monster->pos.x, file); }
+ struct Item * item;
+ for (item = world->item; item != 0; item = item->next) {
+ write_uint16_bigendian(item->pos.y, file);
+ write_uint16_bigendian(item->pos.x, file); }
fclose(file); }
void toggle_window (struct WinMeta * win_meta, struct Win * win) {
@@ -186,21 +199,28 @@ int main (int argc, char *argv[]) {
default:
exit(EXIT_FAILURE); } }
- // Initialize log, player and monsters.
+ // Initialize log, player, monsters and items.
world.log = calloc(1, sizeof(char));
update_log (&world, " ");
struct Player player;
world.player = &player;
- struct Monster monster1;
- struct Monster monster2;
- struct Monster monster3;
- world.monster = &monster1;
- monster1.next = &monster2;
- monster2.next = &monster3;
- monster3.next = 0;
- monster1.name = 'A';
- monster2.name = 'B';
- monster3.name = 'C';
+ char i;
+ struct Monster * monster = malloc(sizeof(struct Monster));
+ world.monster = monster;
+ for (i = 0; i < 2; i++) {
+ monster->name = 'M';
+ monster->next = malloc(sizeof(struct Monster));
+ monster = monster->next; }
+ monster->name = 'M';
+ monster->next = 0;
+ struct Item * item = malloc(sizeof(struct Item));
+ world.item = item;
+ for (i = 0; i < 2; i++) {
+ item->name = '#';
+ item->next = malloc(sizeof(struct Item));
+ item = item->next; }
+ item->name = '#';
+ item->next = 0;
// For interactive mode, try to load world state from savefile.
FILE * file;
@@ -210,12 +230,12 @@ int main (int argc, char *argv[]) {
world.turn = read_uint32_bigendian(file);
player.pos.y = read_uint16_bigendian(file);
player.pos.x = read_uint16_bigendian(file);
- monster1.pos.y = read_uint16_bigendian(file);
- monster1.pos.x = read_uint16_bigendian(file);
- monster2.pos.y = read_uint16_bigendian(file);
- monster2.pos.x = read_uint16_bigendian(file);
- monster3.pos.y = read_uint16_bigendian(file);
- monster3.pos.x = read_uint16_bigendian(file);
+ for (monster = world.monster; monster != 0; monster = monster->next) {
+ monster->pos.y = read_uint16_bigendian(file);
+ monster->pos.x = read_uint16_bigendian(file); }
+ for (item = world.item; item != 0; item = item->next) {
+ item->pos.y = read_uint16_bigendian(file);
+ item->pos.x = read_uint16_bigendian(file); }
fclose(file); }
// For non-interactive mode, try to load world state from frecord file.
@@ -237,12 +257,15 @@ int main (int argc, char *argv[]) {
struct Map map = init_map();
world.map = ↦
if (1 == world.turn) {
- for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos.y, player.pos.x);) {
+ for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos);) {
player.pos.y = rrand(0, 0) % map.size.y;
player.pos.x = rrand(0, 0) % map.size.x; }
- struct Monster * monster;
+ for (item = world.item; item != 0; item = item->next)
+ for (item->pos.y = item->pos.x = 0; 0 == is_passable(&map, item->pos);) {
+ item->pos.y = rrand(0, 0) % map.size.y;
+ item->pos.x = rrand(0, 0) % map.size.x; }
for (monster = world.monster; monster != 0; monster = monster->next)
- for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos.y, monster->pos.x);) {
+ for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos);) {
monster->pos.y = rrand(0, 0) % map.size.y;
monster->pos.x = rrand(0, 0) % map.size.x; } }