X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Frun.c;h=334fe410fcf9200713725ee2f8adcc81b22f2c53;hb=08787351493beb2ad649e94d24eebca0e97192c8;hp=906bed266ae54f76edf06d5659a1f87d0fc24ce4;hpb=3dedf6344c941891491773d1cc5d647aa664b218;p=plomrogue diff --git a/src/server/run.c b/src/server/run.c index 906bed2..334fe41 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -1,9 +1,14 @@ -/* src/server/run.c */ +/* src/server/run.c + * + * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3 + * or any later version. For details on its copyright, license, and warranties, + * see the file NOTICE in the root directory of the PlomRogue source package. + */ #define _POSIX_C_SOURCE 200809L #include "run.h" #include /* NULL */ -#include /* uint8_t, uint16_t, uint32_t */ +#include /* uint8_t, uint16_t, uint32_t, int16_t */ #include /* FILE, printf(), fflush() */ #include /* free() */ #include /* strlen(), strcmp(), strncmp(), strdup() */ @@ -23,7 +28,9 @@ #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */ #include "hardcoded_strings.h" /* s */ #include "io.h" /* io_round(), save_world() */ -#include "things.h" /* Thing, get_thing_action_id_by_name(), get_player() */ +#include "things.h" /* Thing, get_thing_action_id_by_name(), get_player(), + * try_thing_proliferation() + */ #include "world.h" /* world */ @@ -51,8 +58,16 @@ static uint8_t parse_command(char * tok0); */ static void server_test(); +/* Return array of IDs of non-owned things in game world, ended by non-ID -1. */ +static int16_t * build_whitelist(); + +/* Return 1 if value of "id" appears in "whitelist", else 0. */ +static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist); + /* Run the game world and its inhabitants (and their actions) until the player - * avatar is free to receive new commands (or is dead). + * avatar is free to receive new commands (or is dead). Only actions and + * proliferations for non-owned things are performed that exist at the start of + * the turn jumped into, or started anew by the cycle. */ static void turn_over(); @@ -75,8 +90,7 @@ static uint8_t player_commands_allowed() { if (!world.exists) { - err_line(1, "No world exists in which to run player commands."); - return 0; + return !err_line(1, "No world exists in which to run player commands."); } return 1; } @@ -200,40 +214,77 @@ static void server_test() +static int16_t * build_whitelist() +{ + uint16_t i_things = NULL != world.things; + struct Thing * t = world.things; + for (; t; t = t->next, i_things++); + int16_t * whitelist = try_malloc(i_things * sizeof(int16_t), __func__); + for (i_things = 0, t = world.things; t; + whitelist[i_things] = t->id, t = t->next, i_things++) + whitelist[i_things] = -1; + return whitelist; +} + + + +static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist) +{ + int16_t i; + for (i = 0; -1 < whitelist[i]; i++) + { + if ((int16_t) id == whitelist[i]) + { + return 1; + } + } + return 0; +} + + + static void turn_over() { struct Thing * player = get_player(); struct Thing * thing = player; uint16_t start_turn = world.turn; + int16_t * whitelist = build_whitelist(); while ( 0 < player->lifepoints || (0 == player->lifepoints && start_turn == world.turn)) - { - if (NULL == thing) + { /* TODO: check meaning and refactorability of 2nd condition */ + if (!thing) { world.turn++; thing = world.things; + free(whitelist); + whitelist = build_whitelist(); } - if (0 < thing->lifepoints) + if (thing_in_whitelist(thing->id, whitelist)) { - if (0 == thing->command) + if (0 < thing->lifepoints) { - if (thing == player) + if (0 == thing->command) { - break; + if (thing == player) + { + break; + } + ai(thing); + } + thing->progress++; + struct ThingAction * ta = get_thing_action(thing->command); + if (thing->progress == ta->effort) + { + ta->func(thing); + thing->command = 0; + thing->progress = 0; } - ai(thing); - } - thing->progress++; - struct ThingAction * ta = get_thing_action(thing->command); - if (thing->progress == ta->effort) - { - ta->func(thing); - thing->command = 0; - thing->progress = 0; } + try_thing_proliferation(thing); } thing = thing->next; } + free(whitelist); } @@ -285,7 +336,7 @@ extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose) set_err_line_options("Trouble with message: ", msg, 0); char * msg_copy = strdup(msg); char * tok0 = token_from_line(msg_copy); - if (NULL != tok0) + if (tok0) { if (parse_command(tok0)) @@ -299,7 +350,7 @@ extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose) record(msg, 0); } char * tokplus = token_from_line(NULL); - err_line(NULL != tokplus, "Too many arguments, ignoring overflow."); + err_line(!(!tokplus), "Too many arguments, ignoring overflow."); free(msg_copy); return; } @@ -316,7 +367,7 @@ extern uint8_t io_loop() { char * msg = io_round(); server_test(); - if (NULL == msg) + if (!msg) { continue; }