X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Frun.c;h=ba48d8ce3690392dacd39bcb4de5ee9bbd1888a7;hb=c560e7b4c403cf75b5a7ebd7cc4818ade1ae845d;hp=808be55398a3a03e7f1d69d67c104d3850ebaffd;hpb=a9377a5125c28779f812f859564d4f4d0b744e89;p=plomrogue diff --git a/src/server/run.c b/src/server/run.c index 808be55..ba48d8c 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -71,9 +71,6 @@ static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist); */ static void turn_over(); -/* Append "answer" to server output file, with instant fflush(). */ -static void answer_query(char * answer); - /* Try to read "msg" as meta command, act accordingly; on success, free it. */ static uint8_t meta_commands(char * msg); @@ -227,7 +224,7 @@ static int16_t * build_whitelist() 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] = t->id, t = t->next, i_things++); whitelist[i_things] = -1; return whitelist; } @@ -236,7 +233,7 @@ static int16_t * build_whitelist() static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist) { - int16_t i; + uint16_t i; for (i = 0; -1 < whitelist[i]; i++) { if ((int16_t) id == whitelist[i]) @@ -253,18 +250,16 @@ 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)) - { /* TODO: check meaning and refactorability of 2nd condition */ + while (0 < player->lifepoints) + { if (!thing) { world.turn++; thing = world.things; free(whitelist); - whitelist = build_whitelist(); - } + whitelist = build_whitelist();/* The whitelist excludes things */ + } /* that appear only during the turn.*/ if (thing_in_whitelist(thing->id, whitelist)) { if (0 < thing->lifepoints) @@ -295,14 +290,6 @@ static void turn_over() -static void answer_query(char * answer) -{ - try_fwrite(answer, strlen(answer), 1, world.file_out, __func__); - fflush(world.file_out); -} - - - static uint8_t meta_commands(char * msg) { if (!strcmp("QUIT", msg)) @@ -313,13 +300,13 @@ static uint8_t meta_commands(char * msg) if (!strcmp("PING", msg)) { free(msg); - answer_query("PONG\n"); + send_to_outfile("PONG\n"); return 1; } if (!strcmp("STACK", msg)) { free(msg); - answer_query("THINGS_BELOW_PLAYER START\n"); + send_to_outfile("THINGS_BELOW_PLAYER START\n"); struct Thing * player = get_player(); struct Thing * t; for (t = world.things; t; t = t->next) @@ -328,11 +315,11 @@ static uint8_t meta_commands(char * msg) && t != player) { struct ThingType * tt = get_thing_type(t->type); - answer_query(tt->name); - answer_query("\n"); + send_to_outfile(tt->name); + send_to_outfile("\n"); } } - answer_query("THINGS_BELOW_PLAYER END\n"); + send_to_outfile("THINGS_BELOW_PLAYER END\n"); return 1; } return 0; @@ -340,6 +327,14 @@ static uint8_t meta_commands(char * msg) +extern void send_to_outfile(char * answer) +{ + try_fwrite(answer, strlen(answer), 1, world.file_out, __func__); + fflush(world.file_out); +} + + + extern void record(char * msg, uint8_t force) { static FILE * file_tmp = NULL;