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

          Line data    Source code
       1             : /*
       2             :  * Initialisation
       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 <stdbool.h>
      13             : #include <time.h>
      14             : 
      15             : #include "advent.h"
      16             : 
      17             : struct settings_t settings = {
      18             :     .logfp = NULL,
      19             :     .oldstyle = false,
      20             :     .prompt = true
      21             : };
      22             : 
      23             : struct game_t game = {
      24             :     .dloc[1] = LOC_KINGHALL,
      25             :     .dloc[2] = LOC_WESTBANK,
      26             :     .dloc[3] = LOC_Y2,
      27             :     .dloc[4] = LOC_ALIKE3,
      28             :     .dloc[5] = LOC_COMPLEX,
      29             : 
      30             :     /*  Sixth dwarf is special (the pirate).  He always starts at his
      31             :      *  chest's eventual location inside the maze. This loc is saved
      32             :      *  in chloc for ref. The dead end in the other maze has its
      33             :      *  loc stored in chloc2. */
      34             :     .dloc[6] = LOC_DEADEND12,
      35             :     .chloc   = LOC_DEADEND12,
      36             :     .chloc2  = LOC_DEADEND13,
      37             :     .abbnum  = 5,
      38             :     .clock1  = WARNTIME,
      39             :     .clock2  = FLASHTIME,
      40             :     .newloc  = LOC_START,
      41             :     .loc     = LOC_START,
      42             :     .limit   = GAMELIMIT,
      43             :     .foobar  = WORD_EMPTY,
      44             : };
      45             : 
      46         105 : long initialise(void)
      47             : {
      48         105 :     if (settings.oldstyle)
      49           1 :         printf("Initialising...\n");
      50             : 
      51         105 :     srand(time(NULL));
      52         105 :     long seedval = (long)rand();
      53         105 :     set_seed(seedval);
      54             : 
      55        7350 :     for (int i = 1; i <= NOBJECTS; i++) {
      56        7245 :         game.place[i] = LOC_NOWHERE;
      57             :     }
      58             : 
      59       19425 :     for (int i = 1; i <= NLOCATIONS; i++) {
      60       38640 :         if (!(locations[i].description.big == 0 ||
      61       19320 :               tkey[i] == 0)) {
      62       19320 :             int k = tkey[i];
      63       19320 :             if (T_TERMINATE(travel[k]))
      64        2310 :                 conditions[i] |= (1 << COND_FORCED);
      65             :         }
      66             :     }
      67             : 
      68             :     /*  Set up the game.atloc and game.link arrays.
      69             :      *  We'll use the DROP subroutine, which prefaces new objects on the
      70             :      *  lists.  Since we want things in the other order, we'll run the
      71             :      *  loop backwards.  If the object is in two locs, we drop it twice.
      72             :      *  Also, since two-placed objects are typically best described
      73             :      *  last, we'll drop them first. */
      74        7350 :     for (int i = NOBJECTS; i >= 1; i--) {
      75        7245 :         if (objects[i].fixd > 0) {
      76        1050 :             drop(i + NOBJECTS, objects[i].fixd);
      77        1050 :             drop(i, objects[i].plac);
      78             :         }
      79             :     }
      80             : 
      81        7350 :     for (int i = 1; i <= NOBJECTS; i++) {
      82        7245 :         int k = NOBJECTS + 1 - i;
      83        7245 :         game.fixed[k] = objects[k].fixd;
      84        7245 :         if (objects[k].plac != 0 && objects[k].fixd <= 0)
      85        4410 :             drop(k, objects[k].plac);
      86             :     }
      87             : 
      88             :     /*  Treasure props are initially -1, and are set to 0 the first time
      89             :      *  they are described.  game.tally keeps track of how many are
      90             :      *  not yet found, so we know when to close the cave. */
      91        7350 :     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
      92        7245 :         if (objects[treasure].is_treasure) {
      93        2100 :             if (objects[treasure].inventory != 0)
      94        2100 :                 game.prop[treasure] = STATE_NOTFOUND;
      95        2100 :             game.tally = game.tally - game.prop[treasure];
      96             :         }
      97             :     }
      98         105 :     game.conds = setbit(11);
      99             : 
     100         105 :     return seedval;
     101             : }

Generated by: LCOV version 1.13