project_files/frontlib/cmdlineClient.c
changeset 7314 6171f0bad318
parent 7275 15f722e0b96f
equal deleted inserted replaced
7312:d1db8aaa8edc 7314:6171f0bad318
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
     1 #include "frontlib.h"
    20 #include "frontlib.h"
     2 #include "util/logging.h"
    21 #include "util/logging.h"
     3 #include "util/buffer.h"
    22 #include "util/buffer.h"
     4 #include "util/util.h"
    23 #include "util/util.h"
     5 #include "util/list.h"
    24 #include "util/list.h"
     7 #include "model/weapon.h"
    26 #include "model/weapon.h"
     8 #include "model/schemelist.h"
    27 #include "model/schemelist.h"
     9 #include "ipc/mapconn.h"
    28 #include "ipc/mapconn.h"
    10 #include "ipc/gameconn.h"
    29 #include "ipc/gameconn.h"
    11 #include "net/netconn.h"
    30 #include "net/netconn.h"
       
    31 #include "base64/base64.h"
    12 
    32 
    13 #include <stdlib.h>
    33 #include <stdlib.h>
    14 #include <stdbool.h>
    34 #include <stdbool.h>
    15 #include <assert.h>
    35 #include <assert.h>
    16 #include <string.h>
    36 #include <string.h>
    40 			printf(pixel ? "#" : " ");
    60 			printf(pixel ? "#" : " ");
    41 		}
    61 		}
    42 		printf("\n");
    62 		printf("\n");
    43 	}
    63 	}
    44 
    64 
    45 	// Destroy the connection object (this will end the "tick" loop below)
       
    46 	flib_mapconn_destroy(mapconn);
    65 	flib_mapconn_destroy(mapconn);
    47 	mapconn = NULL;
    66 	mapconn = NULL;
    48 }
    67 }
    49 
    68 
    50 static void onGameDisconnect(void *context, int reason) {
    69 static void onGameDisconnect(void *context, int reason) {
    51 	flib_log_i("Connection closed. Reason: %i", reason);
    70 	flib_log_i("Connection closed. Reason: %i", reason);
    52 	flib_gameconn_destroy(gameconn);
    71 	flib_gameconn_destroy(gameconn);
    53 	gameconn = NULL;
    72 	gameconn = NULL;
       
    73 	if(netconn) {
       
    74 		flib_netconn_send_roundfinished(netconn, reason==GAME_END_FINISHED);
       
    75 	}
    54 }
    76 }
    55 
    77 
    56 // Callback function that will be called on error
    78 // Callback function that will be called on error
    57 static void handleMapFailure(void *context, const char *errormessage) {
    79 static void handleMapFailure(void *context, const char *errormessage) {
    58 	flib_log_e("Map rendering failed: %s", errormessage);
    80 	flib_log_e("Map rendering failed: %s", errormessage);
    59 
       
    60 	// Destroy the connection object (this will end the "tick" loop below)
       
    61 	flib_mapconn_destroy(mapconn);
    81 	flib_mapconn_destroy(mapconn);
    62 	mapconn = NULL;
    82 	mapconn = NULL;
    63 }
    83 }
    64 
    84 
    65 static void startEngineMap(int port) {
    85 static void startEngineMap(int port) {
    71 }
    91 }
    72 
    92 
    73 static void startEngineGame(int port) {
    93 static void startEngineGame(int port) {
    74 	char cmdbuffer[255];
    94 	char cmdbuffer[255];
    75 	char argbuffer[255];
    95 	char argbuffer[255];
       
    96 	char base64PlayerName[255];
       
    97 	base64_encode(nickname, strlen(nickname), base64PlayerName, sizeof(base64PlayerName));
    76 	snprintf(cmdbuffer, 255, "%shwengine.exe", ENGINE_DIR);
    98 	snprintf(cmdbuffer, 255, "%shwengine.exe", ENGINE_DIR);
    77 	snprintf(argbuffer, 255, "%s 1024 768 32 %i 0 0 0 10 10 %s 0 0 TWVkbzQy 0 0 en.txt", CONFIG_DIR, port, DATA_DIR);
    99 	snprintf(argbuffer, 255, "%s 1024 768 32 %i 0 0 0 10 10 %s 0 0 %s 0 0 en.txt", CONFIG_DIR, port, DATA_DIR, base64PlayerName);
    78 	ShellExecute(NULL, NULL, cmdbuffer, argbuffer, NULL, SW_HIDE);
   100 	ShellExecute(NULL, NULL, cmdbuffer, argbuffer, NULL, SW_HIDE);
    79 }
       
    80 
       
    81 void testMapPreview() {
       
    82 	// Create a map description and check that there was no error
       
    83 	flib_map *map = flib_map_create_maze("This is the seed value", "Jungle", MAZE_SIZE_SMALL_TUNNELS);
       
    84 	assert(map);
       
    85 
       
    86 	// Create a new connection to the engine and check that there was no error
       
    87 	flib_mapconn *mapConnection = flib_mapconn_create(map);
       
    88 	assert(mapConnection);
       
    89 
       
    90 	// We don't need the map description anymore
       
    91 	flib_map_release(map);
       
    92 	map = NULL;
       
    93 
       
    94 	// Register the callback functions
       
    95 	flib_mapconn_onFailure(mapConnection, &handleMapFailure, &mapConnection);
       
    96 	flib_mapconn_onSuccess(mapConnection, &handleMapGenerated, &mapConnection);
       
    97 
       
    98 	// Start the engine process and tell it which port the frontlib is listening on
       
    99 	startEngineMap(flib_mapconn_getport(mapConnection));
       
   100 
       
   101 	// Usually, flib_mapconn_tick will be called in an event loop that runs several
       
   102 	// times per second. It handles I/O operations and progress, and calls
       
   103 	// callbacks when something interesting happens.
       
   104 	while(mapConnection) {
       
   105 		flib_mapconn_tick(mapConnection);
       
   106 	}
       
   107 }
   101 }
   108 
   102 
   109 void handleNetDisconnect(void *context, int reason, const char *message) {
   103 void handleNetDisconnect(void *context, int reason, const char *message) {
   110 	printf("Disconnected: %s", message);
   104 	printf("Disconnected: %s", message);
   111 	flib_netconn_destroy(netconn);
   105 	flib_netconn_destroy(netconn);
   113 }
   107 }
   114 
   108 
   115 void printRoomList() {
   109 void printRoomList() {
   116 	const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
   110 	const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
   117 	if(roomlist) {
   111 	if(roomlist) {
   118 		for(int i=0; i<roomlist->roomCount; i++) {
   112 		if(roomlist->roomCount>0) {
   119 			if(i>0) {
   113 			for(int i=0; i<roomlist->roomCount; i++) {
   120 				printf(", ");
   114 				if(i>0) {
       
   115 					printf(", ");
       
   116 				}
       
   117 				flib_room *room = roomlist->rooms[i];
       
   118 				printf("%s", room->name);
   121 			}
   119 			}
   122 			flib_room *room = roomlist->rooms[i];
   120 		} else {
   123 			printf("%s", room->name);
   121 			puts("Unfortunately, there are no rooms at the moment.");
   124 		}
   122 		}
   125 		puts("\n");
       
   126 	} else {
   123 	} else {
   127 		puts("Sorry, due to an error the room list is not available.");
   124 		puts("Sorry, due to an error the room list is not available.");
   128 	}
   125 	}
       
   126 	puts("\n");
   129 }
   127 }
   130 
   128 
   131 void printTeamList() {
   129 void printTeamList() {
   132 	flib_gamesetup *setup = flib_netconn_create_gameSetup(netconn);
   130 	flib_gamesetup *setup = flib_netconn_create_gamesetup(netconn);
   133 	if(setup) {
   131 	if(setup) {
   134 		puts("The following teams are in this room:");
   132 		puts("The following teams are in this room:");
   135 		for(int i=0; i<setup->teamlist->teamCount; i++) {
   133 		for(int i=0; i<setup->teamlist->teamCount; i++) {
   136 			if(i>0) {
   134 			if(i>0) {
   137 				printf(", ");
   135 				printf(", ");
   144 	}
   142 	}
   145 	flib_gamesetup_destroy(setup);
   143 	flib_gamesetup_destroy(setup);
   146 }
   144 }
   147 
   145 
   148 void handleNetConnected(void *context) {
   146 void handleNetConnected(void *context) {
   149 	printf("You enter a strange house inhabited by dozens of hedgehogs. There are many rooms in here:\n");
   147 	printf("You enter the lobby of a strange house inhabited by hedgehogs. Looking around, you see hallways branching off to these rooms:\n");
   150 	printRoomList();
   148 	printRoomList();
   151 	printf("\n\nNow, you can chat by just entering text, or join a room with /join <roomname>.");
   149 	printf("\n\nNow, you can chat by just entering text, or join a room with /join <roomname>.");
   152 	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");
   150 	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");
   153 	netConnected = true;
   151 	netConnected = true;
   154 }
   152 }
   155 
   153 
   156 void handleChat(void *context, const char *nick, const char *msg) {
   154 void handleChat(void *context, const char *nick, const char *msg) {
       
   155 	if(gameconn) {
       
   156 		flib_gameconn_send_chatmsg(gameconn, nick, msg);
       
   157 	}
   157 	printf("%s: %s\n", nick, msg);
   158 	printf("%s: %s\n", nick, msg);
   158 }
   159 }
   159 
   160 
   160 void handleEnterRoom(void *context, bool isChief) {
   161 void handleEnterRoom(void *context, bool isChief) {
   161 	puts("You have entered the room.");
   162 	puts("You have entered the room.");
   210 		}
   211 		}
   211 	}
   212 	}
   212 }
   213 }
   213 
   214 
   214 void handleRunGame(void *context) {
   215 void handleRunGame(void *context) {
   215 	flib_gamesetup *gamesetup = flib_netconn_create_gameSetup(netconn);
   216 	flib_gamesetup *gamesetup = flib_netconn_create_gamesetup(netconn);
   216 	if(gamesetup) {
   217 	if(gameconn) {
       
   218 		flib_log_e("Request to start game, but a game is already running.");
       
   219 	} else if(gamesetup) {
   217 		gameconn = flib_gameconn_create(nickname, gamesetup, true);
   220 		gameconn = flib_gameconn_create(nickname, gamesetup, true);
   218 		flib_gameconn_onEngineMessage(gameconn, handleEmFromEngine, NULL);
   221 		flib_gameconn_onEngineMessage(gameconn, handleEmFromEngine, NULL);
   219 		flib_gameconn_onDisconnect(gameconn, onGameDisconnect, NULL);
   222 		flib_gameconn_onDisconnect(gameconn, onGameDisconnect, NULL);
   220 		flib_gameconn_onChat(gameconn, handleChatFromGame, NULL);
   223 		flib_gameconn_onChat(gameconn, handleChatFromGame, NULL);
   221 		startEngineGame(flib_gameconn_getport(gameconn));
   224 		startEngineGame(flib_gameconn_getport(gameconn));
   235 	flib_gets(password, sizeof(password));
   238 	flib_gets(password, sizeof(password));
   236 	flib_netconn_send_password(netconn, password);
   239 	flib_netconn_send_password(netconn, password);
   237 }
   240 }
   238 
   241 
   239 void handleMessage(void *context, int type, const char *msg) {
   242 void handleMessage(void *context, int type, const char *msg) {
       
   243 	if(gameconn) {
       
   244 		flib_gameconn_send_textmsg(gameconn, 1, msg);
       
   245 	}
   240 	printf("*** %s\n", msg);
   246 	printf("*** %s\n", msg);
   241 }
   247 }
   242 
   248 
   243 void handleTeamAccepted(void *context, const char *teamname) {
   249 void handleTeamAccepted(void *context, const char *teamname) {
   244 	printf("The team %s has been accepted.\n", teamname);
   250 	printf("The team %s has been accepted.\n", teamname);
   254 		if(mapconn) {
   260 		if(mapconn) {
   255 			flib_mapconn_onSuccess(mapconn, handleMapGenerated, NULL);
   261 			flib_mapconn_onSuccess(mapconn, handleMapGenerated, NULL);
   256 			flib_mapconn_onFailure(mapconn, handleMapFailure, NULL);
   262 			flib_mapconn_onFailure(mapconn, handleMapFailure, NULL);
   257 			startEngineMap(flib_mapconn_getport(mapconn));
   263 			startEngineMap(flib_mapconn_getport(mapconn));
   258 		}
   264 		}
       
   265 	} else if(map->mapgen == MAPGEN_NAMED) {
       
   266 		printf("The map %s has been selected.\n", map->name);
   259 	}
   267 	}
   260 }
   268 }
   261 
   269 
   262 void handleLeaveRoom(void *context, int reason, const char *msg) {
   270 void handleLeaveRoom(void *context, int reason, const char *msg) {
   263 	if(reason == NETCONN_ROOMLEAVE_ABANDONED) {
   271 	if(reason == NETCONN_ROOMLEAVE_ABANDONED) {