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