Switch opens, closes doors -- prl_door_stayopen.cog
One or two switches open one or two doors. Doors stay open until switch activated again. 14 February 2002

# Jedi Knight Cog Script
#
# prl_door_stayopen.cog
# Sylvicolus [PRL] 28 May 2001
#
# Opens or closes 1 to 2 doors by 2 switches only.
# Doors are closed in frame 0, open in frame 1.
# Doors stay open until switch is activated again.
# Must use at least one switch.
# Sector adjoins are changed.
# If block=1 then doors will open then close when blocked.
# 
# This cog is not supported by LucasArts Entertainment Co.

symbols	
message	startup		
message	activated
message arrived
message	blocked		

thing	door0		linkid=0
thing	door1		linkid=1
surface	switch0		linkid=6
surface	switch1		linkid=6
flex	moveSpeed=2.0
flex	sleeptime=2.0
int	block=1
sound	wav0=beep1.wav
sound	wav1=df_door3-3.wav     local
sector	doorSector0 	local
sector	doorSector1 	local
int	numDoors=0	local
int	doorStatus	local
int	moveStatus	local
int	moveto=1	local

end

# ========================================================

code

startup:
	if (door0 >= 0)	numdoors = numdoors + 1;
	if (door1 >= 0) numdoors = numdoors + 1;
	doorSector0 = GetThingSector(door0);
	SetSectorAdjoins(doorSector0, 0);
	if (door1 >= 0) doorSector1 = GetThingSector(door1); SetSectorAdjoins(doorSector1, 0);
	return;

activated:
	if (GetSenderId() != 6) 
	{ 	// message from door, not from switch
		PlaySoundThing(wav1, door0, 1.0, -1, -1, 0);
		if (door1 >= 0) PlaySoundThing(wav1, door1, 1.0, -1, -1, 0);
		return;  
	}
	call CheckStatus;
	if (moveStatus) return;	// doors are still moving
	if (doorStatus == 0)	// all pieces are at frame 0
	{
		moveto = 1;	
		SetSectorAdjoins(doorSector0, 1);
		if (door1 >= 0) SetSectorAdjoins(doorSector1, 1);
	}
	if (doorStatus == numdoors) moveto = 0;	// all pieces are at frame 1
   	SetWallCel(switch0, moveto);
   	PlaySoundPos(wav0, SurfaceCenter(switch0), 0.6, -1, -1, 0);
	if (switch1 >= 0) 
	{ 
   		SetWallCel(switch1, moveto);
   		PlaySoundPos(wav0, SurfaceCenter(switch1), 0.6, -1, -1, 0);
	}
	call MoveDoors;
	return;

blocked:		// player is blocking doors
	if (block==1)	// door responds to block
	{
	moveto = 1;
	call MoveDoors;	
	sleep(sleeptime);
	moveto = 0;
	call MoveDoors;
	}
	return;

MoveDoors:
	movetoframe(door0, moveto, movespeed);
	if (door1 >= 0) movetoframe(door1, moveto, movespeed);
	return;

arrived:
	call CheckStatus;
	if (moveStatus) return;	// doors are still moving
	if (doorStatus == 0)	// all pieces are at frame 0
	{
		SetSectorAdjoins(doorSector0, 0);
		if (door1 >= 0)  SetSectorAdjoins(doorSector1, 0);
	}
	return;

CheckStatus:
	moveStatus = 0;
	doorStatus = 0;
	movestatus = ismoving(door0);
	if (door1 >= 0) movestatus = movestatus + ismoving(door1);
	doorstatus = getcurframe(door0);
	if (door1 >= 0) doorstatus = doorstatus + getcurframe(door1);
	return;

end