- d = rrand(0, 0) % 5;
- ty = monster->y;
- tx = monster->x;
- if (1 == d)
- ty++;
- else if (2 == d)
- ty--;
- else if (3 == d)
- tx++;
- else if (4 == d)
- tx--;
- if (tx == world->player->x && ty == world->player->y)
- update_log(world, "\nThe monster hits you.");
- else if (is_passable(world->map, ty, tx)) {
- monster->y = ty;
- monster->x = tx; } } }
-
-void update_log (struct World * world, char * text) {
-// Update log with new text to be appended.
- 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);
- free(world->log);
- world->log = new_text; }
-
-void move_player (struct World * world, char d) {
-// Move player in direction d, increment turn counter and update log.
- static char prev = 0;
- char success = 0;
- char * dir;
- uint16_t ty = world->player->y;
- uint16_t tx = world->player->x;
- if ('s' == d) {
- dir = "south";
- ty++; }
- if ('n' == d) {
- dir = "north";
- ty--; }
- if ('w' == d) {
- dir = "west";
- tx--; }
- if ('e' == d) {
- dir = "east";
- tx++; }
- struct Monster * monster;
- for (monster = world->monster; monster != 0; monster = monster->next)
- if (ty == monster->y && tx == monster->x) {
- success = 2;
- break; }
- if (2 != success && is_passable(world->map, ty, tx)) {
- success = 1;
- world->player->y = ty;
- world->player->x = tx; }
- if (success * d == prev)
- update_log (world, ".");
- else {
- if (2 == success)
- update_log (world, "\nYou hit the monster.");
- else {
- char * msg = calloc(25, sizeof(char));
- char * msg_content = "You fail to move";
- if (1 == success)
- msg_content = "You move";
- sprintf(msg, "\n%s %s.", msg_content, dir);
- update_log (world, msg);
- free(msg); } }
- prev = success * d;
- if (1 == world->interactive)
- record_action(d);
- next_turn (world); }
-
-char is_passable (struct Map * map, uint16_t y, uint16_t x) {
-// Check if coordinate on (or beyond) map is accessible to movement.
- char passable = 0;
- if (0 <= x && x < map->width && 0 <= y && y < map->height)
- if ('.' == map->cells[y * map->width + x])
- passable = 1;
- return passable; }
-
-void player_wait (struct World * world) {
-// Make player wait one turn.
- if (1 == world->interactive)
- record_action(0);
- next_turn (world);
- update_log (world, "\nYou wait."); }
+ write_uint16_bigendian(monster->pos.y + 1, file);
+ write_uint16_bigendian(monster->pos.x + 1, file);
+ fputc(monster->name, file); }
+ write_uint16_bigendian(0, file);
+ struct Item * item;
+ for (item = world->item; item != 0; item = item->next) {
+ write_uint16_bigendian(item->pos.y + 1, file);
+ write_uint16_bigendian(item->pos.x + 1, file);
+ fputc(item->name, file); }
+ write_uint16_bigendian(0, file);
+ fclose(file); }