Ejemplo n.º 1
0
int main(int argc, char** argv)
{
    map_config_t config;
    dataset_t* dataset;
    map_t* map;

    configure(&config, argc, argv);

    if(config.ot == OT_CREATE)
    {
        dataset = dataset_load(config.infile);
        if(dataset)
        {
            map = map_create(dataset, &config.level);
            if(config.print)
                map_print(map);
            map_save(config.outfile, map);
            map_destroy(map);
        }
        dataset_destroy(&dataset);
    }
    else if(config.ot == OT_READ)
    {
        map = map_load(config.infile);
        if(config.print)
            map_print(map);
        map_destroy(map);
    }
    return 0;
}
Ejemplo n.º 2
0
static void clist_print() { 
    Content *f; 
    fprintf(stderr,"content list:\n"); 
    for (f=content_head; f; f=f->next) { 
	fprintf(stderr,"  name: %s\n",f->name); 
	map_print(f->bhead); 
    }
} 
Ejemplo n.º 3
0
void test_map() {

    map *m = map_make();
    m->hash = sdbmHash;
    m->copyKey = copyKey;
    m->compare = compareKey;

    char *test[] = {
            "asd",
            "bsddj",
            "csdf",
            "dsdf",
            "esdafasd",
            "fasdf",
            "gsadf",
            "gs",
            "f**k",
            "safsd",
            "sadfasd",
            "asddfasdf",
            "asdfasdf",
            "sadfe",
            "asdfasd",
            "erer",
            "rtert",
            "asdf",
            "4erwerw",
            "sawe23",

    };
    int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};

    int i = 0;
    for (; i < 200; i++) {
        map_set(m, test[i % 17], a + i % 17);
    }

    printf("Over\n");
    for (i = 0; i < 200; i++) {
        bucket* val = map_get(m, test[i % 17]);
        if (val == NULL) {
            printf("not found");
        } else {
//            printf(" %p %s %d\n", val,(char*)val->key, *((int*)(val->val)));
        }
    }
    map_print(m);
}
Ejemplo n.º 4
0
int astar_test_main()
{
	astar_t * as;
	int x0, y0, x1, y1;
	int result;

	srand(time(NULL));

	// Initialise the map.
	map_init();

	// Allocate an A* context.
	as = astar_new (WIDTH, HEIGHT, get_map_cost, NULL);

	// Set the map origin. This allows us to look for a route in
	// an area smaller than the full game map (by setting the
	// origin to the origin of that area, and giving astar_new() a
	// width and height smaller than the full map's. It's
	// mandatory to set this, even if it's just zero. If you don't set it,
	// the algorithm will fail with an ASTAR_ORIGIN_NOT_SET error.

	astar_set_origin (as, 0, 0);

	// By setting the steering penalty, you penalise direction
	// changes. This can lead in more direct routes, but it can also make
	// the algorithm work harder to find a route. The default pre-bundled
	// value for this cost is 5 (5 less than the cost of moving in the four
	// cardinal directions). Set it to zero, and the route can meander
	// more.

	// astar_set_steering_penalty (as, 5);

	// If you have a preference for movement in the cardinal directions
	// only, assign a high cost to the other four directions. If the only
	// way to get from A to B is to move dianogally, a diagonal move will
	// still be used though.

	// astar_set_cost (as, DIR_NE, 100);
	// astar_set_cost (as, DIR_NW, 100);
	// astar_set_cost (as, DIR_SW, 100);
	// astar_set_cost (as, DIR_SE, 100);

	// Instead, if you want to only route using the four cardinal
	// directions, say this:

	// astar_set_movement_mode (as, DIR_CARDINAL);
	// astar_set_movement_mode (as, DIR_8WAY); // This is the default.

	// Starting near the upper left corner of the map.
	x0 = 2;
	y0 = 1;

	// Destination near the lower right corner.
	x1 = WIDTH - 3;
	y1 = HEIGHT - 1;

	// Look for a route.
	result = astar_run (as, x0, y0, x1, y1);

	// What's the result?
	printf ("Route from (%d, %d) to (%d, %d). Result: %s (%d)\n",
		as->x0, as->y0,
		as->x1, as->y1,
		as->str_result, result);

	// Do we have a route? We don't care if the route is partial
	// or full here. If you need a full route, you must ensure the return
	// value of astar_run is ASTAR_FOUND. If it isn't, and
	// astar_have_route() returns non-zero, there's a (possibly partial)
	// route.

	if (astar_have_route (as)) {
		direction_t * directions, * dir;
		uint32_t i, num_steps;
		int x, y;

		num_steps = astar_get_directions (as, &directions);

		if (result == ASTAR_FOUND) {
			printf ("We have a route. It has %d step(s).\n", num_steps);
		} else {
			printf ("The best compromise route has %d step(s).\n", num_steps);
		}

		// The directions start at our (x0, y0) and give us
		// step-by-step directions (e.g. N, E, NE, N) to
		// either our (x1, y1) in the case of a full route, or
		// the best compromise.
		x = x0;
		y = y0;
		dir = directions;
		for (i = 0; i < num_steps; i++, dir++) {

			// Convince ourselves that A* never made us go through walls.
			assert (get_map (x, y) != MAP_WALL);

			// Mark the route on the map.
			set_map (x, y, MAP_ROUTE);

			// Get the next (x, y) co-ordinates of the map.
			x += astar_get_dx (as, *dir);
			y += astar_get_dy (as, *dir);
		}

		// Mark the starting and ending squares on the map. Do this last.
		set_map (x0, y0, MAP_START);
		set_map (x1, y1, MAP_END);

		// VERY IMPORTANT: MEMORY LEAKS OTHERWISE!
		astar_free_directions (directions);
	}

	// Print the map.
	map_print();

	// Dellocate the A* algorithm context.
	astar_destroy (as);
        return 0;
}
Ejemplo n.º 5
0
static int decode(FILE *in, FILE *out) {
    struct hexfile_decoder *hfd = NULL;
    struct decoder decoder = {
        .base = 0,
        .file = out,
        .eof = 0,
        .position = 0,
    };
    char buffer[4096];
    int ret;
    size_t nbytes;

    ret = hexfile_decoder_new(hfd_callback, &decoder, &hfd);
    if (ret < 0) {
        fprintf(stderr, "failed to create hexfile decoder\n");
        _exit(2);
    }

    while ((nbytes = fread(buffer, 1, sizeof(buffer), in)) > 0) {
        ret = hexfile_decoder_feed(hfd, buffer, nbytes);
        if (ret < 0) {
            fprintf(stderr, "failed to decode hexfile\n");
            _exit(2);
        }
    }

    ret = hexfile_decoder_close(&hfd);
    if (ret < 0) {
        fprintf(stderr, "failed to close hexfile decoder\n");
        _exit(2);
    }

    if (!decoder.eof) {
        fprintf(stderr, "no EOF record\n");
        _exit(1);
    }

    return 0;
}

struct mapper {
    unsigned base, start, end;
    FILE *file;
    int eof;
};

static void map_print(const struct mapper *mapper) {
    if (mapper->end > mapper->start)
        fprintf(mapper->file, "0x%08x-0x%08x\n", mapper->start, mapper->end);
}

static int map_callback(void *ctx,
                        unsigned char type, unsigned offset,
                        unsigned char *data, size_t length) {
    struct mapper *mapper = (struct mapper*)ctx;

    (void)data;

    if (mapper->eof) {
        errno = EINVAL;
        return -1;
    }

    if (type == 0x00) {
        /* data record */
        unsigned new_position, new_end;

        new_position = (mapper->base + offset) & ~0xf;
        new_end = (new_position + length) | 0xf;

        if (new_position < mapper->start || new_position > mapper->end + 1) {
            map_print(mapper);
            mapper->start = new_position;
            mapper->end = new_end;
        } else if (new_end > mapper->end) {
            mapper->end = new_end;
        }

        return 0;
    } else if (type == 0x01) {
        /* EOF record */
        mapper->eof = 1;
        return 0;
    } else if (type >= 0x10) {
        /* switch memory bank */
        unsigned new_base = (type - 0x10) * BANK_SIZE;

        if (mapper->end != new_base) {
            map_print(mapper);
            mapper->start = mapper->end = new_base;
        }

        mapper->base = new_base;
        return 0;
    } else {
        errno = ENOSYS;
        return -1;
    }
}

static int map(FILE *in, FILE *out) {
    struct hexfile_decoder *hfd = NULL;
    struct mapper mapper = {
        .base = 0,
        .start = 0,
        .end = 0,
        .file = out,
        .eof = 0,
    };
    char buffer[4096];
    int ret;
    size_t nbytes;

    ret = hexfile_decoder_new(map_callback, &mapper, &hfd);
    if (ret < 0) {
        fprintf(stderr, "failed to create hexfile decoder\n");
        _exit(2);
    }

    while ((nbytes = fread(buffer, 1, sizeof(buffer), in)) > 0) {
        ret = hexfile_decoder_feed(hfd, buffer, nbytes);
        if (ret < 0) {
            fprintf(stderr, "failed to decode hexfile\n");
            _exit(2);
        }
    }

    ret = hexfile_decoder_close(&hfd);
    if (ret < 0) {
        fprintf(stderr, "failed to close hexfile decoder\n");
        _exit(2);
    }

    if (!mapper.eof) {
        fprintf(stderr, "no EOF record\n");
        _exit(1);
    }

    return 0;
}

int main(int argc, char **argv) {
    struct config config;
    FILE *in, *out;

    parse_cmdline(&config, argc, argv);

    if (config.input_path == NULL) {
        in = stdin;
    } else {
        in = fopen(argv[optind], "r");
        if (in == NULL) {
            fprintf(stderr, "failed to open '%s': %s",
                    argv[optind], strerror(errno));
            _exit(1);
        }
    }

    if (config.output_path == NULL) {
        out = stdout;
    } else {
        out = fopen(config.output_path, "w");
        if (out == NULL) {
            fprintf(stderr, "failed to create '%s': %s\n",
                    config.output_path, strerror(errno));
            _exit(1);
        }
    }

    /* do it */
    if (config.decode) {
        return decode(in, out);
    } else if (config.map) {
        return map(in, out);
    } else {
        return encode(in, out);
    }
}