project_files/frontlib/test.c
changeset 7224 5143861c83bd
child 7227 1c859f572d72
equal deleted inserted replaced
7221:8d04e85ca204 7224:5143861c83bd
       
     1 #include "frontlib.h"
       
     2 #include "util/logging.h"
       
     3 #include "model/map.h"
       
     4 #include "ipc/mapconn.h"
       
     5 #include "ipc/gameconn.h"
       
     6 
       
     7 #include <stdlib.h>
       
     8 #include <stdbool.h>
       
     9 #include <assert.h>
       
    10 
       
    11 // Callback function that will be called when the map is rendered
       
    12 static void handleMapSuccess(void *context, const uint8_t *bitmap, int numHedgehogs) {
       
    13 	printf("Drawing map for %i brave little hogs...", numHedgehogs);
       
    14 
       
    15 	// Draw the map as ASCII art
       
    16 	for(int y=0; y<MAPIMAGE_HEIGHT; y++) {
       
    17 		for(int x=0; x<MAPIMAGE_WIDTH; x++) {
       
    18 			int pixelnum = x + y*MAPIMAGE_WIDTH;
       
    19 			bool pixel = bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7)));
       
    20 			printf(pixel ? "#" : " ");
       
    21 		}
       
    22 		printf("\n");
       
    23 	}
       
    24 
       
    25 	// Destroy the connection object (this will end the "tick" loop below)
       
    26 	flib_mapconn **connptr = context;
       
    27 	flib_mapconn_destroy(*connptr);
       
    28 	*connptr = NULL;
       
    29 }
       
    30 
       
    31 static void onDisconnect(void *context, int reason) {
       
    32 	flib_log_i("Connection closed. Reason: %i", reason);
       
    33 	flib_gameconn **connptr = context;
       
    34 	flib_gameconn_destroy(*connptr);
       
    35 	*connptr = NULL;
       
    36 }
       
    37 
       
    38 static void onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {
       
    39 	flib_log_i("Writing %s (%i bytes)...", isSavegame ? "savegame" : "demo", size);
       
    40 	FILE *file = fopen(isSavegame ? "testsave.42.hws" : "testdemo.42.hwd", "wb");
       
    41 	fwrite(record, 1, size, file);
       
    42 	fclose(file);
       
    43 }
       
    44 
       
    45 // Callback function that will be called on error
       
    46 static void handleMapFailure(void *context, const char *errormessage) {
       
    47 	flib_log_e("Map rendering failed: %s", errormessage);
       
    48 
       
    49 	// Destroy the connection object (this will end the "tick" loop below)
       
    50 	flib_mapconn **connptr = context;
       
    51 	flib_mapconn_destroy(*connptr);
       
    52 	*connptr = NULL;
       
    53 }
       
    54 
       
    55 static void startEngineMap(int port) {
       
    56 	char commandbuffer[255];
       
    57 	const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
       
    58 	const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
       
    59 	snprintf(commandbuffer, 255, "start %s\\hwengine.exe %s %i landpreview", enginePath, configPath, port);
       
    60 	system(commandbuffer);
       
    61 }
       
    62 
       
    63 static void startEngineGame(int port) {
       
    64 	char commandbuffer[255];
       
    65 	const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
       
    66 	const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
       
    67 	const char *dataPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars\\Data";
       
    68 	snprintf(commandbuffer, 255, "start %s\\hwengine.exe %s 1024 768 32 %i 0 0 0 10 10 %s 0 0 TWVkbzQy 0 0 en.txt", enginePath, configPath, port, dataPath);
       
    69 	flib_log_d("Starting engine with CMD: %s", commandbuffer);
       
    70 	system(commandbuffer);
       
    71 }
       
    72 
       
    73 void testMapPreview() {
       
    74 	// Create a map description and check that there was no error
       
    75 	flib_map *map = flib_map_create_maze("Jungle", MAZE_SIZE_SMALL_TUNNELS);
       
    76 	assert(map);
       
    77 
       
    78 	// Create a new connection to the engine and check that there was no error
       
    79 	flib_mapconn *mapConnection = flib_mapconn_create("This is the seed value", map);
       
    80 	assert(mapConnection);
       
    81 
       
    82 	// We don't need the map description anymore
       
    83 	flib_map_destroy(map);
       
    84 	map = NULL;
       
    85 
       
    86 	// Register the callback functions
       
    87 	flib_mapconn_onFailure(mapConnection, &handleMapFailure, &mapConnection);
       
    88 	flib_mapconn_onSuccess(mapConnection, &handleMapSuccess, &mapConnection);
       
    89 
       
    90 	// Start the engine process and tell it which port the frontlib is listening on
       
    91 	startEngineMap(flib_mapconn_getport(mapConnection));
       
    92 
       
    93 	// Usually, flib_mapconn_tick will be called in an event loop that runs several
       
    94 	// times per second. It handles I/O operations and progress, and calls
       
    95 	// callbacks when something interesting happens.
       
    96 	while(mapConnection) {
       
    97 		flib_mapconn_tick(mapConnection);
       
    98 	}
       
    99 }
       
   100 
       
   101 void testGame() {
       
   102 	flib_cfg_meta *metaconf = flib_cfg_meta_from_ini("basicsettings.ini", "gamemods.ini");
       
   103 	assert(metaconf);
       
   104 	flib_gamesetup setup;
       
   105 	setup.gamescheme = flib_cfg_from_ini(metaconf, "scheme_shoppa.ini");
       
   106 	setup.map = flib_map_create_maze("Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
       
   107 	setup.seed = "apsfooasdgnds";
       
   108 	setup.script = NULL;
       
   109 	setup.teamcount = 2;
       
   110 	setup.teams = calloc(2, sizeof(flib_team*));
       
   111 	setup.teams[0] = calloc(1, sizeof(flib_team));
       
   112 	setup.teams[0]->color = 0xffff0000;
       
   113 	setup.teams[0]->flag = "australia";
       
   114 	setup.teams[0]->fort = "Plane";
       
   115 	setup.teams[0]->grave = "Bone";
       
   116 	setup.teams[0]->hogsInGame = 2;
       
   117 	setup.teams[0]->name = "Team Awesome";
       
   118 	setup.teams[0]->voicepack = "British";
       
   119 	setup.teams[0]->weaponset = flib_weaponset_create("Defaultweaps");
       
   120 	setup.teams[0]->hogs[0].difficulty = 2;
       
   121 	setup.teams[0]->hogs[0].hat = "NoHat";
       
   122 	setup.teams[0]->hogs[0].initialHealth = 100;
       
   123 	setup.teams[0]->hogs[0].name = "Harry 120";
       
   124 	setup.teams[0]->hogs[1].difficulty = 2;
       
   125 	setup.teams[0]->hogs[1].hat = "chef";
       
   126 	setup.teams[0]->hogs[1].initialHealth = 100;
       
   127 	setup.teams[0]->hogs[1].name = "Chefkoch";
       
   128 	setup.teams[1] = flib_team_from_ini("Cave Dwellers.hwt");
       
   129 	setup.teams[1]->color = 0xff0000ff;
       
   130 	setup.teams[1]->hogsInGame = 8;
       
   131 	setup.teams[1]->weaponset = flib_weaponset_create("Defaultweaps");
       
   132 
       
   133 	flib_gameconn *gameconn = flib_gameconn_create("Medo42", metaconf, &setup, false);
       
   134 	assert(gameconn);
       
   135 
       
   136 	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
       
   137 	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
       
   138 
       
   139 	startEngineGame(flib_gameconn_getport(gameconn));
       
   140 
       
   141 	while(gameconn) {
       
   142 		flib_gameconn_tick(gameconn);
       
   143 	}
       
   144 }
       
   145 
       
   146 void testDemo() {
       
   147 	FILE *demofile = fopen("testdemo.42.hwd", "rb");
       
   148 	assert(demofile);
       
   149 	flib_vector *vec = flib_vector_create();
       
   150 	uint8_t demobuf[512];
       
   151 	int len;
       
   152 	while((len=fread(demobuf, 1, 512, demofile))>0) {
       
   153 		flib_vector_append(vec, demobuf, len);
       
   154 	}
       
   155 	fclose(demofile);
       
   156 	flib_constbuffer constbuf = flib_vector_as_constbuffer(vec);
       
   157 	flib_gameconn *gameconn = flib_gameconn_create_playdemo(constbuf.data, constbuf.size);
       
   158 	flib_vector_destroy(vec);
       
   159 	assert(gameconn);
       
   160 	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
       
   161 	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
       
   162 	startEngineGame(flib_gameconn_getport(gameconn));
       
   163 
       
   164 	while(gameconn) {
       
   165 		flib_gameconn_tick(gameconn);
       
   166 	}
       
   167 }
       
   168 
       
   169 int main(int argc, char *argv[]) {
       
   170 	flib_init(0);
       
   171 	flib_log_setLevel(FLIB_LOGLEVEL_ALL);
       
   172 
       
   173 	//testMapPreview();
       
   174 	testDemo();
       
   175 
       
   176 	flib_quit();
       
   177 	return 0;
       
   178 }