- char * f_name = "obey_msg()";
- if ( apply_player_command(msg, "wait") /* TODO: Check for non-error */
- || apply_player_command(msg, "move") /* return value of a modified */
- || apply_player_command(msg, "pick_up")/* get_moa_id_by_name(); if id */
- || apply_player_command(msg, "drop") /* found, execute on it what's */
- || apply_player_command(msg, "use")); /* in apply_player_command(). */
- else
+ 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)
+{
+ uint16_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))
+ { /* TODO: check meaning and refactorability of 2nd condition */
+ if (!thing)
+ {
+ world.turn++;
+ thing = world.things;
+ free(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)
+ {
+ if (0 == thing->command)
+ {
+ 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;
+ }
+ }
+ try_thing_proliferation(thing);
+ }
+ thing = thing->next;
+ }
+ free(whitelist);
+}
+
+
+
+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))
+ {
+ free(msg);
+ return 2;
+ }
+ if (!strcmp("PING", msg))
+ {
+ free(msg);
+ answer_query("PONG\n");
+ return 1;
+ }
+ if (!strcmp("STACK", msg))