project_files/frontlib/cmdlineClient.c
changeset 7316 f7b49b2c5d84
parent 7314 6171f0bad318
child 7318 a446eafcddeb
equal deleted inserted replaced
7314:6171f0bad318 7316:f7b49b2c5d84
     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 
       
    20 #include "frontlib.h"
       
    21 #include "util/logging.h"
       
    22 #include "util/buffer.h"
       
    23 #include "util/util.h"
       
    24 #include "util/list.h"
       
    25 #include "model/map.h"
       
    26 #include "model/weapon.h"
       
    27 #include "model/schemelist.h"
       
    28 #include "ipc/mapconn.h"
       
    29 #include "ipc/gameconn.h"
       
    30 #include "net/netconn.h"
       
    31 #include "base64/base64.h"
       
    32 
       
    33 #include <stdlib.h>
       
    34 #include <stdbool.h>
       
    35 #include <assert.h>
       
    36 #include <string.h>
       
    37 #include <conio.h>
       
    38 #include <windows.h>
       
    39 
       
    40 #define ENGINE_DIR ".\\"
       
    41 #define CONFIG_DIR "..\\share\\hedgewars"
       
    42 #define DATA_DIR CONFIG_DIR"\\Data"
       
    43 
       
    44 static flib_netconn *netconn;
       
    45 static flib_gameconn *gameconn;
       
    46 static flib_mapconn *mapconn;
       
    47 static char nickname[128];
       
    48 static flib_cfg_meta *metacfg;
       
    49 static bool netConnected = false;
       
    50 
       
    51 // Callback function that will be called when the map is rendered
       
    52 static void handleMapGenerated(void *context, const uint8_t *bitmap, int numHedgehogs) {
       
    53 	printf("Drawing map for %i brave little hogs...", numHedgehogs);
       
    54 
       
    55 	// Draw the map as ASCII art
       
    56 	for(int y=0; y<MAPIMAGE_HEIGHT; y+=8) {
       
    57 		for(int x=0; x<MAPIMAGE_WIDTH; x+=6) {
       
    58 			int pixelnum = x + y*MAPIMAGE_WIDTH;
       
    59 			bool pixel = bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7)));
       
    60 			printf(pixel ? "#" : " ");
       
    61 		}
       
    62 		printf("\n");
       
    63 	}
       
    64 
       
    65 	flib_mapconn_destroy(mapconn);
       
    66 	mapconn = NULL;
       
    67 }
       
    68 
       
    69 static void onGameDisconnect(void *context, int reason) {
       
    70 	flib_log_i("Connection closed. Reason: %i", reason);
       
    71 	flib_gameconn_destroy(gameconn);
       
    72 	gameconn = NULL;
       
    73 	if(netconn) {
       
    74 		flib_netconn_send_roundfinished(netconn, reason==GAME_END_FINISHED);
       
    75 	}
       
    76 }
       
    77 
       
    78 // Callback function that will be called on error
       
    79 static void handleMapFailure(void *context, const char *errormessage) {
       
    80 	flib_log_e("Map rendering failed: %s", errormessage);
       
    81 	flib_mapconn_destroy(mapconn);
       
    82 	mapconn = NULL;
       
    83 }
       
    84 
       
    85 static void startEngineMap(int port) {
       
    86 	char cmdbuffer[255];
       
    87 	char argbuffer[255];
       
    88 	snprintf(cmdbuffer, 255, "%shwengine.exe", ENGINE_DIR);
       
    89 	snprintf(argbuffer, 255, "%s %i landpreview", CONFIG_DIR, port);
       
    90 	ShellExecute(NULL, NULL, cmdbuffer, argbuffer, NULL, SW_HIDE);
       
    91 }
       
    92 
       
    93 static void startEngineGame(int port) {
       
    94 	char cmdbuffer[255];
       
    95 	char argbuffer[255];
       
    96 	char base64PlayerName[255];
       
    97 	base64_encode(nickname, strlen(nickname), base64PlayerName, sizeof(base64PlayerName));
       
    98 	snprintf(cmdbuffer, 255, "%shwengine.exe", ENGINE_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);
       
   100 	ShellExecute(NULL, NULL, cmdbuffer, argbuffer, NULL, SW_HIDE);
       
   101 }
       
   102 
       
   103 void handleNetDisconnect(void *context, int reason, const char *message) {
       
   104 	printf("Disconnected: %s", message);
       
   105 	flib_netconn_destroy(netconn);
       
   106 	netconn = NULL;
       
   107 }
       
   108 
       
   109 void printRoomList() {
       
   110 	const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
       
   111 	if(roomlist) {
       
   112 		if(roomlist->roomCount>0) {
       
   113 			for(int i=0; i<roomlist->roomCount; i++) {
       
   114 				if(i>0) {
       
   115 					printf(", ");
       
   116 				}
       
   117 				flib_room *room = roomlist->rooms[i];
       
   118 				printf("%s", room->name);
       
   119 			}
       
   120 		} else {
       
   121 			puts("Unfortunately, there are no rooms at the moment.");
       
   122 		}
       
   123 	} else {
       
   124 		puts("Sorry, due to an error the room list is not available.");
       
   125 	}
       
   126 	puts("\n");
       
   127 }
       
   128 
       
   129 void printTeamList() {
       
   130 	flib_gamesetup *setup = flib_netconn_create_gamesetup(netconn);
       
   131 	if(setup) {
       
   132 		puts("The following teams are in this room:");
       
   133 		for(int i=0; i<setup->teamlist->teamCount; i++) {
       
   134 			if(i>0) {
       
   135 				printf(", ");
       
   136 			}
       
   137 			printf("%s", setup->teamlist->teams[i]->name);
       
   138 		}
       
   139 		puts("\n");
       
   140 	} else {
       
   141 		puts("Sorry, due to an error the team list is not available.");
       
   142 	}
       
   143 	flib_gamesetup_destroy(setup);
       
   144 }
       
   145 
       
   146 void handleNetConnected(void *context) {
       
   147 	printf("You enter the lobby of a strange house inhabited by hedgehogs. Looking around, you see hallways branching off to these rooms:\n");
       
   148 	printRoomList();
       
   149 	printf("\n\nNow, you can chat by just entering text, or join a room with /join <roomname>.");
       
   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");
       
   151 	netConnected = true;
       
   152 }
       
   153 
       
   154 void handleChat(void *context, const char *nick, const char *msg) {
       
   155 	if(gameconn) {
       
   156 		flib_gameconn_send_chatmsg(gameconn, nick, msg);
       
   157 	}
       
   158 	printf("%s: %s\n", nick, msg);
       
   159 }
       
   160 
       
   161 void handleEnterRoom(void *context, bool isChief) {
       
   162 	puts("You have entered the room.");
       
   163 }
       
   164 
       
   165 void handleRoomJoin(void *context, const char *nick) {
       
   166 	if(strcmp(nick, nickname)) {
       
   167 		printf("%s is here.\n", nick);
       
   168 	}
       
   169 }
       
   170 
       
   171 void handleRoomLeave(void *context, const char *nick, const char *partmsg) {
       
   172 	if(strcmp(nick, nickname)) {
       
   173 		printf("%s leaves.\n", nick);
       
   174 	}
       
   175 }
       
   176 
       
   177 void handleReady(void *context, const char *nick, bool ready) {
       
   178 	if(strcmp(nick, nickname)) {
       
   179 		if(ready) {
       
   180 			printf("%s is ready to go.\n", nick);
       
   181 		} else {
       
   182 			printf("%s is not ready.\n", nick);
       
   183 		}
       
   184 	} else {
       
   185 		if(ready) {
       
   186 			printf("You are ready to go.\n");
       
   187 		} else {
       
   188 			printf("You are not ready.\n");
       
   189 		}
       
   190 	}
       
   191 }
       
   192 
       
   193 void handleEmFromNet(void *context, const uint8_t *em, size_t size) {
       
   194 	if(gameconn) {
       
   195 		flib_gameconn_send_enginemsg(gameconn, (const uint8_t*)em, size);
       
   196 	}
       
   197 }
       
   198 
       
   199 void handleEmFromEngine(void *context, const uint8_t *em, size_t size) {
       
   200 	if(netconn) {
       
   201 		flib_netconn_send_engineMessage(netconn, em, size);
       
   202 	}
       
   203 }
       
   204 
       
   205 void handleChatFromGame(void *context, const char *message, bool teamchat) {
       
   206 	if(netconn) {
       
   207 		if(teamchat) {
       
   208 			flib_netconn_send_teamchat(netconn, message);
       
   209 		} else {
       
   210 			flib_netconn_send_chat(netconn, message);
       
   211 		}
       
   212 	}
       
   213 }
       
   214 
       
   215 void handleRunGame(void *context) {
       
   216 	flib_gamesetup *gamesetup = flib_netconn_create_gamesetup(netconn);
       
   217 	if(gameconn) {
       
   218 		flib_log_e("Request to start game, but a game is already running.");
       
   219 	} else if(gamesetup) {
       
   220 		gameconn = flib_gameconn_create(nickname, gamesetup, true);
       
   221 		flib_gameconn_onEngineMessage(gameconn, handleEmFromEngine, NULL);
       
   222 		flib_gameconn_onDisconnect(gameconn, onGameDisconnect, NULL);
       
   223 		flib_gameconn_onChat(gameconn, handleChatFromGame, NULL);
       
   224 		startEngineGame(flib_gameconn_getport(gameconn));
       
   225 	}
       
   226 	flib_gamesetup_destroy(gamesetup);
       
   227 }
       
   228 
       
   229 void handleNickTaken(void *context, const char *nick) {
       
   230 	printf("The nickname %s is already in use, please choose a different one:\n", nick);
       
   231 	flib_gets(nickname, sizeof(nickname));
       
   232 	flib_netconn_send_nick(netconn, nickname);
       
   233 }
       
   234 
       
   235 void handlePwRequest(void *context, const char *nick) {
       
   236 	printf("A password is required to log in as %s, please enter (warning: shown in cleartext):\n", nick);
       
   237 	char password[256];
       
   238 	flib_gets(password, sizeof(password));
       
   239 	flib_netconn_send_password(netconn, password);
       
   240 }
       
   241 
       
   242 void handleMessage(void *context, int type, const char *msg) {
       
   243 	if(gameconn) {
       
   244 		flib_gameconn_send_textmsg(gameconn, 1, msg);
       
   245 	}
       
   246 	printf("*** %s\n", msg);
       
   247 }
       
   248 
       
   249 void handleTeamAccepted(void *context, const char *teamname) {
       
   250 	printf("The team %s has been accepted.\n", teamname);
       
   251 }
       
   252 
       
   253 void handleMapChanged(void *context, const flib_map *map, int changetype) {
       
   254 	if(map->mapgen != MAPGEN_NAMED && changetype != NETCONN_MAPCHANGE_THEME) {
       
   255 		if(mapconn) {
       
   256 			flib_mapconn_destroy(mapconn);
       
   257 			mapconn = NULL;
       
   258 		}
       
   259 		mapconn = flib_mapconn_create(map);
       
   260 		if(mapconn) {
       
   261 			flib_mapconn_onSuccess(mapconn, handleMapGenerated, NULL);
       
   262 			flib_mapconn_onFailure(mapconn, handleMapFailure, NULL);
       
   263 			startEngineMap(flib_mapconn_getport(mapconn));
       
   264 		}
       
   265 	} else if(map->mapgen == MAPGEN_NAMED) {
       
   266 		printf("The map %s has been selected.\n", map->name);
       
   267 	}
       
   268 }
       
   269 
       
   270 void handleLeaveRoom(void *context, int reason, const char *msg) {
       
   271 	if(reason == NETCONN_ROOMLEAVE_ABANDONED) {
       
   272 		printf("The chief has abandoned the room.");
       
   273 	} else if(reason == NETCONN_ROOMLEAVE_KICKED) {
       
   274 		printf("You have been kicked from the room.");
       
   275 	}
       
   276 	if(msg) {
       
   277 		printf(" (%s)", msg);
       
   278 	}
       
   279 	puts(" You are back in the lobby.");
       
   280 }
       
   281 
       
   282 void handleSchemeChanged(void *context, flib_cfg *scheme) {
       
   283 	printf("Game scheme: %s.\n", scheme->name);
       
   284 }
       
   285 
       
   286 void handleWeaponsetChanged(void *context, flib_weaponset *weaponset) {
       
   287 	printf("Weaponset: %s.\n", weaponset->name);
       
   288 }
       
   289 
       
   290 void handleHogcountChanged(void *context, const char *team, int count) {
       
   291 	printf("Team %s will send %i hogs into the fight.\n", team, count);
       
   292 }
       
   293 
       
   294 void handleRoomAdd(void *context, const flib_room *room) {
       
   295 	printf("%s created a new room called %s.\n", room->owner, room->name);
       
   296 }
       
   297 
       
   298 void handleRoomDelete(void *context, const char *roomName) {
       
   299 	printf("The room %s has collapsed.\n", roomName);
       
   300 }
       
   301 
       
   302 void handleScriptChanged(void *context, const char *script) {
       
   303 	printf("Game Type: %s\n", script);
       
   304 }
       
   305 
       
   306 void handleTeamAdd(void *context, flib_team *team) {
       
   307 	printf("%s puts the team %s to the planning board.\n", team->ownerName, team->name);
       
   308 }
       
   309 
       
   310 void handleTeamDelete(void *context, const char *teamName) {
       
   311 	printf("The team %s decided not to fight this battle after all.\n", teamName);
       
   312 }
       
   313 
       
   314 void handleTeamColorChanged(void *context, const char *name, int colorIndex) {
       
   315 	static const char* colorNames[] = {"red", "blue", "teal", "purple", "pink", "green", "orange", "brown", "yellow"};
       
   316 	const char *colorName = "strange";
       
   317 	if(colorIndex>=0 && colorIndex < 9) {
       
   318 		colorName = colorNames[colorIndex];
       
   319 	}
       
   320 	printf("The team %s will wear %s uniforms today.\n", name, colorName);
       
   321 }
       
   322 
       
   323 void tick() {
       
   324 	if(gameconn) {
       
   325 		flib_gameconn_tick(gameconn);
       
   326 	}
       
   327 	if(netconn) {
       
   328 		flib_netconn_tick(netconn);
       
   329 	}
       
   330 	if(mapconn) {
       
   331 		flib_mapconn_tick(mapconn);
       
   332 	}
       
   333 }
       
   334 
       
   335 static HANDLE hStdin;
       
   336 
       
   337 static int init() {
       
   338 	hStdin = GetStdHandle(STD_INPUT_HANDLE);
       
   339 	if(hStdin == INVALID_HANDLE_VALUE) {
       
   340 		flib_log_e("Unable to get stdin handle");
       
   341 		return 1;
       
   342 	}
       
   343 	if(!flib_init(0)) {
       
   344 		flib_log_setLevel(FLIB_LOGLEVEL_WARNING);
       
   345 		freopen( "CON", "w", stdout );
       
   346 		freopen( "CON", "w", stderr );
       
   347 		metacfg = flib_cfg_meta_from_ini("metasettings.ini");
       
   348 		if(!metacfg) {
       
   349 			flib_quit();
       
   350 			return -1;
       
   351 		} else {
       
   352 			return 0;
       
   353 		}
       
   354 	}
       
   355 	return -1;
       
   356 }
       
   357 
       
   358 int main(int argc, char *argv[]) {
       
   359 	if(init()) {
       
   360 		return -1;
       
   361 	}
       
   362 
       
   363 	puts("Please enter a nickname:");
       
   364 	flib_gets(nickname, sizeof(nickname));
       
   365 
       
   366 	netconn = flib_netconn_create(nickname, metacfg, DATA_DIR"\\", "140.247.62.101", 46631);
       
   367 	if(!netconn) {
       
   368 		flib_quit();
       
   369 		return -1;
       
   370 	}
       
   371 
       
   372 	flib_netconn_onConnected(netconn, handleNetConnected, NULL);
       
   373 	flib_netconn_onDisconnected(netconn, handleNetDisconnect, NULL);
       
   374 	flib_netconn_onChat(netconn, handleChat, NULL);
       
   375 	flib_netconn_onEnterRoom(netconn, handleEnterRoom, NULL);
       
   376 	flib_netconn_onRunGame(netconn, handleRunGame, NULL);
       
   377 	flib_netconn_onEngineMessage(netconn, handleEmFromNet, NULL);
       
   378 	flib_netconn_onRoomJoin(netconn, handleRoomJoin, NULL);
       
   379 	flib_netconn_onRoomLeave(netconn, handleRoomLeave, NULL);
       
   380 	flib_netconn_onReadyState(netconn, handleReady, NULL);
       
   381 	flib_netconn_onNickTaken(netconn, handleNickTaken, NULL);
       
   382 	flib_netconn_onPasswordRequest(netconn, handlePwRequest, NULL);
       
   383 	flib_netconn_onMessage(netconn, handleMessage, NULL);
       
   384 	flib_netconn_onTeamAccepted(netconn, handleTeamAccepted, NULL);
       
   385 	flib_netconn_onMapChanged(netconn, handleMapChanged, NULL);
       
   386 	flib_netconn_onLeaveRoom(netconn, handleLeaveRoom, NULL);
       
   387 	flib_netconn_onCfgScheme(netconn, handleSchemeChanged, NULL);
       
   388 	flib_netconn_onWeaponsetChanged(netconn, handleWeaponsetChanged, NULL);
       
   389 	flib_netconn_onHogCountChanged(netconn, handleHogcountChanged, NULL);
       
   390 	flib_netconn_onRoomAdd(netconn, handleRoomAdd, NULL);
       
   391 	flib_netconn_onRoomDelete(netconn, handleRoomDelete, NULL);
       
   392 	flib_netconn_onScriptChanged(netconn, handleScriptChanged, NULL);
       
   393 	flib_netconn_onTeamAdd(netconn, handleTeamAdd, NULL);
       
   394 	flib_netconn_onTeamDelete(netconn, handleTeamDelete, NULL);
       
   395 	flib_netconn_onTeamColorChanged(netconn, handleTeamColorChanged, NULL);
       
   396 
       
   397 	INPUT_RECORD inputRecord;
       
   398 	DWORD eventCount = 0;
       
   399 
       
   400 	while(netconn || gameconn) {
       
   401 		tick();
       
   402 		if(netconn && netConnected) {
       
   403 			while(PeekConsoleInput(hStdin, &inputRecord, 1, &eventCount) && eventCount>0) {
       
   404 				if(inputRecord.EventType != KEY_EVENT) {
       
   405 					ReadConsoleInput(hStdin, &inputRecord, 1, &eventCount);
       
   406 				} else {
       
   407 					printf("%s: ", nickname);
       
   408 					char input[256];
       
   409 					if(!flib_gets(input, sizeof(input))) {
       
   410 						if(!memcmp("/quit", input, strlen("/quit"))) {
       
   411 							flib_netconn_send_quit(netconn, "Player quit.");
       
   412 						} else if(!memcmp("/describe ", input, strlen("/describe "))) {
       
   413 							const char *roomname = input+strlen("/describe ");
       
   414 							const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn);
       
   415 							flib_room *room = flib_roomlist_find(roomlist, roomname);
       
   416 							if(!room) {
       
   417 								puts("Unknown room.");
       
   418 							} else {
       
   419 								char *text = flib_asprintf(
       
   420 										"%s is a room created by %s, where %i players (%i teams) are %s on %s%s, using the %s scheme and %s weaponset.",
       
   421 										room->name,
       
   422 										room->owner,
       
   423 										room->playerCount,
       
   424 										room->teamCount,
       
   425 										room->inProgress ? "fighting" : "preparing to fight",
       
   426 										room->map[0]=='+' ? "" : "the map ",
       
   427 										!strcmp("+rnd+", room->map) ? "a random map" :
       
   428 												!strcmp("+maze+", room->map) ? "a random maze" :
       
   429 												!strcmp("+drawn+", room->map) ? "a hand-drawn map" :
       
   430 												room->map,
       
   431 										room->scheme,
       
   432 										room->weapons);
       
   433 								if(text) {
       
   434 									puts(text);
       
   435 								}
       
   436 								free(text);
       
   437 							}
       
   438 						} else if(!memcmp("/join ", input, strlen("/join "))) {
       
   439 							const char *roomname = input+strlen("/join ");
       
   440 							flib_netconn_send_joinRoom(netconn, roomname);
       
   441 						} else if(!memcmp("/ready", input, strlen("/ready"))) {
       
   442 							flib_netconn_send_toggleReady(netconn);
       
   443 						} else if(!memcmp("/loglevel ", input, strlen("/loglevel "))) {
       
   444 							int loglevel = atoi(input+strlen("/loglevel "));
       
   445 							flib_log_setLevel(loglevel);
       
   446 						} else if(!memcmp("/list", input, strlen("/list"))) {
       
   447 							if(flib_netconn_is_in_room_context(netconn)) {
       
   448 								printTeamList();
       
   449 							} else {
       
   450 								puts("From this big and expansive lobby, hallways branch off to these rooms:");
       
   451 								printRoomList();
       
   452 							}
       
   453 						} else if(!memcmp("/addteam ", input, strlen("/addteam "))) {
       
   454 							const char *teamname = input+strlen("/addteam ");
       
   455 							if(!flib_contains_dir_separator(teamname)) {
       
   456 								char *teamfilename = flib_asprintf("%s.hwt", teamname);
       
   457 								if(teamfilename) {
       
   458 									flib_team *team = flib_team_from_ini(teamfilename);
       
   459 									if(team) {
       
   460 										flib_netconn_send_addTeam(netconn, team);
       
   461 									} else {
       
   462 										printf("Teamfile %s not found.\n", teamfilename);
       
   463 									}
       
   464 									flib_team_release(team);
       
   465 								}
       
   466 								free(teamfilename);
       
   467 							}
       
   468 						} else if(strlen(input)>0) {
       
   469 							flib_netconn_send_chat(netconn, input);
       
   470 						}
       
   471 					}
       
   472 				}
       
   473 			}
       
   474 		}
       
   475 		fflush(stdout);
       
   476 		Sleep(10);
       
   477 	}
       
   478 
       
   479 
       
   480 	flib_cfg_meta_release(metacfg);
       
   481 	return 0;
       
   482 }