Exemplo n.º 1
0
static int fractional_cascade(struct _cola *c, unsigned int lvlno,
				struct cola_elem *cur)
{
	unsigned int nl = lvlno + 1;
	struct buf next;
	cola_key_t i, j;

	if ( (c->c_nelem < (1U << nl)) )
		return 1;

	/* TODO: investigate relying on prior level pointers
	 * if that level went from 1 to 0.
	*/
	dprintf(" - fractional cascade %u -> %u\n", lvlno, nl);
	if ( !read_level(c, nl, &next) )
		return 0;

	for(i = j = 0; i < (1U << lvlno); i++) {
		while(j < (1U << nl) && next.ptr[j].key < cur[i].key) {
			j++;
		}
		cur[i].fp = j;
	}

	buf_finish(&next);

	return 1;
}
Exemplo n.º 2
0
int cola_dump(cola_t c)
{
	unsigned int i;

	for(i = 0; c->c_nelem >= (1U << i); i++) {
		struct buf level;
		unsigned int j;

		if ( !read_level(c, i, &level) )
			return 0;

		if ( !(c->c_nelem & (1U << i)) )
			printf("\033[2;37m");
		printf("level %u:", i);
		for(j = 0; j < (1U << i); j++) {
			printf(" %"PRIu64, level.ptr[j].key);
			printf("[%"PRIu64"]", level.ptr[j].fp);
		}
		if ( !(c->c_nelem & (1U << i)) )
			printf("\033[0m");
		printf("\n");
		buf_finish(&level);
	}

	return 1;
}
Exemplo n.º 3
0
void available_levels(al_defs *al) {

  bool quit=false;
  level_list *list = get_level_list();
  level_list_mem *p = list->first;
  level* l = NULL;

  while(!quit) {
    al_clear_to_color(al_map_rgb(0,0,0));
    al_draw_text(al->logo_font,al_map_rgb(255,255,255),al->width/2,
        20,ALLEGRO_ALIGN_CENTRE,"sokoban");
    al_draw_text(al->menu_font,al_map_rgb(255,255,255), al->width/2, 
        160, ALLEGRO_ALIGN_CENTRE,"Select your level:");

    al_draw_textf(al->menu_font, al_map_rgb(255,0,0), al->width/2, 
        al->height/2, ALLEGRO_ALIGN_CENTRE, "%s", p->name);

    al_draw_text(al->hint_font, al_map_rgb(120, 120, 120), 10,
        al->height-30, ALLEGRO_ALIGN_LEFT, 
        "Use arrows to navigate, ENTER to choose, Esc to return to the main screen");
    al_flip_display();

    ALLEGRO_EVENT ev;
    al_wait_for_event(al->queue, &ev);
    if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
      //printf("EVENT KEYCODE: %d\n",ev.keyboard.keycode);
      switch(ev.keyboard.keycode) {
        case ALLEGRO_KEY_ESCAPE:
          quit=true;
          break;

        case ALLEGRO_KEY_ENTER: 
          do {
            if(l!=NULL) free_level(l);
            l = read_level(p->name);
            if(l==NULL) break;
          }while(play_level(al, l, p->name));
          free_level(l);
          l=NULL;
          break;

        case ALLEGRO_KEY_LEFT:
          p=p->prev;
          break;

        case ALLEGRO_KEY_RIGHT:
          p=p->next;
          break;
      }
    }
  }
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: bor0/soko
int main(int argc, char *argv[]) {
    FILE *t;
    struct level *map;
    char level_name[LEVELNAMESZ];
    int asset_width = 32, asset_height = 32, i;

    printf("Soko v1.0 by Boro Sitnikovski\n=============================\n");

    if (argc > 1) {
        strncpy(level_name, argv[1], LEVELNAMESZ-1);
    }
    else {
        printf("Enter a valid level name (levels/*.dat): ");
        if (fgets(level_name, LEVELNAMESZ, stdin) == NULL) {
            printf("Enter a valid level, m8.\n");
            return 0;
        }
    }

    /* remove newlines */
    i = strlen(level_name) - 1;
    while (level_name[i] == '\r' || level_name[i] == '\n') i--;
    level_name[i+1] = '\0';

    /* attempt to parse level */
    map = read_level(level_name);

    if (map == NULL) {
        printf("Error: Cannot parse level.\n");
        return 0;
    }

    /* parse assets */
    t = fopen("assets.cfg", "r");
    if (t) {
        fscanf(t, "%d %d", &asset_width, &asset_height);
        fclose(t);
    }

    /* main SDL loop */
    sokosdl_main(map, asset_width, asset_height);

    /* release level */
    free_level(&map);

    return 1;

}
Exemplo n.º 5
0
void Reset_Level(GLuint sprites, Entity& player, std::vector<Entity*>& entities, Entity& flag, unsigned& LEVEL_X, unsigned& LEVEL_Y, unsigned short **& level, std::string fn){
	player.health = 1;
	player.damage = 1;
	player.velocity_x = 0;
	player.velocity_y = 0;

	for (size_t i = 0; i < entities.size(); ++i){
		delete entities[i];
	}
	entities.clear();

	for (size_t i = 0; i < LEVEL_Y; ++i){
		delete[] level[i];
	}
	delete[] level;

	read_level(level, LEVEL_X, LEVEL_Y, entities, player, flag, sprites, fn);
}
void vcos_log_register(const char *name, VCOS_LOG_CAT_T *category)
{
   const char *env;
   VCOS_LOG_CAT_T *i;
   memset(category, 0, sizeof(*category));

   category->name  = name;
   category->level = VCOS_LOG_ERROR;
   category->flags.want_prefix = 1;

   vcos_mutex_lock(&lock);

   /* is it already registered? */
   for (i = vcos_logging_categories; i ; i = i->next )
   {
      if (i == category)
         break;
   }

   if (!i)
   {
      /* not yet registered */
      category->next = vcos_logging_categories;
      vcos_logging_categories = category;
   }

   vcos_mutex_unlock(&lock);

   /* Check to see if this log level has been enabled. Look for
    * (<category:level>,)*
    *
    * VC_LOGLEVEL=ilcs:info,vchiq:warn
    */

   env = _VCOS_LOG_LEVEL();
   if (env)
   {
      do
      {
         char env_name[64];
         VCOS_LOG_LEVEL_T level;
         if (read_tok(env_name, sizeof(env_name), &env, ':') &&
             read_level(&level, &env, ','))
         {
            if (strcmp(env_name, name) == 0)
            {
               category->level = level;
               break;
            }
         }
         else
         {
            if (!warned_loglevel)
            {
                vcos_log("VC_LOGLEVEL format invalid at %s\n", env);
                warned_loglevel = 1;
            }
            return;
         }
      } while (env[0] != '\0');
   }
}
Exemplo n.º 7
0
int cola_insert(cola_t c, cola_key_t key)
{
	cola_key_t newcnt = c->c_nelem + 1;
	struct buf level;
	unsigned int i;

	dprintf("Insert key %"PRIu64"\n", key);

	if ( !buf_alloc(c, 1, &level) )
		return 0;
	level.ptr[0].key = key;

	/* make sure the level we're about to write to is allocated and,
	 * if required, mapped
	*/
	if ( newcnt == (1ULL << c->c_nxtlvl) ) {
		cola_key_t nr_ent, ofs;
		size_t sz;

		nr_ent = (1ULL << c->c_nxtlvl);
		ofs = nr_ent - 1;
		ofs *= sizeof(struct cola_elem);
		ofs += sizeof(struct cola_hdr);

		sz = nr_ent * sizeof(struct cola_elem);
		dprintf("fallocate level %u\n", c->c_nxtlvl);
		if ( posix_fallocate(c->c_fd, ofs, ofs + sz) )
			fprintf(stderr, "%s: fallocate: %s\n",
				cmd, os_err());
		if ( c->c_nxtlvl <= MAP_LEVELS &&
				(1U << c->c_nxtlvl) > c->c_nelem ) {
			if ( !remap(c, c->c_nxtlvl) )
				return 0;
		}
		c->c_nxtlvl++;
	}

	for(i = 0; newcnt >= (1U << i); i++) {
		if ( c->c_nelem & (1U << i) ) {
			struct buf level2, merged;
			int ret;

			dprintf(" - level %u full\n", i);
			if ( !read_level(c, i, &level2) ) {
				buf_finish(&level);
				return 0;
			}

			if ( (c->c_nelem & (1U << (i + 1))) ||
					i + 1 >= c->c_maplvls ) {
				ret = buf_alloc(c, (1U << (i + 1)), &merged);
			}else{
				/* landing in next level so write to map */
				ret = write_prep(c, i + 1, &merged);
			}
			if ( !ret ) {
				buf_finish(&level2);
				buf_finish(&level);
				return 0;
			}

			level_merge(&level2, &level, &merged);
			if ( !write_level(c, i, &level2) ) {
				buf_finish(&level2);
				buf_finish(&level);
				buf_finish(&merged);
				return 0;
			}

			buf_finish(&level2);
			buf_finish(&level);

			memcpy(&level, &merged, sizeof(level));
		}else{
			dprintf(" - level %u empty\n", i);
			if ( !fractional_cascade(c, i, level.ptr) ||
					!write_level(c, i, &level) ) {
				buf_finish(&level);
				return 0;
			}
			buf_finish(&level);
			break;
		}
	}

	c->c_nelem++;
	dprintf("\n");
#if DEBUG
	cola_dump(c);
	dprintf("\n");
#endif
	return 1;
}
Exemplo n.º 8
0
void vcos_log_register(const char *name, VCOS_LOG_CAT_T *category)
{
   const char *env;
   VCOS_LOG_CAT_T *i;

   category->name  = name;
   if ( category->level == VCOS_LOG_UNINITIALIZED )
   {
      category->level = VCOS_LOG_ERROR;
   }
   category->flags.want_prefix = (category != &dflt_log_category );

   vcos_mutex_lock(&lock);

   /* is it already registered? */
   for (i = vcos_logging_categories; i ; i = i->next )
   {
      if (i == category)
      {
         i->refcount++;
         break;
      }
   }

   if (!i)
   {
      /* not yet registered */
      category->next = vcos_logging_categories;
      vcos_logging_categories = category;
      category->refcount++;

      vcos_log_platform_register(category);
   }

   vcos_mutex_unlock(&lock);

   /* Check to see if this log level has been enabled. Look for
    * (<category:level>,)*
    *
    * VC_LOGLEVEL=ilcs:info,vchiq:warn
    */

   env = _VCOS_LOG_LEVEL();
   if (env)
   {
      do
      {
         char env_name[64];
         VCOS_LOG_LEVEL_T level;
         if (read_tok(env_name, sizeof(env_name), &env, ':') &&
             read_level(&level, &env, ','))
         {
            if (strcmp(env_name, name) == 0)
            {
               category->level = level;
               break;
            }
         }
         else
         {
            if (!warned_loglevel)
            {
                vcos_log("VC_LOGLEVEL format invalid at %s\n", env);
                warned_loglevel = 1;
            }
            return;
         }
      } while (env[0] != '\0');
   }

   vcos_log_info( "Registered log category '%s' with level %s",
                  category->name,
                  vcos_log_level_to_string( category->level ));
}
Exemplo n.º 9
0
static void read_uvar_cnstr(environment const & env, io_state const &, deserializer & d) {
    name n    = read_name(d);
    level lvl = read_level(d);
    env->add_uvar_cnstr(n, lvl);
}
Exemplo n.º 10
0
void edit_level(al_defs *al, char name[255]) {
  level *lvl = read_level(name);
  int posx,posy; posx=posy=1;
  while(true){
    draw_background(al);
    int i,j;
    for(i=0;i<lvl->h;i++) {
      for(j=0;j<lvl->w;j++) {
        draw_tile(al, i, j, lvl);
      }
    }
    draw_tile_border(al, posy, posx, lvl);
    al_draw_text(al->hint_font, al_map_rgb(120,120,120),
        20,al->height-80,ALLEGRO_ALIGN_LEFT,
        "W - wall, O - nothing");
    al_draw_text(al->hint_font, al_map_rgb(120,120,120),
        20,al->height-60,ALLEGRO_ALIGN_LEFT,
        "P - player, S - chest, X - destination point");
    al_draw_text(al->hint_font, al_map_rgb(120,120,120),
        20,al->height-40,ALLEGRO_ALIGN_LEFT,
        "Z - chest in correct place, C - player on dest. point");
    al_draw_text(al->hint_font, al_map_rgb(120,120,120),
        20,al->height-20,ALLEGRO_ALIGN_LEFT,
        "Esc - escape, Enter - save");
    al_flip_display();

    //event time!
    ALLEGRO_EVENT ev;
    al_wait_for_event(al->queue, &ev);

    if(ev.type!=ALLEGRO_EVENT_KEY_DOWN) 
      continue;

    switch(ev.keyboard.keycode) {
      case ALLEGRO_KEY_LEFT:
        if(posx>1) posx--;
        break;
      case ALLEGRO_KEY_RIGHT:
        if(posx<(lvl->w-2)) posx++;
        break;
      case ALLEGRO_KEY_UP:
        if(posy>1) posy--;
        break;
      case ALLEGRO_KEY_DOWN:
        if(posy<(lvl->h-2)) posy++;
        break;
      case ALLEGRO_KEY_P:
        for(i=0;i<lvl->h;i++)
          for(j=0;j<lvl->w;j++)
            if(lvl->map[i][j]=='P' || lvl->map[i][j]=='C')
              lvl->map[i][j]=' ';
        lvl->map[posy][posx]='P';
        break;
      case ALLEGRO_KEY_X:
        lvl->map[posy][posx]='X';
        break;
      case ALLEGRO_KEY_Z:
        lvl->map[posy][posx]='Z';
        break;
      case ALLEGRO_KEY_S:
        lvl->map[posy][posx]='S';
        break;
      case ALLEGRO_KEY_C:
        for(i=0;i<lvl->h;i++)
          for(j=0;j<lvl->w;j++)
            if(lvl->map[i][j]=='P' || lvl->map[i][j]=='C')
              lvl->map[i][j]=' ';

        lvl->map[posy][posx]='C';
        break;
      case ALLEGRO_KEY_O:
        lvl->map[posy][posx]=' ';
        break;
      case ALLEGRO_KEY_W:
        lvl->map[posy][posx]='#';
        break;
      case ALLEGRO_KEY_ENTER:
        save_map(name, lvl);
        return;
      case ALLEGRO_KEY_ESCAPE:
        return;
    }

  }
}