author | koda |
Sat, 29 Jun 2013 01:56:16 +0200 | |
changeset 9300 | e5dafb5843c6 |
parent 7576 | 65d29988fd3d |
child 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
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 |
|
7482 | 20 |
/** |
21 |
* This file contains functions for starting and interacting with a game run by the engine. |
|
22 |
* The general usage is to first create a gameconn object by calling one of the flib_gameconn_create |
|
23 |
* functions. That will cause the frontlib to listen on a random port which can be queried using |
|
24 |
* flib_gameconn_getport(). You should also register your callback functions right at the start |
|
25 |
* to ensure you don't miss any callbacks. |
|
26 |
* |
|
27 |
* Next, start the engine (that part is up to you) with the appropriate command line arguments |
|
28 |
* for starting a game. |
|
29 |
* |
|
30 |
* In order to allow the gameconn to run, you should regularly call flib_gameconn_tick(), which |
|
31 |
* performs network I/O and calls your callbacks on interesting events. |
|
32 |
* |
|
33 |
* Once the engine connects, the gameconn will send it the required commands for starting the |
|
34 |
* game you requested in your flib_gameconn_create call. |
|
35 |
* |
|
36 |
* When the game is finished (or the connection is lost), you will receive the onDisconnect |
|
37 |
* message. This is the signal to destroy the gameconn and stop calling tick(). |
|
38 |
*/ |
|
39 |
||
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
#ifndef GAMECONN_H_ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
#define GAMECONN_H_ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
#include "../model/gamesetup.h" |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
#include <stddef.h> |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
#include <stdint.h> |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
47 |
#include <stdbool.h> |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
48 |
|
7482 | 49 |
/* |
50 |
* Different reasons for a disconnect. Only GAME_END_FINISHED signals a correctly completed game. |
|
51 |
*/ |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
52 |
#define GAME_END_FINISHED 0 |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
53 |
#define GAME_END_INTERRUPTED 1 |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
54 |
#define GAME_END_HALTED 2 |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
55 |
#define GAME_END_ERROR 3 |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
57 |
typedef struct _flib_gameconn flib_gameconn; |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
58 |
|
7482 | 59 |
/** |
60 |
* Create a gameconn that will start a local or network game with the indicated configuration. |
|
61 |
*/ |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
62 |
flib_gameconn *flib_gameconn_create(const char *playerName, const flib_gamesetup *setup, bool netgame); |
7482 | 63 |
|
64 |
/** |
|
65 |
* Create a gameconn that will play back a demo. |
|
66 |
*/ |
|
67 |
flib_gameconn *flib_gameconn_create_playdemo(const uint8_t *demoFileContent, size_t size); |
|
68 |
||
69 |
/** |
|
70 |
* Create a gameconn that will continue from a saved game. |
|
71 |
*/ |
|
72 |
flib_gameconn *flib_gameconn_create_loadgame(const char *playerName, const uint8_t *saveFileContent, size_t size); |
|
73 |
||
74 |
/** |
|
75 |
* Create a gameconn that will start a campaign or training mission with the indicated script. |
|
76 |
* seed is the random seed to use as entropy source (any string). |
|
77 |
* script is the path and filename of a Campaign or Training script, relative to the Data directory |
|
78 |
* (e.g. "Missions/Training/Basic_Training_-_Bazooka.lua") |
|
79 |
*/ |
|
7271
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
80 |
flib_gameconn *flib_gameconn_create_campaign(const char *playerName, const char *seed, const char *script); |
5608ac657362
frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents:
7234
diff
changeset
|
81 |
|
7482 | 82 |
/** |
83 |
* Release all resources of this gameconn, including the network connection, and free its memory. |
|
84 |
* It is safe to call this function from a callback. |
|
85 |
*/ |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
86 |
void flib_gameconn_destroy(flib_gameconn *conn); |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
87 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
88 |
/** |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
89 |
* Returns the port on which the gameconn is listening. Only fails if you |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
90 |
* pass NULL (not allowed), in that case 0 is returned. |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
91 |
*/ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
92 |
int flib_gameconn_getport(flib_gameconn *conn); |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
93 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
94 |
/** |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
95 |
* Perform I/O operations and call callbacks if something interesting happens. |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
96 |
* Should be called regularly. |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
97 |
*/ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
98 |
void flib_gameconn_tick(flib_gameconn *conn); |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
99 |
|
7482 | 100 |
/** |
101 |
* Send an engine message to the engine. Only needed in net games, where you receive engine |
|
102 |
* messages from the server and have to pass them here. |
|
103 |
*/ |
|
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7271
diff
changeset
|
104 |
int flib_gameconn_send_enginemsg(flib_gameconn *conn, const uint8_t *data, size_t len); |
7482 | 105 |
|
106 |
/** |
|
107 |
* Send an info message to the engine that will be displayed in the game's chatlog. |
|
108 |
* The msgtype determines the color of the message; in the QTFrontend, info messages and |
|
109 |
* normal chat messages use 1, emote-messages (those starting with /me) use 2, and |
|
110 |
* join/leave messages use 3. You should use flib_gameconn_send_chatmsg for chat messages |
|
111 |
* though because it automatically formats /me messages. |
|
112 |
* |
|
113 |
* Generally only needed in net games. |
|
114 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
115 |
int flib_gameconn_send_textmsg(flib_gameconn *conn, int msgtype, const char *msg); |
7482 | 116 |
|
117 |
/** |
|
118 |
* Send a chat message to be displayed in the game's chatlog. Messages starting with /me are |
|
119 |
* automatically formatted correctly. |
|
120 |
* |
|
121 |
* Generally only needed in net games. |
|
122 |
*/ |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
123 |
int flib_gameconn_send_chatmsg(flib_gameconn *conn, const char *playername, const char *msg); |
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
124 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
125 |
/** |
7576
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
126 |
* Request the engine to stop the game (efinish). |
7482 | 127 |
* You can use this to shut down a game early without directly killing the engine process. |
128 |
*/ |
|
129 |
int flib_gameconn_send_quit(flib_gameconn *conn); |
|
130 |
||
131 |
/** |
|
7576
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
132 |
* Send an arbitrary command to the engine, e.g. "eforcequit" to shut down the engine |
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
133 |
* quickly. Commands prefixed with "e" will be processed by the engine's ProcessCommand |
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
134 |
* method (with the e removed, so e.g. efinish will be parsed as finish). |
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
135 |
*/ |
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
136 |
int flib_gameconn_send_cmd(flib_gameconn *conn, const char *cmdString); |
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
137 |
|
65d29988fd3d
Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents:
7482
diff
changeset
|
138 |
/** |
7482 | 139 |
* Expected callback signature: void handleConnect(void *context) |
140 |
* The engine has successfully connected. You don't have to react to this in any way. |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
141 |
*/ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
142 |
void flib_gameconn_onConnect(flib_gameconn *conn, void (*callback)(void* context), void* context); |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
143 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
144 |
/** |
7482 | 145 |
* Expected callback signature: void handleDisconnect(void *context, int reason) |
146 |
* The connection to the engine was closed, either because the game has ended normally, or |
|
147 |
* because it was interrupted/halted, or because of an error. The reason is provided as one |
|
148 |
* of the GAME_END_xxx constants. |
|
149 |
* |
|
150 |
* You should destroy the gameconn and - in a netgame - notify the server that the game has ended. |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
151 |
*/ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
152 |
void flib_gameconn_onDisconnect(flib_gameconn *conn, void (*callback)(void* context, int reason), void* context); |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
153 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
154 |
/** |
7482 | 155 |
* Expected callback signature: void handleErrorMessage(void* context, const char *msg) |
156 |
* The engine sent an error message, you should probably display it to the user or at least log it. |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
157 |
*/ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
158 |
void flib_gameconn_onErrorMessage(flib_gameconn *conn, void (*callback)(void* context, const char *msg), void* context); |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
159 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
160 |
/** |
7482 | 161 |
* Expected callback signature: void handleChat(void* context, const char *msg, bool teamchat) |
162 |
* The player entered a chat or teamchat message. In a netgame, you should send it on to the server. |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
163 |
*/ |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
164 |
void flib_gameconn_onChat(flib_gameconn *conn, void (*callback)(void* context, const char *msg, bool teamchat), void* context); |
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
165 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
166 |
/** |
7482 | 167 |
* Expected callback signature: void handleGameRecorded(void *context, const uint8_t *record, size_t size, bool isSavegame) |
168 |
* The game has stopped, and a demo or savegame is available. You can store it in a file and later pass it back |
|
169 |
* to the engine to either watch a replay (if it's a demo) or to continue playing (if it's a savegame). |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
170 |
*/ |
7320
e704706008d4
frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments
Medo <smaxein@googlemail.com>
parents:
7316
diff
changeset
|
171 |
void flib_gameconn_onGameRecorded(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *record, size_t size, bool isSavegame), void* context); |
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
172 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
173 |
/** |
7482 | 174 |
* Expected callback signature: void handleEngineMessage(void *context, const uint8_t *em, size_t size) |
175 |
* The engine has generated a message with player input. In a netgame, you should send it on to the server. |
|
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
176 |
*/ |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7271
diff
changeset
|
177 |
void flib_gameconn_onEngineMessage(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *em, size_t size), void* context); |
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
178 |
|
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
179 |
#endif |