project_files/cmdlineClient/cmdlineClient.c
changeset 7473 45b9f25ff611
parent 7340 62043f5f7c67
child 7488 7e09947b6aa5
equal deleted inserted replaced
7470:0db1780ca938 7473:45b9f25ff611
    19 
    19 
    20 #include <frontlib.h>
    20 #include <frontlib.h>
    21 #include <util/logging.h>
    21 #include <util/logging.h>
    22 #include <util/util.h>
    22 #include <util/util.h>
    23 #include <base64/base64.h>
    23 #include <base64/base64.h>
       
    24 #include <model/schemelist.h>
    24 
    25 
    25 #include <stdlib.h>
    26 #include <stdlib.h>
    26 #include <stdbool.h>
    27 #include <stdbool.h>
    27 #include <assert.h>
    28 #include <assert.h>
    28 #include <string.h>
    29 #include <string.h>
    35 
    36 
    36 static flib_netconn *netconn;
    37 static flib_netconn *netconn;
    37 static flib_gameconn *gameconn;
    38 static flib_gameconn *gameconn;
    38 static flib_mapconn *mapconn;
    39 static flib_mapconn *mapconn;
    39 static char nickname[128];
    40 static char nickname[128];
    40 static flib_cfg_meta *metacfg;
    41 static flib_metascheme *metacfg;
    41 static bool netConnected = false;
    42 static bool netConnected = false;
    42 
    43 
    43 // Callback function that will be called when the map is rendered
    44 // Callback function that will be called when the map is rendered
    44 static void handleMapGenerated(void *context, const uint8_t *bitmap, int numHedgehogs) {
    45 static void handleMapGenerated(void *context, const uint8_t *bitmap, int numHedgehogs) {
    45 	printf("Drawing map for %i brave little hogs...", numHedgehogs);
    46 	printf("Drawing map for %i brave little hogs...", numHedgehogs);
    96 	printf("Disconnected: %s", message);
    97 	printf("Disconnected: %s", message);
    97 	flib_netconn_destroy(netconn);
    98 	flib_netconn_destroy(netconn);
    98 	netconn = NULL;
    99 	netconn = NULL;
    99 }
   100 }
   100 
   101 
   101 void printRoomList() {
   102 /*void printRoomList() {
   102 	const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
   103 	const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
   103 	if(roomlist) {
   104 	if(roomlist) {
   104 		if(roomlist->roomCount>0) {
   105 		if(roomlist->roomCount>0) {
   105 			for(int i=0; i<roomlist->roomCount; i++) {
   106 			for(int i=0; i<roomlist->roomCount; i++) {
   106 				if(i>0) {
   107 				if(i>0) {
   114 		}
   115 		}
   115 	} else {
   116 	} else {
   116 		puts("Sorry, due to an error the room list is not available.");
   117 		puts("Sorry, due to an error the room list is not available.");
   117 	}
   118 	}
   118 	puts("\n");
   119 	puts("\n");
   119 }
   120 }*/
   120 
   121 
   121 void printTeamList() {
   122 void printTeamList() {
   122 	flib_gamesetup *setup = flib_netconn_create_gamesetup(netconn);
   123 	flib_gamesetup *setup = flib_netconn_create_gamesetup(netconn);
   123 	if(setup) {
   124 	if(setup) {
   124 		puts("The following teams are in this room:");
   125 		puts("The following teams are in this room:");
   135 	flib_gamesetup_destroy(setup);
   136 	flib_gamesetup_destroy(setup);
   136 }
   137 }
   137 
   138 
   138 void handleNetConnected(void *context) {
   139 void handleNetConnected(void *context) {
   139 	printf("You enter the lobby of a strange house inhabited by hedgehogs. Looking around, you see hallways branching off to these rooms:\n");
   140 	printf("You enter the lobby of a strange house inhabited by hedgehogs. Looking around, you see hallways branching off to these rooms:\n");
   140 	printRoomList();
   141 	//printRoomList();
   141 	printf("\n\nNow, you can chat by just entering text, or join a room with /join <roomname>.");
   142 	printf("\n\nNow, you can chat by just entering text, or join a room with /join <roomname>.");
   142 	printf(" You can also /quit or let me /describe <roomname>. Once in a room, you can /add <teamname> and set yourself /ready. You can also /list the available rooms (in the lobby) or the teams (in a room).\n");
   143 	printf(" You can also /quit or let me /describe <roomname>. Once in a room, you can /add <teamname> and set yourself /ready. You can also /list the available rooms (in the lobby) or the teams (in a room).\n");
   143 	netConnected = true;
   144 	netConnected = true;
   144 }
   145 }
   145 
   146 
   269 		printf(" (%s)", msg);
   270 		printf(" (%s)", msg);
   270 	}
   271 	}
   271 	puts(" You are back in the lobby.");
   272 	puts(" You are back in the lobby.");
   272 }
   273 }
   273 
   274 
   274 void handleSchemeChanged(void *context, flib_cfg *scheme) {
   275 void handleSchemeChanged(void *context, const flib_scheme *scheme) {
   275 	printf("Game scheme: %s.\n", scheme->name);
   276 	printf("Game scheme: %s.\n", scheme->name);
   276 }
   277 }
   277 
   278 
   278 void handleWeaponsetChanged(void *context, flib_weaponset *weaponset) {
   279 void handleWeaponsetChanged(void *context, const flib_weaponset *weaponset) {
   279 	printf("Weaponset: %s.\n", weaponset->name);
   280 	printf("Weaponset: %s.\n", weaponset->name);
   280 }
   281 }
   281 
   282 
   282 void handleHogcountChanged(void *context, const char *team, int count) {
   283 void handleHogcountChanged(void *context, const char *team, int count) {
   283 	printf("Team %s will send %i hogs into the fight.\n", team, count);
   284 	printf("Team %s will send %i hogs into the fight.\n", team, count);
   293 
   294 
   294 void handleScriptChanged(void *context, const char *script) {
   295 void handleScriptChanged(void *context, const char *script) {
   295 	printf("Game Type: %s\n", script);
   296 	printf("Game Type: %s\n", script);
   296 }
   297 }
   297 
   298 
   298 void handleTeamAdd(void *context, flib_team *team) {
   299 void handleTeamAdd(void *context, const flib_team *team) {
   299 	printf("%s puts the team %s to the planning board.\n", team->ownerName, team->name);
   300 	printf("%s puts the team %s to the planning board.\n", team->ownerName, team->name);
   300 }
   301 }
   301 
   302 
   302 void handleTeamDelete(void *context, const char *teamName) {
   303 void handleTeamDelete(void *context, const char *teamName) {
   303 	printf("The team %s decided not to fight this battle after all.\n", teamName);
   304 	printf("The team %s decided not to fight this battle after all.\n", teamName);
   334 	}
   335 	}
   335 	if(!flib_init(0)) {
   336 	if(!flib_init(0)) {
   336 		flib_log_setLevel(FLIB_LOGLEVEL_WARNING);
   337 		flib_log_setLevel(FLIB_LOGLEVEL_WARNING);
   337 		freopen( "CON", "w", stdout );
   338 		freopen( "CON", "w", stdout );
   338 		freopen( "CON", "w", stderr );
   339 		freopen( "CON", "w", stderr );
   339 		metacfg = flib_cfg_meta_from_ini("metasettings.ini");
   340 		metacfg = flib_metascheme_from_ini("metasettings.ini");
   340 		if(!metacfg) {
   341 		if(!metacfg) {
   341 			flib_quit();
   342 			flib_quit();
   342 			return -1;
   343 			return -1;
   343 		} else {
   344 		} else {
   344 			return 0;
   345 			return 0;
   401 					if(!flib_gets(input, sizeof(input))) {
   402 					if(!flib_gets(input, sizeof(input))) {
   402 						if(!memcmp("/quit", input, strlen("/quit"))) {
   403 						if(!memcmp("/quit", input, strlen("/quit"))) {
   403 							flib_netconn_send_quit(netconn, "Player quit.");
   404 							flib_netconn_send_quit(netconn, "Player quit.");
   404 						} else if(!memcmp("/describe ", input, strlen("/describe "))) {
   405 						} else if(!memcmp("/describe ", input, strlen("/describe "))) {
   405 							const char *roomname = input+strlen("/describe ");
   406 							const char *roomname = input+strlen("/describe ");
   406 							const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
   407 							/*const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
   407 							flib_room *room = flib_roomlist_find(roomlist, roomname);
   408 							flib_room *room = flib_roomlist_find(roomlist, roomname);
   408 							if(!room) {
   409 							if(!room) {
   409 								puts("Unknown room.");
   410 								puts("Unknown room.");
   410 							} else {
   411 							} else {
   411 								char *text = flib_asprintf(
   412 								char *text = flib_asprintf(
   424 										room->weapons);
   425 										room->weapons);
   425 								if(text) {
   426 								if(text) {
   426 									puts(text);
   427 									puts(text);
   427 								}
   428 								}
   428 								free(text);
   429 								free(text);
   429 							}
   430 							}*/
   430 						} else if(!memcmp("/join ", input, strlen("/join "))) {
   431 						} else if(!memcmp("/join ", input, strlen("/join "))) {
   431 							const char *roomname = input+strlen("/join ");
   432 							const char *roomname = input+strlen("/join ");
   432 							flib_netconn_send_joinRoom(netconn, roomname);
   433 							flib_netconn_send_joinRoom(netconn, roomname);
   433 						} else if(!memcmp("/ready", input, strlen("/ready"))) {
   434 						} else if(!memcmp("/ready", input, strlen("/ready"))) {
   434 							flib_netconn_send_toggleReady(netconn);
   435 							flib_netconn_send_toggleReady(netconn);
   438 						} else if(!memcmp("/list", input, strlen("/list"))) {
   439 						} else if(!memcmp("/list", input, strlen("/list"))) {
   439 							if(flib_netconn_is_in_room_context(netconn)) {
   440 							if(flib_netconn_is_in_room_context(netconn)) {
   440 								printTeamList();
   441 								printTeamList();
   441 							} else {
   442 							} else {
   442 								puts("From this big and expansive lobby, hallways branch off to these rooms:");
   443 								puts("From this big and expansive lobby, hallways branch off to these rooms:");
   443 								printRoomList();
   444 								//printRoomList();
   444 							}
   445 							}
   445 						} else if(!memcmp("/addteam ", input, strlen("/addteam "))) {
   446 						} else if(!memcmp("/addteam ", input, strlen("/addteam "))) {
   446 							const char *teamname = input+strlen("/addteam ");
   447 							const char *teamname = input+strlen("/addteam ");
   447 							if(!flib_contains_dir_separator(teamname)) {
   448 							if(!flib_contains_dir_separator(teamname)) {
   448 								char *teamfilename = flib_asprintf("%s.hwt", teamname);
   449 								char *teamfilename = flib_asprintf("%s.hwt", teamname);
   451 									if(team) {
   452 									if(team) {
   452 										flib_netconn_send_addTeam(netconn, team);
   453 										flib_netconn_send_addTeam(netconn, team);
   453 									} else {
   454 									} else {
   454 										printf("Teamfile %s not found.\n", teamfilename);
   455 										printf("Teamfile %s not found.\n", teamfilename);
   455 									}
   456 									}
   456 									flib_team_release(team);
   457 									flib_team_destroy(team);
   457 								}
   458 								}
   458 								free(teamfilename);
   459 								free(teamfilename);
   459 							}
   460 							}
   460 						} else if(strlen(input)>0) {
   461 						} else if(strlen(input)>0) {
   461 							flib_netconn_send_chat(netconn, input);
   462 							flib_netconn_send_chat(netconn, input);
   467 		fflush(stdout);
   468 		fflush(stdout);
   468 		Sleep(10);
   469 		Sleep(10);
   469 	}
   470 	}
   470 
   471 
   471 
   472 
   472 	flib_cfg_meta_release(metacfg);
   473 	flib_metascheme_release(metacfg);
   473 	return 0;
   474 	return 0;
   474 }
   475 }