LCOV - code coverage report
Current view: top level - open-adventure - misc.c (source / functions) Hit Total Coverage
Test: advent.info Lines: 349 349 100.0 %
Date: 2018-03-12 03:52:32 Functions: 30 30 100.0 %

          Line data    Source code
       1             : /*
       2             :  * I/O and support riutines.
       3             :  *
       4             :  * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
       5             :  * Copyright (c) 2017 by Eric S. Raymond
       6             :  * SPDX-License-Identifier: BSD-2-clause
       7             :  */
       8             :      
       9             : #include <unistd.h>
      10             : #include <stdlib.h>
      11             : #include <stdio.h>
      12             : #include <string.h>
      13             : #include <stdarg.h>
      14             : #include <sys/time.h>
      15             : #include <ctype.h>
      16             : #include <editline/readline.h>
      17             : #include <inttypes.h>
      18             : 
      19             : #include "advent.h"
      20             : #include "dungeon.h"
      21             : 
      22       54312 : static void* xcalloc(size_t size)
      23             : {
      24       54312 :     void* ptr = calloc(size, 1);
      25       54312 :     if (ptr == NULL) {
      26             :         // LCOV_EXCL_START
      27             :         // exclude from coverage analysis because we can't simulate an out of memory error in testing
      28             :         fprintf(stderr, "Out of memory!\n");
      29             :         exit(EXIT_FAILURE);
      30             :         // LCOV_EXCL_STOP
      31             :     }
      32       54312 :     return (ptr);
      33             : }
      34             : 
      35             : /*  I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */
      36             : 
      37       36843 : static void vspeak(const char* msg, bool blank, va_list ap)
      38             : {
      39             :     // Do nothing if we got a null pointer.
      40       36843 :     if (msg == NULL)
      41         325 :         return;
      42             : 
      43             :     // Do nothing if we got an empty string.
      44       36518 :     if (strlen(msg) == 0)
      45         522 :         return;
      46             : 
      47       35996 :     if (blank == true)
      48       35541 :         printf("\n");
      49             : 
      50       35996 :     int msglen = strlen(msg);
      51             : 
      52             :     // Rendered string
      53       35996 :     ssize_t size = 2000; /* msglen > 50 ? msglen*2 : 100; */
      54       35996 :     char* rendered = xcalloc(size);
      55       35996 :     char* renderp = rendered;
      56             : 
      57             :     // Handle format specifiers (including the custom %S) by
      58             :     // adjusting the parameter accordingly, and replacing the
      59             :     // specifier with %s.
      60       35996 :     bool pluralize = false;
      61     1787383 :     for (int i = 0; i < msglen; i++) {
      62     1751387 :         if (msg[i] != '%') {
      63             :             /* Ugh.  Least obtrusive way to deal with artifacts "on the floor"
      64             :              * being dropped outside of both cave and building. */
      65     1750650 :             if (strncmp(msg + i, "floor", 5) == 0 && strchr(" .", msg[i + 5]) && !INSIDE(game.loc)) {
      66           2 :                 strcpy(renderp, "ground");
      67           2 :                 renderp += 6;
      68           2 :                 i += 4;
      69           2 :                 size -= 5;
      70             :             } else {
      71     1750648 :                 *renderp++ = msg[i];
      72     1750648 :                 size--;
      73             :             }
      74             :         } else {
      75         737 :             i++;
      76             :             // Integer specifier.
      77         737 :             if (msg[i] == 'd') {
      78         512 :                 int32_t arg = va_arg(ap, int32_t);
      79         512 :                 int ret = snprintf(renderp, size, "%" PRId32, arg);
      80         512 :                 if (ret < size) {
      81         512 :                     renderp += ret;
      82         512 :                     size -= ret;
      83             :                 }
      84         512 :                 pluralize = (arg != 1);
      85             :             }
      86             : 
      87             :             // Unmodified string specifier.
      88         737 :             if (msg[i] == 's') {
      89          34 :                 char *arg = va_arg(ap, char *);
      90          34 :                 strncat(renderp, arg, size - 1);
      91          34 :                 size_t len = strlen(renderp);
      92          34 :                 renderp += len;
      93          34 :                 size -= len;
      94             :             }
      95             : 
      96             :             // Singular/plural specifier.
      97         737 :             if (msg[i] == 'S') {
      98             :                 // look at the *previous* numeric parameter
      99         191 :                 if (pluralize) {
     100         187 :                     *renderp++ = 's';
     101         187 :                     size--;
     102             :                 }
     103             :             }
     104             : 
     105             :             // LCOV_EXCL_START - doesn't occur in test suite.
     106             :             /* Version specifier */
     107             :             if (msg[i] == 'V') {
     108             :                 strcpy(renderp, VERSION);
     109             :                 size_t len = strlen(VERSION);
     110             :                 renderp += len;
     111             :                 size -= len;
     112             :             }
     113             :             // LCOV_EXCL_STOP
     114             :         }
     115             :     }
     116       35996 :     *renderp = 0;
     117             : 
     118             :     // Print the message.
     119       35996 :     printf("%s\n", rendered);
     120             : 
     121       35996 :     free(rendered);
     122             : }
     123             : 
     124       14511 : void speak(const char* msg, ...)
     125             : {
     126       14511 :     va_list ap;
     127       14511 :     va_start(ap, msg);
     128       14511 :     vspeak(msg, true, ap);
     129       14511 :     va_end(ap);
     130       14511 : }
     131             : 
     132          68 : void sspeak(const int msg, ...)
     133             : {
     134          68 :     va_list ap;
     135          68 :     va_start(ap, msg);
     136          68 :     fputc('\n', stdout);
     137          68 :     vprintf(arbitrary_messages[msg], ap);
     138          68 :     fputc('\n', stdout);
     139          68 :     va_end(ap);
     140          68 : }
     141             : 
     142       15158 : void pspeak(vocab_t msg, enum speaktype mode, int skip, bool blank, ...)
     143             : /* Find the skip+1st message from msg and print it.  Modes are:
     144             :  * feel = for inventory, what you can touch
     145             :  * look = the full description for the state the object is in
     146             :  * listen = the sound for the state the object is in
     147             :  * study = text on the object. */
     148             : {
     149       15158 :     va_list ap;
     150       15158 :     va_start(ap, blank);
     151       15158 :     switch (mode) {
     152         455 :     case touch:
     153         455 :         vspeak(objects[msg].inventory, blank, ap);
     154         455 :         break;
     155       13624 :     case look:
     156       13624 :         vspeak(objects[msg].descriptions[skip], blank, ap);
     157       13624 :         break;
     158          34 :     case hear:
     159          34 :         vspeak(objects[msg].sounds[skip], blank, ap);
     160          34 :         break;
     161           5 :     case study:
     162           5 :         vspeak(objects[msg].texts[skip], blank, ap);
     163           5 :         break;
     164        1040 :     case change:
     165        1040 :         vspeak(objects[msg].changes[skip], blank, ap);
     166        1040 :         break;
     167             :     }
     168       15158 :     va_end(ap);
     169       15158 : }
     170             : 
     171        7174 : void rspeak(vocab_t i, ...)
     172             : /* Print the i-th "random" message (section 6 of database). */
     173             : {
     174        7174 :     va_list ap;
     175        7174 :     va_start(ap, i);
     176        7174 :     vspeak(arbitrary_messages[i], true, ap);
     177        7174 :     va_end(ap);
     178        7174 : }
     179             : 
     180       18077 : void echo_input(FILE* destination, const char* input_prompt, const char* input)
     181             : {
     182       18077 :     size_t len = strlen(input_prompt) + strlen(input) + 1;
     183       18077 :     char* prompt_and_input = (char*) xcalloc(len);
     184       18077 :     strcpy(prompt_and_input, input_prompt);
     185       18077 :     strcat(prompt_and_input, input);
     186       18077 :     fprintf(destination, "%s\n", prompt_and_input);
     187       18077 :     free(prompt_and_input);
     188       18077 : }
     189             : 
     190       17821 : static int word_count(char* str)
     191             : {
     192       17821 :     char delims[] = " \t";
     193       17821 :     int count = 0;
     194       17821 :     int inblanks = true;
     195             : 
     196       85762 :     for (char *s = str; *s; s++)
     197       67941 :         if (inblanks) {
     198       22193 :             if (strchr(delims, *s) == 0) {
     199       22190 :                 ++count;
     200       22190 :                 inblanks = false;
     201             :             }
     202             :         } else {
     203       45748 :             if (strchr(delims, *s) != 0) {
     204        4395 :                 inblanks = true;
     205             :             }
     206             :         }
     207             : 
     208       17821 :     return (count);
     209             : }
     210             : 
     211       18134 : static char* get_input(void)
     212             : {
     213             :     // Set up the prompt
     214       18134 :     char input_prompt[] = "> ";
     215       18134 :     if (!settings.prompt)
     216           8 :         input_prompt[0] = '\0';
     217             : 
     218             :     // Print a blank line
     219       18134 :     printf("\n");
     220             : 
     221             :     char* input;
     222             :     while (true) {
     223       18572 :         input = readline(input_prompt);
     224             : 
     225       18353 :         if (input == NULL) // Got EOF; return with it.
     226          72 :             return (input);
     227       18281 :         if (input[0] == '#') { // Ignore comments.
     228         219 :             free(input);
     229         219 :             continue;
     230             :         }
     231             :         // We have a 'normal' line; leave the loop.
     232       18062 :         break;
     233             :     }
     234             : 
     235             :     // Strip trailing newlines from the input
     236       18062 :     input[strcspn(input, "\n")] = 0;
     237             : 
     238       18062 :     add_history(input);
     239             : 
     240       18062 :     if (!isatty(0))
     241       18062 :         echo_input(stdout, input_prompt, input);
     242             : 
     243       18062 :     if (settings.logfp)
     244          15 :         echo_input(settings.logfp, "", input);
     245             : 
     246       18062 :     return (input);
     247             : }
     248             : 
     249          52 : bool silent_yes(void)
     250             : {
     251          52 :     bool outcome = false;
     252             : 
     253           2 :     for (;;) {
     254          54 :         char* reply = get_input();
     255          54 :         if (reply == NULL) {
     256             :             // LCOV_EXCL_START
     257             :             // Should be unreachable. Reply should never be NULL
     258             :             free(reply);
     259             :             exit(EXIT_SUCCESS);
     260             :             // LCOV_EXCL_STOP
     261             :         }
     262          54 :         if (strlen(reply) == 0) {
     263           1 :             free(reply);
     264           1 :             rspeak(PLEASE_ANSWER);
     265           1 :             continue;
     266             :         }
     267             : 
     268          53 :         char* firstword = (char*) xcalloc(strlen(reply) + 1);
     269          53 :         sscanf(reply, "%s", firstword);
     270             : 
     271          53 :         free(reply);
     272             : 
     273         180 :         for (int i = 0; i < (int)strlen(firstword); ++i)
     274         127 :             firstword[i] = tolower(firstword[i]);
     275             : 
     276          53 :         int yes = strncmp("yes", firstword, sizeof("yes") - 1);
     277          53 :         int y = strncmp("y", firstword, sizeof("y") - 1);
     278          53 :         int no = strncmp("no", firstword, sizeof("no") - 1);
     279          53 :         int n = strncmp("n", firstword, sizeof("n") - 1);
     280             : 
     281          53 :         free(firstword);
     282             : 
     283          53 :         if (yes == 0 ||
     284             :             y == 0) {
     285          51 :             outcome = true;
     286          51 :             break;
     287           2 :         } else if (no == 0 ||
     288             :                    n == 0) {
     289           1 :             outcome = false;
     290           1 :             break;
     291             :         } else
     292           1 :             rspeak(PLEASE_ANSWER);
     293             :     }
     294          52 :     return (outcome);
     295             : }
     296             : 
     297             : 
     298         150 : bool yes(const char* question, const char* yes_response, const char* no_response)
     299             : /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
     300             :  *  if no, print Z and return false. */
     301             : {
     302         150 :     bool outcome = false;
     303             : 
     304          41 :     for (;;) {
     305         191 :         speak(question);
     306             : 
     307         191 :         char* reply = get_input();
     308         191 :         if (reply == NULL) {
     309             :             // LCOV_EXCL_START
     310             :             // Should be unreachable. Reply should never be NULL
     311             :             free(reply);
     312             :             exit(EXIT_SUCCESS);
     313             :             // LCOV_EXCL_STOP
     314             :         }
     315             : 
     316         187 :         if (strlen(reply) == 0) {
     317           1 :             free(reply);
     318           1 :             rspeak(PLEASE_ANSWER);
     319           1 :             continue;
     320             :         }
     321             : 
     322         186 :         char* firstword = (char*) xcalloc(strlen(reply) + 1);
     323         186 :         sscanf(reply, "%s", firstword);
     324             : 
     325         186 :         free(reply);
     326             : 
     327         448 :         for (int i = 0; i < (int)strlen(firstword); ++i)
     328         262 :             firstword[i] = tolower(firstword[i]);
     329             : 
     330         186 :         int yes = strncmp("yes", firstword, sizeof("yes") - 1);
     331         186 :         int y = strncmp("y", firstword, sizeof("y") - 1);
     332         186 :         int no = strncmp("no", firstword, sizeof("no") - 1);
     333         186 :         int n = strncmp("n", firstword, sizeof("n") - 1);
     334             : 
     335         186 :         free(firstword);
     336             : 
     337         186 :         if (yes == 0 ||
     338             :             y == 0) {
     339          46 :             speak(yes_response);
     340          46 :             outcome = true;
     341          46 :             break;
     342         140 :         } else if (no == 0 ||
     343             :                    n == 0) {
     344         100 :             speak(no_response);
     345         100 :             outcome = false;
     346         100 :             break;
     347             :         } else
     348          40 :             rspeak(PLEASE_ANSWER);
     349             : 
     350             :     }
     351             : 
     352         146 :     return (outcome);
     353             : }
     354             : 
     355             : /*  Data structure  routines */
     356             : 
     357       22187 : static int get_motion_vocab_id(const char* word)
     358             : // Return the first motion number that has 'word' as one of its words.
     359             : {
     360     1225472 :     for (int i = 0; i < NMOTIONS; ++i) {
     361     2910336 :         for (int j = 0; j < motions[i].words.n; ++j) {
     362     1714715 :             if (strncasecmp(word, motions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
     363        7666 :                     strchr(ignore, word[0]) == NULL ||
     364           2 :                     !settings.oldstyle))
     365       12420 :                 return (i);
     366             :         }
     367             :     }
     368             :     // If execution reaches here, we didn't find the word.
     369        9767 :     return (WORD_NOT_FOUND);
     370             : }
     371             : 
     372        9767 : static int get_object_vocab_id(const char* word)
     373             : // Return the first object number that has 'word' as one of its words.
     374             : {
     375      533306 :     for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
     376     1291733 :         for (int j = 0; j < objects[i].words.n; ++j) {
     377      768194 :             if (strncasecmp(word, objects[i].words.strs[j], TOKLEN) == 0)
     378        4377 :                 return (i);
     379             :         }
     380             :     }
     381             :     // If execution reaches here, we didn't find the word.
     382        5390 :     return (WORD_NOT_FOUND);
     383             : }
     384             : 
     385        5390 : static int get_action_vocab_id(const char* word)
     386             : // Return the first motion number that has 'word' as one of its words.
     387             : {
     388       47183 :     for (int i = 0; i < NACTIONS; ++i) {
     389      170615 :         for (int j = 0; j < actions[i].words.n; ++j) {
     390      128990 :             if (strncasecmp(word, actions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
     391         170 :                     strchr(ignore, word[0]) == NULL ||
     392           2 :                     !settings.oldstyle))
     393        5244 :                 return (i);
     394             :         }
     395             :     }
     396             :     // If execution reaches here, we didn't find the word.
     397         146 :     return (WORD_NOT_FOUND);
     398             : }
     399             : 
     400         102 : static bool is_valid_int(const char *str)
     401             : /* Returns true if the string passed in is represents a valid integer,
     402             :  * that could then be parsed by atoi() */
     403             : {
     404             :     // Handle negative number
     405         102 :     if (*str == '-')
     406           1 :         ++str;
     407             : 
     408             :     // Handle empty string or just "-". Should never reach this
     409             :     // point, because this is only used with transitive verbs.
     410         102 :     if (!*str)
     411             :         return false; // LCOV_EXCL_LINE
     412             : 
     413             :     // Check for non-digit chars in the rest of the stirng.
     414        1040 :     while (*str) {
     415         851 :         if (!isdigit(*str))
     416          15 :             return false;
     417             :         else
     418         836 :             ++str;
     419             :     }
     420             : 
     421          87 :     return true;
     422             : }
     423             : 
     424       35594 : static void get_vocab_metadata(command_word_t* word)
     425             : {
     426             :     /* Check for an empty string */
     427       35594 :     if (strncmp(word->raw, "", sizeof("")) == 0) {
     428       13407 :         word->id = WORD_EMPTY;
     429       13407 :         word->type = NO_WORD_TYPE;
     430       13407 :         return;
     431             :     }
     432             : 
     433             :     vocab_t ref_num;
     434             : 
     435       22187 :     ref_num = get_motion_vocab_id(word->raw);
     436       22187 :     if (ref_num != WORD_NOT_FOUND) {
     437       12420 :         word->id = ref_num;
     438       12420 :         word->type = MOTION;
     439       12420 :         return;
     440             :     }
     441             : 
     442        9767 :     ref_num = get_object_vocab_id(word->raw);
     443        9767 :     if (ref_num != WORD_NOT_FOUND) {
     444        4377 :         word->id = ref_num;
     445        4377 :         word->type = OBJECT;
     446        4377 :         return;
     447             :     }
     448             : 
     449        5390 :     ref_num = get_action_vocab_id(word->raw);
     450        5390 :     if (ref_num != WORD_NOT_FOUND) {
     451        5244 :         word->id = ref_num;
     452        5244 :         word->type = ACTION;
     453        5244 :         return;
     454             :     }
     455             : 
     456             :     // Check for the reservoir magic word.
     457         146 :     if (strcasecmp(word->raw, game.zzword) == 0) {
     458          44 :         word->id = PART;
     459          44 :         word->type = ACTION;
     460          44 :         return;
     461             :     }
     462             : 
     463             :     // Check words that are actually numbers.
     464         102 :     if (is_valid_int(word->raw)) {
     465          87 :         word->id = WORD_EMPTY;
     466          87 :         word->type = NUMERIC;
     467          87 :         return;
     468             :     }
     469             : 
     470          15 :     word->id = WORD_NOT_FOUND;
     471          15 :     word->type = NO_WORD_TYPE;
     472          15 :     return;
     473             : }
     474             : 
     475       17797 : static void tokenize(char* raw, command_t *cmd)
     476             : {
     477       17797 :     memset(cmd, '\0', sizeof(command_t));
     478             : 
     479             :     /* Bound prefix on the %s would be needed to prevent buffer
     480             :      * overflow.  but we shortstop this more simply by making each
     481             :      * raw-input buffer as long as the entire input buffer. */
     482       17797 :     sscanf(raw, "%s%s", cmd->word[0].raw, cmd->word[1].raw);
     483             : 
     484             :     /* (ESR) In oldstyle mode, simulate the uppercasing and truncating
     485             :      * effect on raw tokens of packing them into sixbit characters, 5
     486             :      * to a 32-bit word.  This is something the FORTRAN version did
     487             :      * becuse archaic FORTRAN had no string types.  Don Wood's
     488             :      * mechanical translation of 2.5 to C retained the packing and
     489             :      * thus this misfeature.
     490             :      *
     491             :      * It's philosophically questionable whether this is the right
     492             :      * thing to do even in oldstyle mode.  On one hand, the text
     493             :      * mangling was not authorial intent, but a result of limitations
     494             :      * in their tools. On the other, not simulating this misbehavior
     495             :      * goes against the goal of making oldstyle as accurate as
     496             :      * possible an emulation of the original UI.
     497             :      */
     498       17797 :     if (settings.oldstyle) {
     499           6 :         cmd->word[0].raw[TOKLEN + TOKLEN] = cmd->word[1].raw[TOKLEN + TOKLEN] = '\0';
     500          16 :         for (size_t i = 0; i < strlen(cmd->word[0].raw); i++)
     501          10 :             cmd->word[0].raw[i] = toupper(cmd->word[0].raw[i]);
     502          11 :         for (size_t i = 0; i < strlen(cmd->word[1].raw); i++)
     503           5 :             cmd->word[1].raw[i] = toupper(cmd->word[1].raw[i]);
     504             :     }
     505             : 
     506             :     /* populate command with parsed vocabulary metadata */
     507       17797 :     get_vocab_metadata(&(cmd->word[0]));
     508       17797 :     get_vocab_metadata(&(cmd->word[1]));
     509       17797 : }
     510             : 
     511       17865 : bool get_command_input(command_t *command)
     512             : /* Get user input on stdin, parse and map to command */
     513             : {
     514       17865 :     char inputbuf[LINESIZE];
     515             :     char* input;
     516             : 
     517             :     for (;;) {
     518       17913 :         input = get_input();
     519       17889 :         if (input == NULL)
     520          68 :             return false;
     521       17821 :         if (word_count(input) > 2) {
     522           1 :             rspeak(TWO_WORDS);
     523           1 :             free(input);
     524           1 :             continue;
     525             :         }
     526       17820 :         if (strcmp(input, "") != 0)
     527       17797 :             break;
     528          23 :         free(input);
     529             :     }
     530             : 
     531       17797 :     strncpy(inputbuf, input, LINESIZE - 1);
     532       17797 :     free(input);
     533             : 
     534       17797 :     tokenize(inputbuf, command);
     535             : 
     536       17797 :     return true;
     537             : }
     538             : 
     539          92 : void juggle(obj_t object)
     540             : /*  Juggle an object by picking it up and putting it down again, the purpose
     541             :  *  being to get the object to the front of the chain of things at its loc. */
     542             : {
     543             :     loc_t i, j;
     544             : 
     545          92 :     i = game.place[object];
     546          92 :     j = game.fixed[object];
     547          92 :     move(object, i);
     548          92 :     move(object + NOBJECTS, j);
     549          92 : }
     550             : 
     551        1210 : void move(obj_t object, loc_t where)
     552             : /*  Place any object anywhere by picking it up and dropping it.  May
     553             :  *  already be toting, in which case the carry is a no-op.  Mustn't
     554             :  *  pick up objects which are not at any loc, since carry wants to
     555             :  *  remove objects from game.atloc chains. */
     556             : {
     557             :     loc_t from;
     558             : 
     559        1210 :     if (object > NOBJECTS)
     560         385 :         from = game.fixed[object - NOBJECTS];
     561             :     else
     562         825 :         from = game.place[object];
     563             :     /* (ESR) Used to check for !SPECIAL(from). I *think* that was wrong... */
     564        1210 :     if (from != LOC_NOWHERE && from != CARRIED)
     565         899 :         carry(object, from);
     566        1210 :     drop(object, where);
     567        1210 : }
     568             : 
     569         126 : loc_t put(obj_t object, loc_t where, long pval)
     570             : /*  put() is the same as move(), except it returns a value used to set up the
     571             :  *  negated game.prop values for the repository objects. */
     572             : {
     573         126 :     move(object, where);
     574         126 :     return STASHED(pval);
     575             : }
     576             : 
     577        2886 : void carry(obj_t object, loc_t where)
     578             : /*  Start toting an object, removing it from the list of things at its former
     579             :  *  location.  Incr holdng unless it was already being toted.  If object>NOBJECTS
     580             :  *  (moving "fixed" second loc), don't change game.place or game.holdng. */
     581             : {
     582             :     long temp;
     583             : 
     584        2886 :     if (object <= NOBJECTS) {
     585        2586 :         if (game.place[object] == CARRIED)
     586         144 :             return;
     587        2442 :         game.place[object] = CARRIED;
     588             :         
     589        2442 :         if (object!= BIRD)
     590        2261 :             ++game.holdng;
     591             :     }
     592        2742 :     if (game.atloc[where] == object) {
     593        1720 :         game.atloc[where] = game.link[object];
     594        1720 :         return;
     595             :     }
     596        1022 :     temp = game.atloc[where];
     597        3679 :     while (game.link[temp] != object) {
     598        1635 :         temp = game.link[temp];
     599             :     }
     600        1022 :     game.link[temp] = game.link[object];
     601             : }
     602             : 
     603        9608 : void drop(obj_t object, loc_t where)
     604             : /*  Place an object at a given loc, prefixing it onto the game.atloc list.  Decr
     605             :  *  game.holdng if the object was being toted. */
     606             : {
     607        9608 :     if (object > NOBJECTS)
     608        1463 :         game.fixed[object - NOBJECTS] = where;
     609             :     else {
     610        8145 :         if (game.place[object] == CARRIED)
     611        2231 :             if (object != BIRD)
     612             :                 /* The bird has to be weightless.  This ugly hack (and the
     613             :                  * corresponding code in the drop function) brought to you
     614             :                  * by the fact that when the bird is caged, we need to be able
     615             :                  * to either 'take bird' or 'take cage' and have the right thing
     616             :                  * happen.
     617             :                  */
     618        2056 :                 --game.holdng;
     619        8145 :         game.place[object] = where;
     620             :     }
     621        9608 :     if (where == LOC_NOWHERE ||
     622             :         where == CARRIED)
     623         602 :         return;
     624        9006 :     game.link[object] = game.atloc[where];
     625        9006 :     game.atloc[where] = object;
     626             : }
     627             : 
     628         360 : int atdwrf(loc_t where)
     629             : /*  Return the index of first dwarf at the given location, zero if no dwarf is
     630             :  *  there (or if dwarves not active yet), -1 if all dwarves are dead.  Ignore
     631             :  *  the pirate (6th dwarf). */
     632             : {
     633             :     int at;
     634             : 
     635         360 :     at = 0;
     636         360 :     if (game.dflag < 2)
     637          21 :         return at;
     638         339 :     at = -1;
     639        1070 :     for (long i = 1; i <= NDWARVES - 1; i++) {
     640        1025 :         if (game.dloc[i] == where)
     641         294 :             return i;
     642         731 :         if (game.dloc[i] != 0)
     643         325 :             at = 0;
     644             :     }
     645          45 :     return at;
     646             : }
     647             : 
     648             : /*  Utility routines (setbit, tstbit, set_seed, get_next_lcg_value,
     649             :  *  randrange) */
     650             : 
     651         105 : long setbit(int bit)
     652             : /*  Returns 2**bit for use in constructing bit-masks. */
     653             : {
     654         105 :     return (1L << bit);
     655             : }
     656             : 
     657      506290 : bool tstbit(long mask, int bit)
     658             : /*  Returns true if the specified bit is set in the mask. */
     659             : {
     660      506290 :     return (mask & (1 << bit)) != 0;
     661             : }
     662             : 
     663         191 : void set_seed(int32_t seedval)
     664             : /* Set the LCG seed */
     665             : {
     666         191 :     game.lcg_x = seedval % LCG_M;
     667         191 :     if (game.lcg_x < 0) {
     668           1 :         game.lcg_x = LCG_M + game.lcg_x;
     669             :     }
     670             :     // once seed is set, we need to generate the Z`ZZZ word
     671        1146 :     for (int i = 0; i < 5; ++i) {
     672         955 :         game.zzword[i] = 'A' + randrange(26);
     673             :     }
     674         191 :     game.zzword[1] = '\''; // force second char to apostrophe
     675         191 :     game.zzword[5] = '\0';
     676         191 : }
     677             : 
     678       27929 : static int32_t get_next_lcg_value(void)
     679             : /* Return the LCG's current value, and then iterate it. */
     680             : {
     681       27929 :     int32_t old_x = game.lcg_x;
     682       27929 :     game.lcg_x = (LCG_A * game.lcg_x + LCG_C) % LCG_M;
     683       27929 :     return old_x;
     684             : }
     685             : 
     686       27929 : int32_t randrange(int32_t range)
     687             : /* Return a random integer from [0, range). */
     688             : {
     689       27929 :     return range * get_next_lcg_value() / LCG_M;
     690             : }
     691             : 
     692             : // LCOV_EXCL_START
     693             : void bug(enum bugtype num, const char *error_string)
     694             : {
     695             :     fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
     696             :     exit(EXIT_FAILURE);
     697             : }
     698             : // LCOV_EXCL_STOP
     699             : 
     700             : /* end */
     701             : 
     702        1039 : void state_change(obj_t obj, int state)
     703             : /* Object must have a change-message list for this to be useful; only some do */
     704             : {
     705        1039 :     game.prop[obj] = state;
     706        1039 :     pspeak(obj, change, state, true);
     707        1039 : }
     708             : 
     709             : /* end */

Generated by: LCOV version 1.13