project_files/frontlib/test.c.nocompile
author Medo <smaxein@googlemail.com>
Wed, 27 Jun 2012 22:52:19 +0200
changeset 7314 6171f0bad318
parent 7275 15f722e0b96f
permissions -rw-r--r--
frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib files that did not have their license fixed already.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7314
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     1
/*
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     2
 * Hedgewars, a free turn based strategy game
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     4
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     5
 * This program is free software; you can redistribute it and/or
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     6
 * modify it under the terms of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     8
 * of the License, or (at your option) any later version.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
     9
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    13
 * GNU General Public License for more details.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    14
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    15
 * You should have received a copy of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    16
 * along with this program; if not, write to the Free Software
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    18
 */
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    19
 
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7275
diff changeset
    20
 #include "frontlib.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
#include "util/logging.h"
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
    22
#include "util/buffer.h"
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    23
#include "util/util.h"
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
    24
#include "util/list.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
#include "model/map.h"
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    26
#include "model/weapon.h"
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    27
#include "model/schemelist.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
#include "ipc/mapconn.h"
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
#include "ipc/gameconn.h"
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    30
#include "net/netconn.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
#include <stdlib.h>
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
#include <stdbool.h>
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
#include <assert.h>
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
    35
#include <string.h>
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
// Callback function that will be called when the map is rendered
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
static void handleMapSuccess(void *context, const uint8_t *bitmap, int numHedgehogs) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	printf("Drawing map for %i brave little hogs...", numHedgehogs);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	// Draw the map as ASCII art
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	for(int y=0; y<MAPIMAGE_HEIGHT; y++) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
		for(int x=0; x<MAPIMAGE_WIDTH; x++) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
			int pixelnum = x + y*MAPIMAGE_WIDTH;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
			bool pixel = bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7)));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
			printf(pixel ? "#" : " ");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
		}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
		printf("\n");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	// Destroy the connection object (this will end the "tick" loop below)
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	flib_mapconn **connptr = context;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
	flib_mapconn_destroy(*connptr);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	*connptr = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
static void onDisconnect(void *context, int reason) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	flib_log_i("Connection closed. Reason: %i", reason);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	flib_gameconn **connptr = context;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
	flib_gameconn_destroy(*connptr);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	*connptr = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
static void onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
	flib_log_i("Writing %s (%i bytes)...", isSavegame ? "savegame" : "demo", size);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
	FILE *file = fopen(isSavegame ? "testsave.42.hws" : "testdemo.42.hwd", "wb");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	fwrite(record, 1, size, file);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
	fclose(file);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
// Callback function that will be called on error
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
static void handleMapFailure(void *context, const char *errormessage) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
	flib_log_e("Map rendering failed: %s", errormessage);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	// Destroy the connection object (this will end the "tick" loop below)
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
	flib_mapconn **connptr = context;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
	flib_mapconn_destroy(*connptr);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	*connptr = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
static void startEngineMap(int port) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	char commandbuffer[255];
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
	const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
	const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
	snprintf(commandbuffer, 255, "start %s\\hwengine.exe %s %i landpreview", enginePath, configPath, port);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
	system(commandbuffer);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
static void startEngineGame(int port) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	char commandbuffer[255];
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
	const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
	const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
	const char *dataPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars\\Data";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
	snprintf(commandbuffer, 255, "start %s\\hwengine.exe %s 1024 768 32 %i 0 0 0 10 10 %s 0 0 TWVkbzQy 0 0 en.txt", enginePath, configPath, port, dataPath);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
	flib_log_d("Starting engine with CMD: %s", commandbuffer);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
	system(commandbuffer);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
void testMapPreview() {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
	// Create a map description and check that there was no error
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   101
	flib_map *map = flib_map_create_maze("This is the seed value", "Jungle", MAZE_SIZE_SMALL_TUNNELS);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
	assert(map);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
	// Create a new connection to the engine and check that there was no error
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   105
	flib_mapconn *mapConnection = flib_mapconn_create(map);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
	assert(mapConnection);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
	// We don't need the map description anymore
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   109
	flib_map_release(map);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
	map = NULL;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
	// Register the callback functions
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
	flib_mapconn_onFailure(mapConnection, &handleMapFailure, &mapConnection);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
	flib_mapconn_onSuccess(mapConnection, &handleMapSuccess, &mapConnection);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
	// Start the engine process and tell it which port the frontlib is listening on
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
	startEngineMap(flib_mapconn_getport(mapConnection));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
	// Usually, flib_mapconn_tick will be called in an event loop that runs several
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
	// times per second. It handles I/O operations and progress, and calls
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
	// callbacks when something interesting happens.
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
	while(mapConnection) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
		flib_mapconn_tick(mapConnection);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   127
/*void testGame() {
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   128
	flib_cfg_meta *metaconf = flib_cfg_meta_from_ini("metasettings.ini");
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
	assert(metaconf);
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   130
	flib_weaponset *weapons = flib_weaponset_create("Defaultweaps");
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   131
	flib_schemelist *schemelist = flib_schemelist_from_ini(metaconf, "schemes.ini");
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   132
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
	flib_gamesetup setup;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   134
	setup.gamescheme = flib_schemelist_find(schemelist, "Default");
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   135
	setup.map = flib_map_create_maze("asparagus", "Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
	setup.script = NULL;
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   137
	setup.teamCount = 2;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
	setup.teams = calloc(2, sizeof(flib_team*));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
	setup.teams[0] = calloc(1, sizeof(flib_team));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
	setup.teams[0]->color = 0xffff0000;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
	setup.teams[0]->flag = "australia";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
	setup.teams[0]->fort = "Plane";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
	setup.teams[0]->grave = "Bone";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
	setup.teams[0]->hogsInGame = 2;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
	setup.teams[0]->name = "Team Awesome";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
	setup.teams[0]->voicepack = "British";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
	setup.teams[0]->hogs[0].difficulty = 2;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
	setup.teams[0]->hogs[0].hat = "NoHat";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
	setup.teams[0]->hogs[0].initialHealth = 100;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
	setup.teams[0]->hogs[0].name = "Harry 120";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
	setup.teams[0]->hogs[1].difficulty = 2;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
	setup.teams[0]->hogs[1].hat = "chef";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
	setup.teams[0]->hogs[1].initialHealth = 100;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
	setup.teams[0]->hogs[1].name = "Chefkoch";
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
	setup.teams[1] = flib_team_from_ini("Cave Dwellers.hwt");
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   156
	setup.teams[1]->color = 0xFF0000F0;
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   157
	setup.teams[1]->hogsInGame = 8;
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   158
	flib_team_set_weaponset(setup.teams[0], weapons);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   159
	flib_team_set_weaponset(setup.teams[1], weapons);
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   160
	flib_weaponset_release(weapons);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   162
	flib_gameconn *gameconn = flib_gameconn_create("Medo42", &setup, false);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
	assert(gameconn);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   165
	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   166
	//flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
	startEngineGame(flib_gameconn_getport(gameconn));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
	while(gameconn) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
		flib_gameconn_tick(gameconn);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
	}
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   173
}*/
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   175
void testDemo() {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
	FILE *demofile = fopen("testdemo.42.hwd", "rb");
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
	assert(demofile);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
	flib_vector *vec = flib_vector_create();
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
	uint8_t demobuf[512];
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
	int len;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
	while((len=fread(demobuf, 1, 512, demofile))>0) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   182
		flib_vector_append(vec, demobuf, len);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   183
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
	fclose(demofile);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
	flib_constbuffer constbuf = flib_vector_as_constbuffer(vec);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
	flib_gameconn *gameconn = flib_gameconn_create_playdemo(constbuf.data, constbuf.size);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
	flib_vector_destroy(vec);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
	assert(gameconn);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
	startEngineGame(flib_gameconn_getport(gameconn));
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   192
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   193
	while(gameconn) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   194
		flib_gameconn_tick(gameconn);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   195
	}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   196
}
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   197
7227
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   198
void testSave() {
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   199
	FILE *demofile = fopen("testsave.42.hws", "rb");
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   200
	assert(demofile);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   201
	flib_vector *vec = flib_vector_create();
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   202
	uint8_t demobuf[512];
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   203
	int len;
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   204
	while((len=fread(demobuf, 1, 512, demofile))>0) {
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   205
		flib_vector_append(vec, demobuf, len);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   206
	}
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   207
	fclose(demofile);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   208
	flib_constbuffer constbuf = flib_vector_as_constbuffer(vec);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   209
	flib_gameconn *gameconn = flib_gameconn_create_loadgame("Medo42", constbuf.data, constbuf.size);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   210
	flib_vector_destroy(vec);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   211
	assert(gameconn);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   212
	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   213
	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   214
	startEngineGame(flib_gameconn_getport(gameconn));
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   215
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   216
	while(gameconn) {
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   217
		flib_gameconn_tick(gameconn);
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   218
	}
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   219
}
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   220
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   221
void handleNetDisconnect(void *context, int reason, const char *message) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   222
	flib_log_i("Disconnected: %s", message);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   223
	flib_netconn_destroy(*(flib_netconn**)context);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   224
	*(flib_netconn**)context = NULL;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   225
}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   226
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   227
void handleNetConnected(void *context) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   228
	const flib_roomlist *roomlist = flib_netconn_get_roomlist(*(flib_netconn**)context);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   229
	flib_log_i("List of rooms:");
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   230
	for(int i=0; i<roomlist->roomCount; i++) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   231
		flib_roomlist_room *room = roomlist->rooms[i];
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   232
		flib_log_i("%1s %20s %20s %2i %2i %20s %20s %20s", room->inProgress ? "X" : " ", room->name, room->owner, room->playerCount, room->teamCount, room->map, room->scheme, room->weapons);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   233
	}
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   234
	flib_netconn_send_joinRoom(*(flib_netconn**)context, "frontlib test");
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   235
}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   236
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   237
void handleLobbyJoin(void *context, const char *nick) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   238
	flib_log_i("%s joined", nick);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   239
}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   240
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   241
void handleChat(void *context, const char *nick, const char *msg) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   242
	flib_log_i("%s: %s", nick, msg);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   243
	if(!memcmp("frontbot ", msg, strlen("frontbot "))) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   244
		const char *command = msg+strlen("frontbot ");
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   245
		if(!memcmp("quit", command, strlen("quit"))) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   246
			flib_netconn_send_quit(*(flib_netconn**)context, "Yeth Mathter");
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   247
		} else if(!memcmp("describe ", command, strlen("describe "))) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   248
			const char *roomname = command+strlen("describe ");
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   249
			const flib_roomlist *roomlist = flib_netconn_get_roomlist(*(flib_netconn**)context);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   250
			flib_roomlist_room *room = flib_roomlist_find((flib_roomlist*)roomlist, roomname);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   251
			if(!room) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   252
				flib_netconn_send_chat(*(flib_netconn**)context, "Unknown room.");
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   253
			} else {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   254
				char *text = flib_asprintf(
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   255
						"%s is a room created by %s, where %i players (%i teams) are %s on %s%s, using the %s scheme and %s weaponset.",
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   256
						room->name,
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   257
						room->owner,
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   258
						room->playerCount,
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   259
						room->teamCount,
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   260
						room->inProgress ? "fighting" : "preparing to fight",
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   261
						room->map[0]=='+' ? "" : "the map ",
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   262
						!strcmp("+rnd+", room->map) ? "a random map" :
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   263
								!strcmp("+maze+", room->map) ? "a random maze" :
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   264
								!strcmp("+drawn+", room->map) ? "a hand-drawn map" :
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   265
								room->map,
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   266
						room->scheme,
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   267
						room->weapons);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   268
				if(text) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   269
					flib_netconn_send_chat(*(flib_netconn**)context, text);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   270
				}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   271
				free(text);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   272
			}
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   273
		} else if(!memcmp("join ", command, strlen("join "))) {
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   274
			const char *roomname = command+strlen("join ");
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   275
			flib_netconn_send_joinRoom(*(flib_netconn**)context, roomname);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   276
		} else if(!memcmp("ready", command, strlen("ready"))) {
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   277
			flib_netconn_send_toggleReady(*(flib_netconn**)context);
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   278
		}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   279
	}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   280
}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   281
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   282
void handleEnterRoom(void *context, bool isChief) {
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   283
	flib_netconn_send_toggleReady(*(flib_netconn**)context);
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   284
}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   285
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   286
flib_gameconn *gameconn = NULL;
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   287
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   288
void emFromNetHandler(void *context, const char *em, int size) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   289
	flib_gameconn_send_enginemsg(gameconn, (const uint8_t*)em, size);
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   290
}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   291
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   292
void emFromEngineHandler(void *context, const uint8_t *em, int size) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   293
	flib_netconn_send_engineMessage((flib_netconn*)context, em, size);
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   294
}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   295
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   296
void handleRunGame(void *context) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   297
	flib_gamesetup *gamesetup = flib_netconn_create_gameSetup((flib_netconn*)context);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   298
	if(gamesetup) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   299
		gameconn = flib_gameconn_create("frontbot", gamesetup, true);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   300
		flib_gameconn_onEngineMessage(gameconn, emFromEngineHandler, context);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   301
		flib_gameconn_onDisconnect(gameconn, onDisconnect, &gameconn);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   302
		startEngineGame(flib_gameconn_getport(gameconn));
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   303
	}
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   304
}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   305
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   306
int main(int argc, char *argv[]) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   307
	flib_init(0);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   308
	flib_log_setLevel(FLIB_LOGLEVEL_ALL);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   309
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   310
	//testMapPreview();
7227
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   311
	//testDemo();
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
   312
	//testSave();
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   313
	//testGame();
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   314
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   315
	flib_cfg_meta *meta = flib_cfg_meta_from_ini("metasettings.ini");
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   316
	assert(meta);
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   317
	flib_netconn *conn = flib_netconn_create("frontbot", meta, "140.247.62.101", 46631);
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   318
	assert(conn);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   319
	flib_cfg_meta_release(meta);
7230
240620f46dd7 Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   320
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   321
	flib_netconn_onConnected(conn, handleNetConnected, &conn);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   322
	flib_netconn_onDisconnected(conn, handleNetDisconnect, &conn);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   323
	flib_netconn_onLobbyJoin(conn, handleLobbyJoin, &conn);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   324
	flib_netconn_onChat(conn, handleChat, &conn);
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7269
diff changeset
   325
	flib_netconn_onEnterRoom(conn, handleEnterRoom, conn);
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   326
	flib_netconn_onRunGame(conn, handleRunGame, conn);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   327
	flib_netconn_onEngineMessage(conn, emFromNetHandler, NULL);
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   328
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   329
	while(conn) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   330
		flib_netconn_tick(conn);
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   331
		if(gameconn) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   332
			flib_gameconn_tick(gameconn);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
   333
		}
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7230
diff changeset
   334
	}
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   335
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   336
	flib_quit();
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   337
	return 0;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   338
}