project_files/frontlib/test.c.nocompile
changeset 7275 15f722e0b96f
parent 7271 5608ac657362
child 7314 6171f0bad318
equal deleted inserted replaced
7273:8eed495fd8da 7275:15f722e0b96f
       
     1 #include "frontlib.h"
       
     2 #include "util/logging.h"
       
     3 #include "util/buffer.h"
       
     4 #include "util/util.h"
       
     5 #include "util/list.h"
       
     6 #include "model/map.h"
       
     7 #include "model/weapon.h"
       
     8 #include "model/schemelist.h"
       
     9 #include "ipc/mapconn.h"
       
    10 #include "ipc/gameconn.h"
       
    11 #include "net/netconn.h"
       
    12 
       
    13 #include <stdlib.h>
       
    14 #include <stdbool.h>
       
    15 #include <assert.h>
       
    16 #include <string.h>
       
    17 
       
    18 // Callback function that will be called when the map is rendered
       
    19 static void handleMapSuccess(void *context, const uint8_t *bitmap, int numHedgehogs) {
       
    20 	printf("Drawing map for %i brave little hogs...", numHedgehogs);
       
    21 
       
    22 	// Draw the map as ASCII art
       
    23 	for(int y=0; y<MAPIMAGE_HEIGHT; y++) {
       
    24 		for(int x=0; x<MAPIMAGE_WIDTH; x++) {
       
    25 			int pixelnum = x + y*MAPIMAGE_WIDTH;
       
    26 			bool pixel = bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7)));
       
    27 			printf(pixel ? "#" : " ");
       
    28 		}
       
    29 		printf("\n");
       
    30 	}
       
    31 
       
    32 	// Destroy the connection object (this will end the "tick" loop below)
       
    33 	flib_mapconn **connptr = context;
       
    34 	flib_mapconn_destroy(*connptr);
       
    35 	*connptr = NULL;
       
    36 }
       
    37 
       
    38 static void onDisconnect(void *context, int reason) {
       
    39 	flib_log_i("Connection closed. Reason: %i", reason);
       
    40 	flib_gameconn **connptr = context;
       
    41 	flib_gameconn_destroy(*connptr);
       
    42 	*connptr = NULL;
       
    43 }
       
    44 
       
    45 static void onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {
       
    46 	flib_log_i("Writing %s (%i bytes)...", isSavegame ? "savegame" : "demo", size);
       
    47 	FILE *file = fopen(isSavegame ? "testsave.42.hws" : "testdemo.42.hwd", "wb");
       
    48 	fwrite(record, 1, size, file);
       
    49 	fclose(file);
       
    50 }
       
    51 
       
    52 // Callback function that will be called on error
       
    53 static void handleMapFailure(void *context, const char *errormessage) {
       
    54 	flib_log_e("Map rendering failed: %s", errormessage);
       
    55 
       
    56 	// Destroy the connection object (this will end the "tick" loop below)
       
    57 	flib_mapconn **connptr = context;
       
    58 	flib_mapconn_destroy(*connptr);
       
    59 	*connptr = NULL;
       
    60 }
       
    61 
       
    62 static void startEngineMap(int port) {
       
    63 	char commandbuffer[255];
       
    64 	const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
       
    65 	const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
       
    66 	snprintf(commandbuffer, 255, "start %s\\hwengine.exe %s %i landpreview", enginePath, configPath, port);
       
    67 	system(commandbuffer);
       
    68 }
       
    69 
       
    70 static void startEngineGame(int port) {
       
    71 	char commandbuffer[255];
       
    72 	const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
       
    73 	const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
       
    74 	const char *dataPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars\\Data";
       
    75 	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);
       
    76 	flib_log_d("Starting engine with CMD: %s", commandbuffer);
       
    77 	system(commandbuffer);
       
    78 }
       
    79 
       
    80 void testMapPreview() {
       
    81 	// Create a map description and check that there was no error
       
    82 	flib_map *map = flib_map_create_maze("This is the seed value", "Jungle", MAZE_SIZE_SMALL_TUNNELS);
       
    83 	assert(map);
       
    84 
       
    85 	// Create a new connection to the engine and check that there was no error
       
    86 	flib_mapconn *mapConnection = flib_mapconn_create(map);
       
    87 	assert(mapConnection);
       
    88 
       
    89 	// We don't need the map description anymore
       
    90 	flib_map_release(map);
       
    91 	map = NULL;
       
    92 
       
    93 	// Register the callback functions
       
    94 	flib_mapconn_onFailure(mapConnection, &handleMapFailure, &mapConnection);
       
    95 	flib_mapconn_onSuccess(mapConnection, &handleMapSuccess, &mapConnection);
       
    96 
       
    97 	// Start the engine process and tell it which port the frontlib is listening on
       
    98 	startEngineMap(flib_mapconn_getport(mapConnection));
       
    99 
       
   100 	// Usually, flib_mapconn_tick will be called in an event loop that runs several
       
   101 	// times per second. It handles I/O operations and progress, and calls
       
   102 	// callbacks when something interesting happens.
       
   103 	while(mapConnection) {
       
   104 		flib_mapconn_tick(mapConnection);
       
   105 	}
       
   106 }
       
   107 
       
   108 /*void testGame() {
       
   109 	flib_cfg_meta *metaconf = flib_cfg_meta_from_ini("metasettings.ini");
       
   110 	assert(metaconf);
       
   111 	flib_weaponset *weapons = flib_weaponset_create("Defaultweaps");
       
   112 	flib_schemelist *schemelist = flib_schemelist_from_ini(metaconf, "schemes.ini");
       
   113 
       
   114 	flib_gamesetup setup;
       
   115 	setup.gamescheme = flib_schemelist_find(schemelist, "Default");
       
   116 	setup.map = flib_map_create_maze("asparagus", "Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
       
   117 	setup.script = NULL;
       
   118 	setup.teamCount = 2;
       
   119 	setup.teams = calloc(2, sizeof(flib_team*));
       
   120 	setup.teams[0] = calloc(1, sizeof(flib_team));
       
   121 	setup.teams[0]->color = 0xffff0000;
       
   122 	setup.teams[0]->flag = "australia";
       
   123 	setup.teams[0]->fort = "Plane";
       
   124 	setup.teams[0]->grave = "Bone";
       
   125 	setup.teams[0]->hogsInGame = 2;
       
   126 	setup.teams[0]->name = "Team Awesome";
       
   127 	setup.teams[0]->voicepack = "British";
       
   128 	setup.teams[0]->hogs[0].difficulty = 2;
       
   129 	setup.teams[0]->hogs[0].hat = "NoHat";
       
   130 	setup.teams[0]->hogs[0].initialHealth = 100;
       
   131 	setup.teams[0]->hogs[0].name = "Harry 120";
       
   132 	setup.teams[0]->hogs[1].difficulty = 2;
       
   133 	setup.teams[0]->hogs[1].hat = "chef";
       
   134 	setup.teams[0]->hogs[1].initialHealth = 100;
       
   135 	setup.teams[0]->hogs[1].name = "Chefkoch";
       
   136 	setup.teams[1] = flib_team_from_ini("Cave Dwellers.hwt");
       
   137 	setup.teams[1]->color = 0xFF0000F0;
       
   138 	setup.teams[1]->hogsInGame = 8;
       
   139 	flib_team_set_weaponset(setup.teams[0], weapons);
       
   140 	flib_team_set_weaponset(setup.teams[1], weapons);
       
   141 	flib_weaponset_release(weapons);
       
   142 
       
   143 	flib_gameconn *gameconn = flib_gameconn_create("Medo42", &setup, false);
       
   144 	assert(gameconn);
       
   145 
       
   146 	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
       
   147 	//flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
       
   148 
       
   149 	startEngineGame(flib_gameconn_getport(gameconn));
       
   150 
       
   151 	while(gameconn) {
       
   152 		flib_gameconn_tick(gameconn);
       
   153 	}
       
   154 }*/
       
   155 
       
   156 void testDemo() {
       
   157 	FILE *demofile = fopen("testdemo.42.hwd", "rb");
       
   158 	assert(demofile);
       
   159 	flib_vector *vec = flib_vector_create();
       
   160 	uint8_t demobuf[512];
       
   161 	int len;
       
   162 	while((len=fread(demobuf, 1, 512, demofile))>0) {
       
   163 		flib_vector_append(vec, demobuf, len);
       
   164 	}
       
   165 	fclose(demofile);
       
   166 	flib_constbuffer constbuf = flib_vector_as_constbuffer(vec);
       
   167 	flib_gameconn *gameconn = flib_gameconn_create_playdemo(constbuf.data, constbuf.size);
       
   168 	flib_vector_destroy(vec);
       
   169 	assert(gameconn);
       
   170 	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
       
   171 	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
       
   172 	startEngineGame(flib_gameconn_getport(gameconn));
       
   173 
       
   174 	while(gameconn) {
       
   175 		flib_gameconn_tick(gameconn);
       
   176 	}
       
   177 }
       
   178 
       
   179 void testSave() {
       
   180 	FILE *demofile = fopen("testsave.42.hws", "rb");
       
   181 	assert(demofile);
       
   182 	flib_vector *vec = flib_vector_create();
       
   183 	uint8_t demobuf[512];
       
   184 	int len;
       
   185 	while((len=fread(demobuf, 1, 512, demofile))>0) {
       
   186 		flib_vector_append(vec, demobuf, len);
       
   187 	}
       
   188 	fclose(demofile);
       
   189 	flib_constbuffer constbuf = flib_vector_as_constbuffer(vec);
       
   190 	flib_gameconn *gameconn = flib_gameconn_create_loadgame("Medo42", constbuf.data, constbuf.size);
       
   191 	flib_vector_destroy(vec);
       
   192 	assert(gameconn);
       
   193 	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
       
   194 	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
       
   195 	startEngineGame(flib_gameconn_getport(gameconn));
       
   196 
       
   197 	while(gameconn) {
       
   198 		flib_gameconn_tick(gameconn);
       
   199 	}
       
   200 }
       
   201 
       
   202 void handleNetDisconnect(void *context, int reason, const char *message) {
       
   203 	flib_log_i("Disconnected: %s", message);
       
   204 	flib_netconn_destroy(*(flib_netconn**)context);
       
   205 	*(flib_netconn**)context = NULL;
       
   206 }
       
   207 
       
   208 void handleNetConnected(void *context) {
       
   209 	const flib_roomlist *roomlist = flib_netconn_get_roomlist(*(flib_netconn**)context);
       
   210 	flib_log_i("List of rooms:");
       
   211 	for(int i=0; i<roomlist->roomCount; i++) {
       
   212 		flib_roomlist_room *room = roomlist->rooms[i];
       
   213 		flib_log_i("%1s %20s %20s %2i %2i %20s %20s %20s", room->inProgress ? "X" : " ", room->name, room->owner, room->playerCount, room->teamCount, room->map, room->scheme, room->weapons);
       
   214 	}
       
   215 	flib_netconn_send_joinRoom(*(flib_netconn**)context, "frontlib test");
       
   216 }
       
   217 
       
   218 void handleLobbyJoin(void *context, const char *nick) {
       
   219 	flib_log_i("%s joined", nick);
       
   220 }
       
   221 
       
   222 void handleChat(void *context, const char *nick, const char *msg) {
       
   223 	flib_log_i("%s: %s", nick, msg);
       
   224 	if(!memcmp("frontbot ", msg, strlen("frontbot "))) {
       
   225 		const char *command = msg+strlen("frontbot ");
       
   226 		if(!memcmp("quit", command, strlen("quit"))) {
       
   227 			flib_netconn_send_quit(*(flib_netconn**)context, "Yeth Mathter");
       
   228 		} else if(!memcmp("describe ", command, strlen("describe "))) {
       
   229 			const char *roomname = command+strlen("describe ");
       
   230 			const flib_roomlist *roomlist = flib_netconn_get_roomlist(*(flib_netconn**)context);
       
   231 			flib_roomlist_room *room = flib_roomlist_find((flib_roomlist*)roomlist, roomname);
       
   232 			if(!room) {
       
   233 				flib_netconn_send_chat(*(flib_netconn**)context, "Unknown room.");
       
   234 			} else {
       
   235 				char *text = flib_asprintf(
       
   236 						"%s is a room created by %s, where %i players (%i teams) are %s on %s%s, using the %s scheme and %s weaponset.",
       
   237 						room->name,
       
   238 						room->owner,
       
   239 						room->playerCount,
       
   240 						room->teamCount,
       
   241 						room->inProgress ? "fighting" : "preparing to fight",
       
   242 						room->map[0]=='+' ? "" : "the map ",
       
   243 						!strcmp("+rnd+", room->map) ? "a random map" :
       
   244 								!strcmp("+maze+", room->map) ? "a random maze" :
       
   245 								!strcmp("+drawn+", room->map) ? "a hand-drawn map" :
       
   246 								room->map,
       
   247 						room->scheme,
       
   248 						room->weapons);
       
   249 				if(text) {
       
   250 					flib_netconn_send_chat(*(flib_netconn**)context, text);
       
   251 				}
       
   252 				free(text);
       
   253 			}
       
   254 		} else if(!memcmp("join ", command, strlen("join "))) {
       
   255 			const char *roomname = command+strlen("join ");
       
   256 			flib_netconn_send_joinRoom(*(flib_netconn**)context, roomname);
       
   257 		} else if(!memcmp("ready", command, strlen("ready"))) {
       
   258 			flib_netconn_send_toggleReady(*(flib_netconn**)context);
       
   259 		}
       
   260 	}
       
   261 }
       
   262 
       
   263 void handleEnterRoom(void *context, bool isChief) {
       
   264 	flib_netconn_send_toggleReady(*(flib_netconn**)context);
       
   265 }
       
   266 
       
   267 flib_gameconn *gameconn = NULL;
       
   268 
       
   269 void emFromNetHandler(void *context, const char *em, int size) {
       
   270 	flib_gameconn_send_enginemsg(gameconn, (const uint8_t*)em, size);
       
   271 }
       
   272 
       
   273 void emFromEngineHandler(void *context, const uint8_t *em, int size) {
       
   274 	flib_netconn_send_engineMessage((flib_netconn*)context, em, size);
       
   275 }
       
   276 
       
   277 void handleRunGame(void *context) {
       
   278 	flib_gamesetup *gamesetup = flib_netconn_create_gameSetup((flib_netconn*)context);
       
   279 	if(gamesetup) {
       
   280 		gameconn = flib_gameconn_create("frontbot", gamesetup, true);
       
   281 		flib_gameconn_onEngineMessage(gameconn, emFromEngineHandler, context);
       
   282 		flib_gameconn_onDisconnect(gameconn, onDisconnect, &gameconn);
       
   283 		startEngineGame(flib_gameconn_getport(gameconn));
       
   284 	}
       
   285 }
       
   286 
       
   287 int main(int argc, char *argv[]) {
       
   288 	flib_init(0);
       
   289 	flib_log_setLevel(FLIB_LOGLEVEL_ALL);
       
   290 
       
   291 	//testMapPreview();
       
   292 	//testDemo();
       
   293 	//testSave();
       
   294 	//testGame();
       
   295 
       
   296 	flib_cfg_meta *meta = flib_cfg_meta_from_ini("metasettings.ini");
       
   297 	assert(meta);
       
   298 	flib_netconn *conn = flib_netconn_create("frontbot", meta, "140.247.62.101", 46631);
       
   299 	assert(conn);
       
   300 	flib_cfg_meta_release(meta);
       
   301 
       
   302 	flib_netconn_onConnected(conn, handleNetConnected, &conn);
       
   303 	flib_netconn_onDisconnected(conn, handleNetDisconnect, &conn);
       
   304 	flib_netconn_onLobbyJoin(conn, handleLobbyJoin, &conn);
       
   305 	flib_netconn_onChat(conn, handleChat, &conn);
       
   306 	flib_netconn_onEnterRoom(conn, handleEnterRoom, conn);
       
   307 	flib_netconn_onRunGame(conn, handleRunGame, conn);
       
   308 	flib_netconn_onEngineMessage(conn, emFromNetHandler, NULL);
       
   309 
       
   310 	while(conn) {
       
   311 		flib_netconn_tick(conn);
       
   312 		if(gameconn) {
       
   313 			flib_gameconn_tick(gameconn);
       
   314 		}
       
   315 	}
       
   316 
       
   317 	flib_quit();
       
   318 	return 0;
       
   319 }