summaryrefslogtreecommitdiff
path: root/src/heretic/p_plats.c
blob: 6443909823b8a71170ad93b18dd5277d42e4b135 (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
// P_plats.c

#include "DoomDef.h"
#include "P_local.h"
#include "soundst.h"

plat_t	*activeplats[MAXPLATS];

//==================================================================
//
//	Move a plat up and down
//
//==================================================================
void	T_PlatRaise(plat_t	*plat)
{
	result_e res;

	switch(plat->status)
	{
		case up:
			res = T_MovePlane(plat->sector,plat->speed,
					plat->high,plat->crush,0,1);
			if(!(leveltime&31))
			{
				S_StartSound((mobj_t *)&plat->sector->soundorg,
					sfx_stnmov);
			}
			if(plat->type == raiseAndChange
				|| plat->type == raiseToNearestAndChange)
			{
				if(!(leveltime&7))
				{
					S_StartSound((mobj_t *)&plat->sector->soundorg,
						sfx_stnmov);
				}
			}
			if (res == crushed && (!plat->crush))
			{
				plat->count = plat->wait;
				plat->status = down;
				S_StartSound((mobj_t *)&plat->sector->soundorg, sfx_pstart);
			}
			else
			if (res == pastdest)
			{
				plat->count = plat->wait;
				plat->status = waiting;
				S_StartSound((mobj_t *)&plat->sector->soundorg, sfx_pstop);
				switch(plat->type)
				{
					case downWaitUpStay:
						P_RemoveActivePlat(plat);
						break;
					case raiseAndChange:
						P_RemoveActivePlat(plat);
						break;
					default:
						break;
				}
			}
			break;
		case	down:
			res = T_MovePlane(plat->sector,plat->speed,plat->low,false,0,-1);
			if (res == pastdest)
			{
				plat->count = plat->wait;
				plat->status = waiting;
				S_StartSound((mobj_t *)&plat->sector->soundorg, sfx_pstop);
			}
			else
			{
				if(!(leveltime&31))
				{
					S_StartSound((mobj_t *)&plat->sector->soundorg,
						sfx_stnmov);
				}
			}
			break;
		case	waiting:
			if (!--plat->count)
			{
				if (plat->sector->floorheight == plat->low)
					plat->status = up;
				else
					plat->status = down;
				S_StartSound((mobj_t *)&plat->sector->soundorg, sfx_pstart);
			}
		case	in_stasis:
			break;
	}
}

//==================================================================
//
//	Do Platforms
//	"amount" is only used for SOME platforms.
//
//==================================================================
int	EV_DoPlat(line_t *line,plattype_e type,int amount)
{
	plat_t		*plat;
	int			secnum;
	int			rtn;
	sector_t	*sec;
	
	secnum = -1;
	rtn = 0;
	
	//
	//	Activate all <type> plats that are in_stasis
	//
	switch(type)
	{
		case perpetualRaise:
			P_ActivateInStasis(line->tag);
			break;
		default:
			break;
	}
	
	while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
	{
		sec = &sectors[secnum];
		if (sec->specialdata)
			continue;
	
		//
		// Find lowest & highest floors around sector
		//
		rtn = 1;
		plat = Z_Malloc( sizeof(*plat), PU_LEVSPEC, 0);
		P_AddThinker(&plat->thinker);
		
		plat->type = type;
		plat->sector = sec;
		plat->sector->specialdata = plat;
		plat->thinker.function = T_PlatRaise;
		plat->crush = false;
		plat->tag = line->tag;
		switch(type)
		{
			case raiseToNearestAndChange:
				plat->speed = PLATSPEED/2;
				sec->floorpic = sides[line->sidenum[0]].sector->floorpic;
				plat->high = P_FindNextHighestFloor(sec,sec->floorheight);
				plat->wait = 0;
				plat->status = up;
				sec->special = 0;		// NO MORE DAMAGE, IF APPLICABLE
				S_StartSound((mobj_t *)&sec->soundorg, sfx_stnmov);
				break;
			case raiseAndChange:
				plat->speed = PLATSPEED/2;
				sec->floorpic = sides[line->sidenum[0]].sector->floorpic;
				plat->high = sec->floorheight + amount*FRACUNIT;
				plat->wait = 0;
				plat->status = up;
				S_StartSound((mobj_t *)&sec->soundorg, sfx_stnmov);
				break;
			case downWaitUpStay:
				plat->speed = PLATSPEED * 4;
				plat->low = P_FindLowestFloorSurrounding(sec);
				if (plat->low > sec->floorheight)
					plat->low = sec->floorheight;
				plat->high = sec->floorheight;
				plat->wait = 35*PLATWAIT;
				plat->status = down;
				S_StartSound((mobj_t *)&sec->soundorg, sfx_pstart);
				break;
			case perpetualRaise:
				plat->speed = PLATSPEED;
				plat->low = P_FindLowestFloorSurrounding(sec);
				if (plat->low > sec->floorheight)
					plat->low = sec->floorheight;
				plat->high = P_FindHighestFloorSurrounding(sec);
				if (plat->high < sec->floorheight)
					plat->high = sec->floorheight;
				plat->wait = 35*PLATWAIT;
				plat->status = P_Random()&1;
				S_StartSound((mobj_t *)&sec->soundorg, sfx_pstart);
				break;
		}
		P_AddActivePlat(plat);
	}
	return rtn;
}

void P_ActivateInStasis(int tag)
{
	int		i;
	
	for (i = 0;i < MAXPLATS;i++)
		if (activeplats[i] &&
			(activeplats[i])->tag == tag &&
			(activeplats[i])->status == in_stasis)
		{
			(activeplats[i])->status = (activeplats[i])->oldstatus;
			(activeplats[i])->thinker.function = T_PlatRaise;
		}
}

void EV_StopPlat(line_t *line)
{
	int		j;
	
	for (j = 0;j < MAXPLATS;j++)
		if (activeplats[j] && ((activeplats[j])->status != in_stasis) &&
			((activeplats[j])->tag == line->tag))
		{
			(activeplats[j])->oldstatus = (activeplats[j])->status;
			(activeplats[j])->status = in_stasis;
			(activeplats[j])->thinker.function = NULL;
		}
}

void P_AddActivePlat(plat_t *plat)
{
	int		i;
	for (i = 0;i < MAXPLATS;i++)
		if (activeplats[i] == NULL)
		{
			activeplats[i] = plat;
			return;
		}
	I_Error ("P_AddActivePlat: no more plats!");
}

void P_RemoveActivePlat(plat_t *plat)
{
	int		i;
	for (i = 0;i < MAXPLATS;i++)
		if (plat == activeplats[i])
		{
			(activeplats[i])->sector->specialdata = NULL;
			P_RemoveThinker(&(activeplats[i])->thinker);
			activeplats[i] = NULL;
			return;
		}
	I_Error ("P_RemoveActivePlat: can't find plat!");
}