Beispiel #1
0
static void
state_table_init (StateTable *table, unsigned n_entries)
{
  table->n_entries = n_entries;
  table->table_size = 19;
  table->hash_table = dsk_malloc0 (sizeof (State*) * table->table_size);
  table->occupancy = 0;
  table->sizeof_state = DSK_ALIGN (sizeof (State) + (n_entries-1) * sizeof(unsigned), sizeof (void*));
  table->n_states_in_slab = 16;
  table->sizeof_slab = sizeof (StateSlab) + table->sizeof_state * table->n_states_in_slab;
  table->n_states_free_in_slab = 0;
  table->next_free_in_cur_slab = NULL;		/* unneeded */
  table->cur_slab = NULL;
  table->uninitialized_list = NULL;
};
Beispiel #2
0
static Game *
create_game (const char *name,
             unsigned    width,
             unsigned    height)

{
  Game *game = dsk_malloc (sizeof (Game));
  unsigned usize;
  unsigned i;

  game->name = dsk_strdup (name);
  game->next_game = all_games;
  all_games = game;
  game->universe_width = width;
  game->universe_height = height;
  usize = width * height;
  game->h_walls = generate_ones (usize);
  game->v_walls = generate_ones (usize);
  for (i = 0; i < N_OBJECT_TYPES; i++)
    game->objects[i] = NULL;
  game->generators = NULL;
  game->cells = dsk_malloc0 (sizeof (Cell) * width * height);
  game->latest_update = 0;
  game->wrap = DSK_TRUE;
  game->diag_bullets_bounce = DSK_TRUE;
  game->bullet_kills_player = DSK_TRUE;
  game->bullet_kills_generator = DSK_TRUE;
  game->pending_updates = NULL;

  /* Generate with Modified Kruskals Algorithm, see 
   *    http://en.wikipedia.org/wiki/Maze_generation_algorithm
   */
  TmpWall *tmp_walls = dsk_malloc (sizeof (TmpWall) * usize * 2);
  TmpSetInfo *sets = dsk_malloc (sizeof (TmpSetInfo) * usize);

  /* connect the walls together in random order */
  unsigned *scramble;
  scramble = dsk_malloc (sizeof (unsigned) * usize * 2);
  for (i = 0; i < usize * 2; i++)
    scramble[i] = i;
  for (i = 0; i < usize * 2; i++)
    swap_ints (scramble + random_int_range (usize * 2), scramble + random_int_range (usize * 2));

  TmpWall *wall_list = NULL;
  for (i = 0; i < usize * 2; i++)
    {
      unsigned e = scramble[i];
      unsigned h = e % 2;
      unsigned x = (e / 2) % width;
      unsigned y = e / (width * 2);
      if (!game->wrap)
        {
          if ((h && y == 0) || (!h && x == 0))
            continue;
        }
      tmp_walls[e].prev = NULL;
      tmp_walls[e].next = wall_list;
      if (wall_list)
        wall_list->prev = tmp_walls + e;
      wall_list = tmp_walls + e;
    }

  for (i = 0; i < usize; i++)
    {
      sets[i].set_number = i;
      sets[i].next_in_set = sets + i;
    }

  while (wall_list != NULL)
    {
      /* Invariants:
           - the sets are in a ring by set number.
           - The wall_list only consists of walls that separate distinct sets.
       */

      /* remove wall */
      unsigned e = wall_list - tmp_walls;
      unsigned h = e % 2;
      unsigned x = (e / 2) % width;
      unsigned y = e / (width * 2);
      TmpSetInfo *si = sets + e / 2;
      TmpSetInfo *osi;
      if (h)
        {
          if (y == 0)
            osi = si + (height - 1) * width;
          else
            osi = si - width;
          game->h_walls[x + y * width] = 0;
        }
      else
        {
          if (x == 0)
            osi = si + width - 1;
          else
            osi = si - 1;
          game->v_walls[x + y * width] = 0;
        }
      dsk_assert (osi->set_number != si->set_number);
      TmpSetInfo *kring = osi->set_number < si->set_number ? osi : si;              /* ring to keep */
      TmpSetInfo *dring = osi->set_number < si->set_number ? si : osi;              /* ring to change */
      TmpSetInfo *dring_start = dring;

      /* combine sets (removing any walls that no longer separate different sets
         from the list of walls to remove) */
      unsigned set = kring->set_number;
      do
        {
          unsigned x = (dring - sets) % width;
          unsigned y = (dring - sets) / width;
          int wall_idx;
          dring->set_number = set;

#if 0
          if (wall_list)
            {
              dsk_assert (wall_list->prev == NULL);
              TmpWall *t;
              for (t = wall_list; t; t = t->next)
                if (t->next)
                  dsk_assert (t->next->prev == t);
            }
#endif

          /* Maybe remove left wall from candidate set of walls. */
          wall_idx = -1;
          if (x > 0 && (dring-1)->set_number == set)
            wall_idx = 2 * (x + y * width);
          else if (x == 0 && game->wrap && (dring+width-1)->set_number == set)
            wall_idx = 2 * (x + y * width);
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          /* Maybe remove right wall from candidate set of walls. */
          wall_idx = -1;
          if (x < width - 1 && (dring+1)->set_number == set)
            wall_idx = 2 * ((x+1) + y * width);
          else if (x == width - 1 && game->wrap && (dring-width+1)->set_number == set)
            wall_idx = 2 * (0 + y * width);
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          /* Maybe remove top wall from candidate set of walls. */
          wall_idx = -1;
          if (y > 0 && (dring-width)->set_number == dring->set_number)
            wall_idx = 2 * (x + y * width) + 1;
          else if (y == 0 && game->wrap && (dring+width*(height-1))->set_number == set)
            wall_idx = 2 * (x + y * width) + 1;
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          /* Maybe remove bottom wall from candidate set of walls. */
          wall_idx = -1;
          if (y < height - 1 && (dring+width)->set_number == set)
            wall_idx = 2 * (x + (y+1) * width) + 1;
          else if (y == height - 1 && game->wrap && (dring-(height-1)*width)->set_number == set)
            wall_idx = 2 * (x + 0 * width) + 1;
          if (wall_idx >= 0)
            remove_tmp_wall (tmp_walls, wall_idx, &wall_list);

          dring = dring->next_in_set;
        }
      while (dring != dring_start);

      /* Merge the rings */
      TmpSetInfo *old_dring_next = dring->next_in_set;
      dring->next_in_set = kring->next_in_set;
      kring->next_in_set = old_dring_next;

    }

  dsk_free (tmp_walls);
  dsk_free (sets);
  dsk_free (scramble);

  /* generate generators */
  unsigned n_generators = 12 + rand () % 6;
  dsk_warning ("%u generators", n_generators);
  i = 0;
  while (i < n_generators)
    {
      unsigned idx = random_int_range (usize);
      Cell *cell = game->cells + idx;
      if (cell->generator == NULL)
        {
          cell->generator = dsk_malloc (sizeof (Generator));
          cell->generator->game = game;
          cell->generator->x = (idx % width) * CELL_SIZE + CELL_SIZE/2;
          cell->generator->y = (idx / width) * CELL_SIZE + CELL_SIZE/2;
          dsk_warning ("created generator at %u,%u",cell->generator->x ,cell->generator->y);
          cell->generator->generator_prob = 0.01;
          cell->generator->next_in_game = game->generators;
          cell->generator->prev_in_game = NULL;
          if (game->generators)
            game->generators->prev_in_game = cell->generator;
          game->generators = cell->generator;

          i++;
        }
    }

  game->timer = dsk_main_add_timer_millis (update_period_msecs,
                                    (DskTimerFunc) game_update_timer_callback,
                                    game);
  return game;
}