summaryrefslogtreecommitdiff
path: root/src/uqm/supermelee/netplay/netmisc.c
blob: 3ab2f72b963f0e1cd30f70cf3146cd72879152ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 *  Copyright 2006  Serge van den Boom <svdb@stack.nl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "netplay.h"
#include "netmisc.h"

#include "netmelee.h"
#include "notifyall.h"
#include "packetsenders.h"
#include "proto/ready.h"

#include "../melee.h"
		// For feedback functions.

#include <stdlib.h>


static BattleStateData *BattleStateData_alloc(void);
static void BattleStateData_free(BattleStateData *battleStateData);
static inline BattleStateData *BattleStateData_new(
		struct melee_state *meleeState,
		struct battlestate_struct *battleState,
		struct getmelee_struct *getMeleeState);
static void BattleStateData_delete(BattleStateData *battleStateData);


static BattleStateData *
BattleStateData_alloc(void) {
	return (BattleStateData *) malloc(sizeof (BattleStateData));
}

static void
BattleStateData_free(BattleStateData *battleStateData) {
	free(battleStateData);
}

static inline BattleStateData *
BattleStateData_new(struct melee_state *meleeState,
		struct battlestate_struct *battleState,
		struct getmelee_struct *getMeleeState) {
	BattleStateData *battleStateData = BattleStateData_alloc();
	battleStateData->releaseFunction =
			(NetConnectionStateData_ReleaseFunction) BattleStateData_delete;
	battleStateData->meleeState = meleeState;
	battleStateData->battleState = battleState;
	battleStateData->getMeleeState = getMeleeState;
	return battleStateData;
}

static void
BattleStateData_delete(BattleStateData *battleStateData) {
	BattleStateData_free(battleStateData);
}


////////////////////////////////////////////////////////////////////////////


static void NetMelee_enterState_inSetup(NetConnection *conn, void *arg);

// Called when a connection has been established.
void
NetMelee_connectCallback(NetConnection *conn) {
	BattleStateData *battleStateData;
	struct melee_state *meleeState;

	meleeState = (struct melee_state *) NetConnection_getExtra(conn);
	battleStateData = BattleStateData_new(meleeState, NULL, NULL);
	NetConnection_setStateData(conn, (void *) battleStateData);
	NetConnection_setExtra(conn, NULL);

	// We have sent no teams yet. Initialize the state accordingly.
	MeleeSetup_resetSentTeams (meleeState->meleeSetup);

	sendInit(conn);
	Netplay_localReady (conn, NetMelee_enterState_inSetup, NULL, false);
}

// Called when a connection is closed.
void
NetMelee_closeCallback(NetConnection *conn) {
	closeFeedback(conn);
}

// Called when a network error occurs during connect.
void
NetMelee_errorCallback(NetConnection *conn,
		const NetConnectionError *error) {
	errorFeedback(conn);
	(void) error;
}

// Callback function for when both sides have finished initialisation after
// initial connect.
static void
NetMelee_enterState_inSetup(NetConnection *conn, void *arg) {
	BattleStateData *battleStateData;
	struct melee_state *meleeState;
	int player;

	NetConnection_setState(conn, NetState_inSetup);
		
	battleStateData = (BattleStateData *) NetConnection_getStateData(conn);
	meleeState = battleStateData->meleeState;
	
	player = NetConnection_getPlayerNr(conn);

	connectedFeedback(conn);

	// Send our team to the remote side.
	// XXX This only works with 2 players atm.
	assert (NUM_PLAYERS == 2);
	Melee_bootstrapSyncTeam (meleeState, player);

	flushPacketQueues();

	(void) arg;
}