|
1 #include "frontlib.h" |
|
2 #include "util/logging.h" |
|
3 #include "util/buffer.h" |
|
4 #include "util/util.h" |
|
5 #include "util/list.h" |
|
6 #include "model/map.h" |
|
7 #include "model/weapon.h" |
|
8 #include "model/schemelist.h" |
|
9 #include "ipc/mapconn.h" |
|
10 #include "ipc/gameconn.h" |
|
11 #include "net/netconn.h" |
|
12 |
|
13 #include <stdlib.h> |
|
14 #include <stdbool.h> |
|
15 #include <assert.h> |
|
16 #include <string.h> |
|
17 #include <conio.h> |
|
18 #include <windows.h> |
|
19 |
|
20 #define ENGINE_DIR ".\\" |
|
21 #define CONFIG_DIR "..\\share\\hedgewars" |
|
22 #define DATA_DIR CONFIG_DIR"\\Data" |
|
23 |
|
24 static flib_netconn *netconn; |
|
25 static flib_gameconn *gameconn; |
|
26 static flib_mapconn *mapconn; |
|
27 static char nickname[128]; |
|
28 static flib_cfg_meta *metacfg; |
|
29 static bool netConnected = false; |
|
30 |
|
31 // Callback function that will be called when the map is rendered |
|
32 static void handleMapGenerated(void *context, const uint8_t *bitmap, int numHedgehogs) { |
|
33 printf("Drawing map for %i brave little hogs...", numHedgehogs); |
|
34 |
|
35 // Draw the map as ASCII art |
|
36 for(int y=0; y<MAPIMAGE_HEIGHT; y+=8) { |
|
37 for(int x=0; x<MAPIMAGE_WIDTH; x+=6) { |
|
38 int pixelnum = x + y*MAPIMAGE_WIDTH; |
|
39 bool pixel = bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7))); |
|
40 printf(pixel ? "#" : " "); |
|
41 } |
|
42 printf("\n"); |
|
43 } |
|
44 |
|
45 // Destroy the connection object (this will end the "tick" loop below) |
|
46 flib_mapconn_destroy(mapconn); |
|
47 mapconn = NULL; |
|
48 } |
|
49 |
|
50 static void onGameDisconnect(void *context, int reason) { |
|
51 flib_log_i("Connection closed. Reason: %i", reason); |
|
52 flib_gameconn_destroy(gameconn); |
|
53 gameconn = NULL; |
|
54 } |
|
55 |
|
56 // Callback function that will be called on error |
|
57 static void handleMapFailure(void *context, const char *errormessage) { |
|
58 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); |
|
62 mapconn = NULL; |
|
63 } |
|
64 |
|
65 static void startEngineMap(int port) { |
|
66 char cmdbuffer[255]; |
|
67 char argbuffer[255]; |
|
68 snprintf(cmdbuffer, 255, "%shwengine.exe", ENGINE_DIR); |
|
69 snprintf(argbuffer, 255, "%s %i landpreview", CONFIG_DIR, port); |
|
70 ShellExecute(NULL, NULL, cmdbuffer, argbuffer, NULL, SW_HIDE); |
|
71 } |
|
72 |
|
73 static void startEngineGame(int port) { |
|
74 char cmdbuffer[255]; |
|
75 char argbuffer[255]; |
|
76 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); |
|
78 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 } |
|
108 |
|
109 void handleNetDisconnect(void *context, int reason, const char *message) { |
|
110 printf("Disconnected: %s", message); |
|
111 flib_netconn_destroy(netconn); |
|
112 netconn = NULL; |
|
113 } |
|
114 |
|
115 void printRoomList() { |
|
116 const flib_roomlist *roomlist = flib_netconn_get_roomlist(netconn); |
|
117 if(roomlist) { |
|
118 for(int i=0; i<roomlist->roomCount; i++) { |
|
119 if(i>0) { |
|
120 printf(", "); |
|
121 } |
|
122 flib_room *room = roomlist->rooms[i]; |
|
123 printf("%s", room->name); |
|
124 } |
|
125 puts("\n"); |
|
126 } else { |
|
127 puts("Sorry, due to an error the room list is not available."); |
|
128 } |
|
129 } |
|
130 |
|
131 void printTeamList() { |
|
132 flib_gamesetup *setup = flib_netconn_create_gameSetup(netconn); |
|
133 if(setup) { |
|
134 puts("The following teams are in this room:"); |
|
135 for(int i=0; i<setup->teamlist->teamCount; i++) { |
|
136 if(i>0) { |
|
137 printf(", "); |
|
138 } |
|
139 printf("%s", setup->teamlist->teams[i]->name); |
|
140 } |
|
141 puts("\n"); |
|
142 } else { |
|
143 puts("Sorry, due to an error the team list is not available."); |
|
144 } |
|
145 flib_gamesetup_destroy(setup); |
|
146 } |
|
147 |
|
148 void handleNetConnected(void *context) { |
|
149 printf("You enter a strange house inhabited by dozens of hedgehogs. There are many rooms in here:\n"); |
|
150 printRoomList(); |
|
151 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"); |
|
153 netConnected = true; |
|
154 } |
|
155 |
|
156 void handleChat(void *context, const char *nick, const char *msg) { |
|
157 printf("%s: %s\n", nick, msg); |
|
158 } |
|
159 |
|
160 void handleEnterRoom(void *context, bool isChief) { |
|
161 puts("You have entered the room."); |
|
162 } |
|
163 |
|
164 void handleRoomJoin(void *context, const char *nick) { |
|
165 if(strcmp(nick, nickname)) { |
|
166 printf("%s is here.\n", nick); |
|
167 } |
|
168 } |
|
169 |
|
170 void handleRoomLeave(void *context, const char *nick, const char *partmsg) { |
|
171 if(strcmp(nick, nickname)) { |
|
172 printf("%s leaves.\n", nick); |
|
173 } |
|
174 } |
|
175 |
|
176 void handleReady(void *context, const char *nick, bool ready) { |
|
177 if(strcmp(nick, nickname)) { |
|
178 if(ready) { |
|
179 printf("%s is ready to go.\n", nick); |
|
180 } else { |
|
181 printf("%s is not ready.\n", nick); |
|
182 } |
|
183 } else { |
|
184 if(ready) { |
|
185 printf("You are ready to go.\n"); |
|
186 } else { |
|
187 printf("You are not ready.\n"); |
|
188 } |
|
189 } |
|
190 } |
|
191 |
|
192 void handleEmFromNet(void *context, const uint8_t *em, size_t size) { |
|
193 if(gameconn) { |
|
194 flib_gameconn_send_enginemsg(gameconn, (const uint8_t*)em, size); |
|
195 } |
|
196 } |
|
197 |
|
198 void handleEmFromEngine(void *context, const uint8_t *em, size_t size) { |
|
199 if(netconn) { |
|
200 flib_netconn_send_engineMessage(netconn, em, size); |
|
201 } |
|
202 } |
|
203 |
|
204 void handleChatFromGame(void *context, const char *message, bool teamchat) { |
|
205 if(netconn) { |
|
206 if(teamchat) { |
|
207 flib_netconn_send_teamchat(netconn, message); |
|
208 } else { |
|
209 flib_netconn_send_chat(netconn, message); |
|
210 } |
|
211 } |
|
212 } |
|
213 |
|
214 void handleRunGame(void *context) { |
|
215 flib_gamesetup *gamesetup = flib_netconn_create_gameSetup(netconn); |
|
216 if(gamesetup) { |
|
217 gameconn = flib_gameconn_create(nickname, gamesetup, true); |
|
218 flib_gameconn_onEngineMessage(gameconn, handleEmFromEngine, NULL); |
|
219 flib_gameconn_onDisconnect(gameconn, onGameDisconnect, NULL); |
|
220 flib_gameconn_onChat(gameconn, handleChatFromGame, NULL); |
|
221 startEngineGame(flib_gameconn_getport(gameconn)); |
|
222 } |
|
223 flib_gamesetup_destroy(gamesetup); |
|
224 } |
|
225 |
|
226 void handleNickTaken(void *context, const char *nick) { |
|
227 printf("The nickname %s is already in use, please choose a different one:\n", nick); |
|
228 flib_gets(nickname, sizeof(nickname)); |
|
229 flib_netconn_send_nick(netconn, nickname); |
|
230 } |
|
231 |
|
232 void handlePwRequest(void *context, const char *nick) { |
|
233 printf("A password is required to log in as %s, please enter (warning: shown in cleartext):\n", nick); |
|
234 char password[256]; |
|
235 flib_gets(password, sizeof(password)); |
|
236 flib_netconn_send_password(netconn, password); |
|
237 } |
|
238 |
|
239 void handleMessage(void *context, int type, const char *msg) { |
|
240 printf("*** %s\n", msg); |
|
241 } |
|
242 |
|
243 void handleTeamAccepted(void *context, const char *teamname) { |
|
244 printf("The team %s has been accepted.\n", teamname); |
|
245 } |
|
246 |
|
247 void handleMapChanged(void *context, const flib_map *map, int changetype) { |
|
248 if(map->mapgen != MAPGEN_NAMED && changetype != NETCONN_MAPCHANGE_THEME) { |
|
249 if(mapconn) { |
|
250 flib_mapconn_destroy(mapconn); |
|
251 mapconn = NULL; |
|
252 } |
|
253 mapconn = flib_mapconn_create(map); |
|
254 if(mapconn) { |
|
255 flib_mapconn_onSuccess(mapconn, handleMapGenerated, NULL); |
|
256 flib_mapconn_onFailure(mapconn, handleMapFailure, NULL); |
|
257 startEngineMap(flib_mapconn_getport(mapconn)); |
|
258 } |
|
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 } |