liblashgame

Pathfinder and path decision making library for 2D tile game
git clone git://holbrook.no/liblashgame.git
Log | Files | Refs

findpath.c (4401B)


      1 // test the pathfinder
      2 // this test should be split up for the add space and other individual items
      3 
      4 
      5 #include <stdlib.h>
      6 #include <stdio.h>
      7 #include <time.h>
      8 #include <string.h>
      9 #include "liblashgame/lash_game_path_simple.h"
     10 #include "liblashgame/lash_game_map.h"
     11 #include "liblashgame/lash_game_standard.h"
     12 #include "liblash/lash_tree3_dump.h"
     13 
     14 // segfault if input formats with commas aren't right
     15 // the y coords should be checked for valid return from strtok before atoi
     16 // does not check bounds of coords
     17 
     18 unsigned int _unitsize = 1;
     19 
     20 
     21 void dumpmappathlayer(lash_map_simple_t *map) {
     22 	lash_game_coords_t tmpcoords;
     23 	lash_game_map_index_t tmpidx;
     24 	for (tmpcoords.y = 0; tmpcoords.y < *map->h; tmpcoords.y++) {
     25 		for (tmpcoords.x = 0; tmpcoords.x < *map->w; tmpcoords.x++) {
     26 			lash_cartesianToIndex(&tmpidx, map->w, map->h, &_unitsize, &tmpcoords);
     27 			printf("%02i ", (int)lash_mapSimpleLayerPeek(map->layer_path, tmpidx));
     28 		}
     29 		printf("\n");
     30 	}
     31 	printf("\n");	
     32 }
     33 
     34 int main(int argc, char *argv[]) {
     35 	if (argc < 5) {
     36 		printf("Usage: %s world_width world_height startx,starty targetx,targety obstacleindexval[,obstacleoindexval[...]] [obstaclex,obstacley,obstacleitemval [obstaclex,obstacley,obstacleitemval ...]]\n", argv[0]);
     37 		return 1;
     38 	}
     39 	
     40 	int i;
     41 	
     42 	lash_game_coords_t offset_coords;
     43 	lash_game_coords_t target_coords;
     44 	lash_game_map_index_t offset_idx;
     45 	lash_game_map_index_t target_idx;
     46 	lash_game_map_index_t *obstacles;
     47 	
     48 	unsigned int w = atoi(argv[1]);
     49 	unsigned int h = atoi(argv[2]);
     50 	
     51 	offset_coords.x = atoi(strtok(argv[3], ","));
     52 	offset_coords.y = atoi(strtok(NULL, ","));
     53 	lash_cartesianToIndex(&offset_idx, &w, &h, &_unitsize, &offset_coords);
     54 	
     55 	target_coords.x = atoi(strtok(argv[4], ","));
     56 	target_coords.y = atoi(strtok(NULL, ","));
     57 	lash_cartesianToIndex(&target_idx, &w, &h, &_unitsize, &target_coords);
     58 
     59 	printf("World: w%d x h%d\nOffset: x%d,y%d (idx %d)\nTarget: x%d,y%d (idx %d)\n", w, h, offset_coords.x, offset_coords.y, (int)offset_idx, target_coords.x, target_coords.y, (int)target_idx);
     60 	printf("Obstacle definitions: ");
     61 
     62 	int obstaclemastercount = 0;
     63 	char *tmpobstaclemastersource = (char*)malloc(strlen(argv[5]));
     64 	strcpy(tmpobstaclemastersource, argv[5]);
     65 	char *tmpobstaclemaster = strtok(tmpobstaclemastersource, ",");
     66 	while (tmpobstaclemaster != NULL) {
     67 		obstaclemastercount++;
     68 		tmpobstaclemaster = strtok(NULL, ",");
     69 	}
     70 	
     71 	lash_path_simple_obstacle_t *obstaclemasterlist = (lash_path_simple_obstacle_t*)malloc(sizeof(lash_path_simple_obstacle_t) * obstaclemastercount);
     72 	i=0;
     73 	strcpy(tmpobstaclemastersource, argv[5]);
     74 	char *tmpobstaclemaster2 = strtok(tmpobstaclemastersource, ",");
     75 	while (tmpobstaclemaster2 != NULL) {
     76 		(obstaclemasterlist + i)->val = atoi(tmpobstaclemaster2);
     77 		(obstaclemasterlist + i)->modifier = LASH_GAME_PATH_SIMPLE_OBSTACLE_MODIFIER_FULL;
     78 		printf("%d ", (obstaclemasterlist + i)->val);
     79 		tmpobstaclemaster2 = strtok(NULL, ",");
     80 		i++;
     81 	}
     82 	free(tmpobstaclemastersource);
     83 	printf("\n");
     84 	
     85 
     86 	lash_map_simple_t map;
     87 	lash_mapSimpleInit(&map, &w, &h);
     88 
     89 	if (argc > 6) {
     90 		obstacles = (lash_game_map_index_t*)malloc(sizeof(lash_game_map_index_t) * (argc - 6));
     91 		
     92 		for (i = 0; i < argc - 6; i++) {
     93 			lash_game_coords_t obstacle_coords;	
     94 			lash_map_simple_layer_item_t obstacleval;
     95 			obstacle_coords.x = atoi(strtok(argv[i + 6], ","));
     96 			obstacle_coords.y = atoi(strtok(NULL, ","));
     97 			obstacleval = atoi(strtok(NULL, ","));
     98 			lash_cartesianToIndex((obstacles + i), &w, &h, &_unitsize, &obstacle_coords);
     99 			lash_mapSimpleLayerPoke(map.layer_path, *(obstacles +i), obstacleval);
    100 			printf("Obstacle #%d: x%d,y%d (idx %d) of type %d\n", i + 1, obstacle_coords.x, obstacle_coords.y, (int)*(obstacles + i), obstacleval);
    101 		}
    102 	}
    103 	
    104 	lash_path_simple_t path;
    105 	lash_path_simple_space_t *space;
    106 	
    107 	lash_pathSimpleNew(&path, &map, offset_idx, target_idx);
    108 	
    109 	lash_treeDumpInit(1);
    110 	lash_treeDumpAdd(path.closedtree, "closedtree");
    111 	
    112 	lash_pathSimpleNext(&path, &space);
    113 
    114 	printf("Map path layer dump:\n");
    115 	dumpmappathlayer(&map);
    116 	printf("---\nTrying step...\n");
    117 	i = 0;
    118 	
    119 	while (space->index != path.target) {
    120 		
    121 		printf("Step %d with opentree index %p spaceindex %d target %d\n", ++i, *path.opentree->key, (int)space->index, (int)path.target);
    122 		lash_pathSimpleStepProcess(&path, &map, space, obstaclemasterlist, obstaclemastercount);
    123 		lash_pathSimpleNext(&path, &space);
    124 		if (space == NULL)
    125 			break;
    126 	}
    127 	
    128 	return 0;
    129 }