summaryrefslogtreecommitdiff
path: root/src/teleport.c
blob: e402fcc61653109e63796f49260db27495fcb0ed (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

#include <stdlib.h>
#include <assert.h>

#include "main.h"
#include "tux.h"
#include "teleport.h"
#include "shot.h"
#include "net_multiplayer.h"
#include "proto.h"
#include "arena.h"

#ifndef PUBLIC_SERVER
#include "layer.h"
#include "screen_world.h"
#include "sound.h"
#endif

#ifdef PUBLIC_SERVER
#include "publicServer.h"
#endif

#ifndef PUBLIC_SERVER	
teleport_t* newTeleport(int x, int y, int w, int h, int layer, SDL_Surface *img)
#endif

#ifdef PUBLIC_SERVER	
teleport_t* newTeleport(int x, int y, int w, int h, int layer)
#endif
{
	teleport_t *new;
	
#ifndef PUBLIC_SERVER	
	assert( img != NULL );
#endif

	new  = malloc( sizeof(teleport_t) );
	assert( new != NULL );

	new->x = x;
	new->y = y;
	new->w = w;
	new->h = h;
	new->layer = layer;
#ifndef PUBLIC_SERVER	
	new->img = img;
#endif
	return new;
}

#ifndef PUBLIC_SERVER

void drawTeleport(teleport_t *p)
{
	assert( p != NULL );

	addLayer(p->img, p->x, p->y, 0, 0, p->img->w, p->img->h, p->layer);
}

void drawListTeleport(list_t *listTeleport)
{
	teleport_t *thisTeleport;
	int i;

	assert( listTeleport != NULL );

	for( i = 0 ; i < listTeleport->count ; i++ )
	{
		thisTeleport  = (teleport_t *)listTeleport->list[i];
		assert( thisTeleport != NULL );
		drawTeleport(thisTeleport);
	}
}

#endif

teleport_t* isConflictWithListTeleport(list_t *listTeleport, int x, int y, int w, int h)
{
	teleport_t *thisTeleport;
	int i;

	assert( listTeleport != NULL );

	for( i = 0 ; i < listTeleport->count ; i++ )
	{
		thisTeleport  = (teleport_t *)listTeleport->list[i];
		assert( thisTeleport != NULL );

		if( conflictSpace(x, y, w, h,
		    thisTeleport->x, thisTeleport->y, thisTeleport->w, thisTeleport->h) )
		{
			return thisTeleport;
		}
	}

	return NULL;
}

static teleport_t* getRandomTeleportBut(list_t *listTeleport, teleport_t *teleport)
{
	int x;

	do{
		x = random() % listTeleport->count;
	}while( listTeleport->list[x] == teleport );

	return (teleport_t *) listTeleport->list[x];
}

static int getRandomPosition()
{
	switch( random() % 4 )
	{
		case 0 :
		return TUX_UP;

		case 1 :
		return TUX_LEFT;

		case 2 :
		return TUX_RIGHT;

		case 3 :
		return TUX_DOWN;
	}

	assert( ! "Stupid error ! " );
}

static void moveShotFromTeleport(shot_t *shot, teleport_t *teleport, list_t *listTeleport)
{
	teleport_t *distTeleport;

	distTeleport = getRandomTeleportBut(listTeleport, teleport);

	moveShot(shot, getRandomPosition(), teleport->x, teleport->y,
		distTeleport->x, distTeleport->y, distTeleport->w, distTeleport->h);
}

void eventConflictShotWithTeleport(list_t *listTeleport, list_t *listShot)
{
	shot_t *thisShot;
	teleport_t *thisTeleport;
	int i;

	assert( listTeleport != NULL );
	assert( listShot != NULL );
	
	for( i = 0 ; i < listShot->count ; i++ )
	{
		thisShot  = (shot_t *)listShot->list[i];
		assert( thisShot != NULL );

		if( ( thisTeleport = isConflictWithListTeleport(listTeleport, thisShot->x, thisShot->y, thisShot->w, thisShot->h) ) != NULL )
		{
			tux_t *author;

			author = getTuxID(getCurrentArena()->listTux, thisShot->author_id);
			
			if( author != NULL && 
			    author->bonus == BONUS_GHOST &&
			    author->bonus_time > 0 )
			{
				continue;
			}

			if( getNetTypeGame() == NET_GAME_TYPE_CLIENT )
			{
				delListItem(listShot, i, destroyShot);
				i--;
			}
			else
			{
				moveShotFromTeleport(thisShot, thisTeleport, listTeleport);
			}
		}
	}
}

void eventTeleportTux(list_t *listTeleport, teleport_t *teleport, tux_t *tux)
{
	teleport_t *distTeleport;
	int current_x, current_y;
	int dist_x, dist_y;

	if( tux->bonus == BONUS_GHOST ||
	    getNetTypeGame() == NET_GAME_TYPE_CLIENT )
	{
		return;
	}

	distTeleport = getRandomTeleportBut(listTeleport, teleport);

	getTuxProportion(tux, &current_x, &current_y, NULL, NULL);

	switch( tux->position )
	{
		case TUX_UP :
			dist_x = distTeleport->x + distTeleport->w/2;
			dist_y = distTeleport->y - ( TUX_HEIGHT + 10 );
		break;

		case TUX_LEFT :
			dist_y = distTeleport->y + distTeleport->h/2;
			dist_x = distTeleport->x - ( TUX_WIDTH + 10 );
		break;

		case TUX_RIGHT :
			dist_y = distTeleport->y + distTeleport->h/2;
			dist_x = distTeleport->x + distTeleport->w + 10;
		break;

		case TUX_DOWN :
			dist_x = distTeleport->x + distTeleport->w/2;
			dist_y = distTeleport->y + distTeleport->h + 10;
		break;

		default :
			assert( ! "premenna p->control ma zlu hodnotu !" );
		break;
	}

	if( isFreeSpace(getCurrentArena(), dist_x, dist_y, TUX_WIDTH, TUX_HEIGHT) )
	{
		setTuxProportion(tux, dist_x, dist_y);
#ifndef PUBLIC_SERVER
		playSound("teleport", SOUND_GROUP_BASE);
#endif
		if( getNetTypeGame() == NET_GAME_TYPE_SERVER )
		{
			proto_send_newtux_server(PROTO_SEND_ALL, NULL, tux);
		}
	}
}

void destroyTeleport(teleport_t *p)
{
	assert( p != NULL );
	free(p);
}