project_files/frontlib/frontlib.c
changeset 7179 f84805e6df03
parent 7177 bf6cf4dd847a
child 7182 076aba32abd3
equal deleted inserted replaced
7177:bf6cf4dd847a 7179:f84805e6df03
     1 #include "frontlib.h"
     1 #include "frontlib.h"
     2 #include "logging.h"
     2 #include "util/logging.h"
     3 #include "model/map.h"
     3 #include "model/map.h"
     4 #include "ipc/mapconn.h"
     4 #include "ipc/mapconn.h"
     5 #include "ipc.h"
     5 #include "ipc/gameconn.h"
     6 
     6 
     7 #include <SDL.h>
     7 #include <SDL.h>
     8 #include <SDL_net.h>
     8 #include <SDL_net.h>
     9 #include <stdio.h>
     9 #include <stdio.h>
    10 #include <stdint.h>
    10 #include <stdint.h>
    39 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    39 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    40 		SDL_Quit();
    40 		SDL_Quit();
    41 	}
    41 	}
    42 }
    42 }
    43 
    43 
    44 static void onConfigQuery(void *context) {
    44 static void onDisconnect(void *context, int reason) {
    45 	flib_log_i("Sending config...");
    45 	flib_log_i("Connection closed. Reason: %i", reason);
    46 	flib_ipc ipc = (flib_ipc)context;
    46 	flib_gameconn **connptr = context;
    47 	flib_ipc_send_messagestr(ipc, "TL");
    47 	flib_gameconn_destroy(*connptr);
    48 	flib_ipc_send_messagestr(ipc, "eseed loremipsum");
       
    49 	flib_ipc_send_messagestr(ipc, "e$mapgen 0");
       
    50 	flib_ipc_send_messagestr(ipc, "e$template_filter 0");
       
    51 	flib_ipc_send_messagestr(ipc, "etheme Jungle");
       
    52 	flib_ipc_send_messagestr(ipc, "eaddteam 11111111111111111111111111111111 255 Medo42");
       
    53 }
       
    54 
       
    55 static void onDisconnect(void *context) {
       
    56 	flib_log_i("Connection closed.");
       
    57 	flib_ipc_destroy((flib_ipc*)context);
       
    58 }
       
    59 
       
    60 static void onGameEnd(void *context, int gameEndType) {
       
    61 	switch(gameEndType) {
       
    62 	case GAME_END_FINISHED:
       
    63 		flib_log_i("Game finished.");
       
    64 		flib_constbuffer demobuf = flib_ipc_getdemo(context);
       
    65 		flib_log_i("Writing demo (%u bytes)...", (unsigned)demobuf.size);
       
    66 		FILE *file = fopen("testdemo.dem", "wb");
       
    67 		fwrite(demobuf.data, 1, demobuf.size, file);
       
    68 		fclose(file);
       
    69 		file = NULL;
       
    70 		break;
       
    71 	case GAME_END_HALTED:
       
    72 		flib_log_i("Game halted.");
       
    73 		break;
       
    74 	case GAME_END_INTERRUPTED:
       
    75 		flib_log_i("Game iterrupted.");
       
    76 		break;
       
    77 	}
       
    78 }
       
    79 
       
    80 static void handleMapSuccess(void *context, const uint8_t *bitmap, int numHedgehogs) {
       
    81 	printf("Drawing map for %i brave little hogs...", numHedgehogs);
       
    82 	int pixelnum = 0;
       
    83 	for(int y=0; y<MAPIMAGE_HEIGHT; y++) {
       
    84 		for(int x=0; x<MAPIMAGE_WIDTH; x++) {
       
    85 			if(bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7)))) {
       
    86 				printf("#");
       
    87 			} else {
       
    88 				printf(" ");
       
    89 			}
       
    90 			pixelnum++;
       
    91 		}
       
    92 		printf("\n");
       
    93 	}
       
    94 
       
    95 	flib_mapconn **connptr = context;
       
    96 	flib_mapconn_destroy(*connptr);
       
    97 	*connptr = NULL;
    48 	*connptr = NULL;
    98 }
    49 }
    99 
    50 
   100 static void handleMapFailure(void *context, const char *errormessage) {
    51 static void onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {
   101 	flib_log_e("Map rendering failed: %s", errormessage);
    52 	flib_log_i("Writing %s (%i bytes)...", isSavegame ? "savegame" : "demo", size);
   102 
    53 	FILE *file = fopen(isSavegame ? "testsave.42.hws" : "testdemo.42.hwd", "wb");
   103 	flib_mapconn **connptr = context;
    54 	fwrite(record, 1, size, file);
   104 	flib_mapconn_destroy(*connptr);
    55 	fclose(file);
   105 	*connptr = NULL;
       
   106 }
    56 }
   107 
    57 
   108 int main(int argc, char *argv[]) {
    58 int main(int argc, char *argv[]) {
   109 /*	flib_init(0);
    59 	flib_init(0);
   110 
    60 
   111 	flib_cfg_meta *meta = flib_cfg_meta_from_ini("basicsettings.ini", "gamemods.ini");
    61 	flib_cfg_meta *metaconf = flib_cfg_meta_from_ini("basicsettings.ini", "gamemods.ini");
   112 	flib_cfg *cfg = flib_cfg_create(meta, "DefaultScheme");
    62 	assert(metaconf);
   113 	flib_cfg_to_ini(meta, "defaulttest.ini", cfg);
    63 	flib_gamesetup setup;
       
    64 	setup.gamescheme = flib_cfg_from_ini(metaconf, "scheme_shoppa.ini");
       
    65 	setup.map = flib_map_create_maze("Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
       
    66 	setup.seed = "apsfooasdgnds";
       
    67 	setup.teamcount = 2;
       
    68 	setup.teams = calloc(2, sizeof(flib_team));
       
    69 	setup.teams[0].color = 0xffff0000;
       
    70 	setup.teams[0].flag = "australia";
       
    71 	setup.teams[0].fort = "Plane";
       
    72 	setup.teams[0].grave = "Bone";
       
    73 	setup.teams[0].hogsInGame = 2;
       
    74 	setup.teams[0].name = "Team Awesome";
       
    75 	setup.teams[0].voicepack = "British";
       
    76 	setup.teams[0].weaponset = flib_weaponset_create("Defaultweaps");
       
    77 	setup.teams[0].hogs[0].difficulty = 2;
       
    78 	setup.teams[0].hogs[0].hat = "NoHat";
       
    79 	setup.teams[0].hogs[0].initialHealth = 100;
       
    80 	setup.teams[0].hogs[0].name = "Harry 120";
       
    81 	setup.teams[0].hogs[1].difficulty = 2;
       
    82 	setup.teams[0].hogs[1].hat = "chef";
       
    83 	setup.teams[0].hogs[1].initialHealth = 100;
       
    84 	setup.teams[0].hogs[1].name = "Chefkoch";
       
    85 	setup.teams[1].color = 0xff0000ff;
       
    86 	setup.teams[1].flag = "germany";
       
    87 	setup.teams[1].fort = "Cake";
       
    88 	setup.teams[1].grave = "Cherry";
       
    89 	setup.teams[1].hogsInGame = 2;
       
    90 	setup.teams[1].name = "The Krauts";
       
    91 	setup.teams[1].voicepack = "Pirate";
       
    92 	setup.teams[1].weaponset = flib_weaponset_create("Defaultweaps");
       
    93 	setup.teams[1].hogs[0].difficulty = 0;
       
    94 	setup.teams[1].hogs[0].hat = "quotecap";
       
    95 	setup.teams[1].hogs[0].initialHealth = 100;
       
    96 	setup.teams[1].hogs[0].name = "Quote";
       
    97 	setup.teams[1].hogs[1].difficulty = 0;
       
    98 	setup.teams[1].hogs[1].hat = "chef";
       
    99 	setup.teams[1].hogs[1].initialHealth = 100;
       
   100 	setup.teams[1].hogs[1].name = "Chefkoch2";
   114 
   101 
   115 	flib_cfg_meta_destroy(meta);
   102 	flib_gameconn *gameconn = flib_gameconn_create("Medo42", metaconf, &setup, false);
       
   103 	assert(gameconn);
   116 
   104 
   117 	flib_quit();
   105 	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
   118 	return 0;*/
   106 	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
   119 
   107 
   120 	flib_init(0);
   108 	while(gameconn) {
   121 	flib_map *mapconf = flib_map_create_regular("Jungle", TEMPLATEFILTER_CAVERN);
   109 		flib_gameconn_tick(gameconn);
   122 	assert(mapconf);
       
   123 
       
   124 	flib_mapconn *mapconn = flib_mapconn_create("foobart", mapconf);
       
   125 	assert(mapconn);
       
   126 
       
   127 	flib_map_destroy(mapconf);
       
   128 	mapconf = NULL;
       
   129 
       
   130 	flib_mapconn_onFailure(mapconn, &handleMapFailure, &mapconn);
       
   131 	flib_mapconn_onSuccess(mapconn, &handleMapSuccess, &mapconn);
       
   132 
       
   133 	while(mapconn) {
       
   134 		flib_mapconn_tick(mapconn);
       
   135 	}
   110 	}
   136 	flib_log_i("Shutting down...");
   111 	flib_log_i("Shutting down...");
   137 	flib_quit();
   112 	flib_quit();
   138 	return 0;
   113 	return 0;
   139 }
   114 }