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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <time.h>
#include "main.h"
#include "list.h"
#include "tux.h"
#include "wall.h"
#include "teleport.h"
#include "pipe.h"
#include "myTimer.h"
#include "arena.h"
#include "item.h"
#include "shot.h"
#include "arenaFile.h"
#include "proto.h"
#include "publicServer.h"
#include "net_multiplayer.h"
static int arenaId;
static arena_t *arena;
static bool_t isSignalEnd;
static int my_argc;
static char **my_argv;
arena_t* getWorldArena()
{
return arena;
}
void countRoundInc()
{
}
int initPublicServer()
{
initArenaFile();
initTux();
initItem();
initShot();
initTimer();
arenaId = getArenaIdFormNetName( getParamElse("--arena", "FAGN") );
arena = getArena(arenaId);
addNewItem(arena->listItem, NULL);
isSignalEnd = FALSE;
if( initNetMuliplayer(NET_GAME_TYPE_SERVER, NULL, atoi( getParamElse("--port", "2200") ) ) < 0 )
{
printf("Nemozem inicalizovat sietovy socket !\n");
return -1;
}
setServerMaxClients( atoi( getParamElse("--maxclients", "100") ));
return 0;
}
int getChoiceArenaId()
{
return arenaId;
}
int conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2)
{
return (x1<x2+w2 && x2<x1+w1 && y1<y2+h2 && y2<y1+h1);
}
int isFreeSpace(int x, int y, int w, int h)
{
if ( isConflictWithListTux(arena->listTux, x, y, w, h) )return 0;
if ( isConflictWithListWall(arena->listWall, x, y, w, h) )return 0;
if ( isConflictWithListShot(arena->listShot, x, y, w, h) )return 0;
if ( isConflictWithListItem(arena->listItem, x, y, w, h) )return 0;
if ( isConflictWithListTeleport(arena->listTeleport, x, y, w, h) )return 0;
if ( isConflictWithListPipe(arena->listPipe, x, y, w, h) )return 0;
return 1;
}
void findFreeSpace(int *x, int *y, int w, int h)
{
int z_x;
int z_y;
assert( x != NULL );
assert( y != NULL );
do{
z_x = random() % WINDOW_SIZE_X;
z_y = random() % (WINDOW_SIZE_Y-200);
}while( isFreeSpace(z_x, z_y ,w ,h) == 0 );
*x = z_x;
*y = z_y;
}
void eventPublicServer()
{
static my_time_t lastActive = 0;
my_time_t interval;
int i;
eventNetMultiplayer();
if( isSignalEnd == TRUE )
{
quitPublicServer();
}
if( lastActive == 0 )
{
lastActive = getMyTime();
}
interval = getMyTime() - lastActive;
if( interval < 50 )
{
return;
}
/*
printf("interval = %d\n", interval);
*/
lastActive = getMyTime();
eventConflictTuxWithTeleport(arena->listTux, arena->listTeleport);
eventConflictTuxWithShot(arena->listTux, arena->listShot);
for( i = 0 ; i < 4 ; i++)
{
eventMoveListShot(arena->listShot);
eventConflictShotWithWall(arena->listWall, arena->listShot);
eventConflictShotWithTeleport(arena->listTeleport, arena->listShot);
eventConflictShotWithPipe(arena->listPipe, arena->listShot);
eventConflictShotWithItem(arena->listItem, arena->listShot);
}
eventListItem(arena->listItem);
eventListTux(arena->listTux);
eventTimer();
}
void my_handler_quit(int n)
{
printf("my_handler_quit\n");
isSignalEnd = TRUE;
}
void quitPublicServer()
{
printf("quit public server\n");
quitNetMultiplayer();
destroyArena(arena);
quitArenaFile();
quitTimer();
exit(0);
}
char* getParam(char *s)
{
int i;
for( i = 0 ; i < my_argc - 1 ; i++ )
{
if( strcmp(s, my_argv[i]) == 0 )
{
return my_argv[i+1];
}
}
return NULL;
}
char* getParamElse(char *s1, char *s2)
{
char *ret;
ret = getParam(s1);
if( ret == NULL)
{
return s2;
}
return ret;
}
bool_t isParamFlag(char *s)
{
int i;
for( i = 0 ; i < my_argc ; i++ )
{
if( strcmp(s, my_argv[i]) == 0 )
{
return TRUE;
}
}
return FALSE;
}
char *getString(int n)
{
char str[STR_NUM_SIZE];
sprintf(str, "%d", n);
return strdup(str);
}
int main(int argc, char *argv[])
{
srand( (unsigned) time(NULL) );
my_argc = argc;
my_argv = argv;
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, my_handler_quit);
signal(SIGTERM, my_handler_quit);
signal(SIGQUIT, my_handler_quit);
if( initPublicServer() < 0 )
{
quitPublicServer();
return -1;
}
while(1)
{
eventPublicServer();
}
return 0;
}
|