summaryrefslogtreecommitdiff
path: root/src/proto.c
blob: 8304c6dee0a5915d27dfac0acaae9bd7661d4456 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

#include "list.h"
#include "tux.h"
#include "item.h"
#include "network.h"
#include "string_length.h"
#include "screen_world.h"
#include "screen_setting.h"
#include "screen_choiceArena.h"
#include "arenaFile.h"
#include "net_multiplayer.h"
#include "client.h"
#include "server.h"
#include "proto.h"

void proto_send_init_server(client_t *client)
{
	char msg[STR_SIZE];
	int n;

	getSettingCountRound(&n);

	sprintf(msg, "init %d %d %d %d %s\n",
		client->tux->id, client->tux->x, client->tux->y,
		n, getArenaNetName( getChoiceArenaId() ));
	
	sendClient(client, msg);
}

void proto_recv_init_client(char *msg)
{
	char cmd[STR_SIZE];
	char arena_name[STR_SIZE];
	tux_t *tux;
	int id;
	int x, y, n;

	sscanf(msg, "%s %d %d %d %d %s\n",
	cmd, &id, &x, &y, &n, arena_name);

	setWorldArena( getArenaIdFormNetName(arena_name) );
	setMaxCountRound(n);

	tux = newTux();
	tux->id = id;
	tux->x = x;
	tux->y = y;
	tux->control = TUX_CONTROL_KEYBOARD_RIGHT;
	getSettingNameRight(tux->name);

	proto_send_context_client(tux);
	addList(getWorldArena()->listTux, tux);
}

void proto_send_event_server(tux_t *tux, int action, client_t *client)
{
	char msg[STR_SIZE];
	
	sprintf(msg, "event %d %d\n", tux->id, action);
	
	sendAllClientBut(msg, client);
}

void proto_recv_event_client(char *msg)
{
	char cmd[STR_SIZE];
	int id, action;
	tux_t *tux;

	sscanf(msg, "%s %d %d", cmd, &id, &action);

	tux = getTuxID(getWorldArena()->listTux, id);

	if( tux != NULL )
	{
		actionTux(tux, action);
	}
}

void proto_send_event_client(int action)
{
	char msg[STR_SIZE];
	
	sprintf(msg, "event %d\n", action);
	
	sendServer(msg);
}

void proto_recv_event_server(client_t *client, char *msg)
{
	char cmd[STR_SIZE];
	int action;

	sscanf(msg, "%s %d", cmd, &action);

	actionTux(client->tux, action);

	proto_send_event_server(client->tux, action, client);
}

void proto_send_newtux_server(client_t *client, tux_t *tux)
{
	char msg[STR_SIZE];

	sprintf(msg, "newtux %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n",
	tux->id ,tux->x, tux->y, tux->position ,tux->frame, tux->score, tux->name,
	tux->gun, tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE],
	tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], tux->shot[GUN_LASSER],
	tux->shot[GUN_MINE], tux->shot[GUN_BOMBBALL],
	tux->bonus_time, tux->pickup_time);
	
	if( client == NULL )
	{
		sendAllClient(msg);
	}
	else
	{
		sendClient(client, msg);
	}
}

void proto_recv_newtux_client(char *msg)
{
	char cmd[STR_SIZE];
	char name[STR_NAME_SIZE];
	int id, x, y, position, frame, score;
	int myGun, myBonus;
	int  gun1, gun2, gun3, gun4, gun5, gun6, gun7;
	int time1, time2;
	tux_t *tux;

	sscanf(msg, "%s %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d",
	cmd, &id, &x, &y, &position, &frame, &score, name,
	&myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, &gun7, &time1, &time2);

	tux = getTuxID(getWorldArena()->listTux, id);

	if( tux == NULL )
	{
		tux = newTux();
		tux->control = TUX_CONTROL_NET;
		addList(getWorldArena()->listTux, tux);
	}

	tux->id = id;
	tux->x = x;
	tux->y = y;
	tux->position = position;
	tux->frame = frame;
	tux->score = score;
	strcpy(tux->name, name);
	tux->gun = myGun;
	tux->bonus = myBonus;
	tux->shot[GUN_SIMPLE] = gun1;
	tux->shot[GUN_DUAL_SIMPLE] = gun2;
	tux->shot[GUN_SCATTER] = gun3;
	tux->shot[GUN_TOMMY] = gun4;
	tux->shot[GUN_LASSER] = gun5;
	tux->shot[GUN_MINE] = gun6;
	tux->shot[GUN_BOMBBALL] = gun7;
	tux->bonus_time = time1;
	tux->pickup_time = time2;
	tux->status = TUX_STATUS_ALIVE;
}

void proto_send_deltux_server(client_t *client)
{
	char msg[STR_SIZE];
	
	sprintf(msg, "deltux %d\n", client->tux->id);

	sendAllClientBut(msg, client);
}

void proto_recv_deltux_client(char *msg)
{
	char cmd[STR_SIZE];
	int id;
	tux_t *tux;

	sscanf(msg, "%s %d\n", cmd, &id);

	tux = getTuxID(getWorldArena()->listTux, id);

	if( tux != NULL )
	{
		int index;
		
		index = searchListItem(getWorldArena()->listTux, tux);
		delListItem(getWorldArena()->listTux, index, destroyTux);
	}
}

void proto_send_additem_server(item_t *p)
{
	char msg[STR_SIZE];
	int id = -1;

	if( p->author != NULL )
	{
		id = p->author->id;
	}

	sprintf(msg, "additem %d %d %d %d %d %d\n",
		p->type, p->x, p->y, p->count, p->frame, id);

	sendAllClient(msg);
}

void proto_recv_additem_client(char *msg)
{
	char cmd[STR_SIZE];
	int type, x, y, count, frame, id;
	item_t *item;

	sscanf(msg, "%s %d %d %d %d %d %d\n", cmd, &type, &x, &y, &count, &frame, &id);

	item = newItem(x, y, type, getTuxID(getWorldArena()->listTux, id));

	if( isConflictWithListItem(getWorldArena()->listItem, item->x, item->y, item->w, item->h) )
	{
		destroyItem(item);
		return;
	}

	item->count = count;
	item->frame = frame;

	addList(getWorldArena()->listItem, item);
}

void proto_send_context_client(tux_t *tux)
{
	char msg[STR_SIZE];
	
	sprintf(msg, "context %s\n", tux->name);

	sendServer(msg);
}

void proto_recv_context_server(client_t *client, char *msg)
{
	char cmd[STR_SIZE];
	char name[STR_NAME_SIZE];

	sscanf(msg, "%s %s\n", cmd, name);

	strcpy(client->tux->name, name);
	
	proto_send_newtux_server(NULL, client->tux);
}