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