summaryrefslogtreecommitdiff
path: root/src/pipe.c
blob: 19b12c5e2ef2175b43ada1ebcefa7b87768a2808 (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

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

#include "main.h"
#include "pipe.h"
#include "shot.h"
#include "net_multiplayer.h"

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

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

#ifndef PUBLIC_SERVER	
pipe_t* newPipe(int x, int y, int w, int h, int layer, int id, int id_out, int position, SDL_Surface *img)
#endif

#ifdef PUBLIC_SERVER	
pipe_t* newPipe(int x, int y, int w, int h, int layer, int id, int id_out, int position)
#endif

{
	pipe_t *new;
	
#ifndef PUBLIC_SERVER	
	assert( img != NULL );
#endif

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

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

#ifndef PUBLIC_SERVER	

void drawPipe(pipe_t *p)
{
	assert( p != NULL );

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

void drawListPipe(list_t *listPipe)
{
	pipe_t *thisPipe;
	int i;

	assert( listPipe != NULL );

	for( i = 0 ; i < listPipe->count ; i++ )
	{
		thisPipe  = (pipe_t *)listPipe->list[i];
		assert( thisPipe != NULL );
		drawPipe(thisPipe);
	}
}

#endif

pipe_t* isConflictWithListPipe(list_t *listPipe, int x, int y, int w, int h)
{
	pipe_t *thisPipe;
	int i;

	assert( listPipe != NULL );

	for( i = 0 ; i < listPipe->count ; i++ )
	{
		thisPipe  = (pipe_t *)listPipe->list[i];
		assert( thisPipe != NULL );

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

	return NULL;
}

static int negPosition(int n)
{
	switch( n )
	{
		case TUX_UP :
		return TUX_DOWN;

		case TUX_LEFT :
		return TUX_RIGHT;

		case TUX_RIGHT :
		return TUX_LEFT;

		case TUX_DOWN :
		return TUX_UP;

		default :
			assert( ! "premenna n ma zlu hodnotu !" );
		break;
	}
}

static pipe_t* getPipeId(list_t *listPipe, int id)
{
	int i;

	for( i = 0 ; i < listPipe->count ; i++ )
	{
		pipe_t *thisPipe;

		thisPipe = (pipe_t *) listPipe->list[i];

		if( thisPipe->id == id )
		{
			return thisPipe;
		}
	}

	return NULL;
}

static void moveShotFromPipe(shot_t *shot, pipe_t *pipe, list_t *listPipe)
{
	pipe_t *distPipe;

	distPipe = getPipeId(listPipe, pipe->id_out);

	if( distPipe == NULL )
	{
		fprintf(stderr, "Pipe %d ID not found\n", pipe->id);
		return;
	}

	moveShot(shot, distPipe->position, pipe->x, pipe->y,
		distPipe->x, distPipe->y, distPipe->w, distPipe->h);
}

void eventConflictShotWithPipe(list_t *listPipe, list_t *listShot)
{
	shot_t *thisShot;
	pipe_t *thisPipe;
	int i;

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

		if( ( thisPipe = isConflictWithListPipe(listPipe, thisShot->x, thisShot->y,
			thisShot->w, thisShot->h) ) != NULL )
		{
			if( thisShot->author->bonus == BONUS_GHOST &&
			    thisShot->author->bonus_time > 0 )
			{
				continue;
			}

			if( negPosition( thisShot->position ) == thisPipe->position )
			{
				if( getNetTypeGame() == NET_GAME_TYPE_CLIENT )
				{
					delListItem(listShot, i, destroyShot);
					i--;
				}
				else
				{
					moveShotFromPipe(thisShot, thisPipe, listPipe);
				}
			}
			else
			{
				delListItem(listShot, i, destroyShot);
				i--;
			}
		}
	}
}

void destroyPipe(pipe_t *p)
{
	assert( p != NULL );
	free(p);
}