Beispiel #1
0
/*
** Check that 'arg' either is a table or can behave like one (that is,
** has a metatable with the required metamethods)
*/
static void checktab (lua_State *L, int arg, int what) {
  if (lua_type(L, arg) != LUA_TTABLE) {  /* is it not a table? */
    int n = 1;  /* number of elements to pop */
    if (lua_getmetatable(L, arg) &&  /* must have metatable */
        (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
        (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
        (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
      lua_pop(L, n);  /* pop metatable and tested metamethods */
    }
    else
      luaL_checktype(L, arg, LUA_TTABLE);  /* force an error */
  }
}
Beispiel #2
0
int pPathfind(struct game *g, int checkonly)
{
    int i = 0, x, y;
    struct queue_t *queue;
	struct grid_t *grid;

    printf("Copying grid\n");

    grid = copy_grid(g);
    printf("Allocating queue\n");
    queue = alloc_queue(grid);
/*    printf("Have we crashed yet?\n");
    for(i=0;i< (signed int)grid->size;i++)
    {
        if ( i % grid->w == 0 ) printf("\n");
        switch(grid->ar[i]) {
            case 0:     printf("%3d",grid->path[i]); break;
            case 1:     printf("###"); break;
            case 2:     printf("XXX"); break;
            case 254:   printf("SSS"); break;
            case 255:   printf("EEE"); break;

        }
    }
    printf("\n\n");*/


    for(i=0; i < (signed int)grid->size; i++) {
        if (grid->ar[i] == 255) {
            push(queue, grid, i, 1);
        }
    }

    while (grid->marked[HEAD(queue).id] != 0) {
        checkfield(queue, grid);
        queue->head++;
		if ( queue->head >= (signed int)grid->size ) break;
//		printf("Gonna check: %d (%d)\n", HEAD(queue).id, queue->head);
    }

/*    for(i=0;i< (signed int)grid->size;i++)
    {
        if ( i % grid->w == 0 ) printf("\n");
        switch(grid->ar[i]) {
            case 0:     printf("%3d",grid->path[i]); break;
            case 1:     printf("###"); break;
			case 2:     printf("XXX"); break;
            case 254:   printf("SSS"); break;
            case 255:   printf("EEE"); break;

        }
    }
    printf("\n\n");*/


	if ( checkonly )
	{
		int ret = 0;
//		puts("We need to check if any of the entrances are blocked.");
		for(i=0;i<g->startN;i++)
		{
			if ( grid->path[g->start[i][0]+(g->start[i][1]*G_WIDTH)] > 0 )
			{
				continue;
			}
//			printf("Start position no: %d, cell %d x %d, is value %d\n", i, g->start[i][0], g->start[i][1]*G_WIDTH, grid->path[g->start[i][0]+(g->start[i][1]*G_WIDTH)]);
			ret++;
		}
		free_queue(queue);
		free_grid(grid);
		return ret;
	}

	i = 0;
    for(y=0; y < G_HEIGHT; y++) {
        for(x=0; x < G_WIDTH; x++) {
            g->path[y][x] = grid->path[i];
            i++;
        }
    }

    free_queue(queue);
    free_grid(grid);
    return 0;
}