Duke4.net Forums: Teleport player (con code) (solved) - Duke4.net Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Teleport player (con code) (solved)

User is offline   Jaap 

#1

 
Hi Guys,

I am working on a new elevator that teleports the player and other sprites to a floor of the players choice. Moving sprites seems to work just fine but moving the player proves to be problematic, the first time the player is moved just fine but after that it seems to randomly work.

I have to following:

All sprites are moved relatively to the elevator switch.


The location and sector of the switch the player is using are stored in:
MX,MY,MZ, MSECTOR

And the location and sectore of the destination switch are stored in:
x, y, z, sect

What I do is get the position of the player and make it relative to the current switch then I add the location of the destination switch (this should give the right coordinates). The first time I use the switch the player is moved and if there are sprites in the elevator they are moved as well. But the second time only the sprites get moved and the player acts randomly. Sometimes you can see a flash of the destination and then are immediately moved back to the old sector and position. Sometimes it just works. I have no idea and am now also changing the old position of the player but no luck so far.

This is my code. Some help would be appreciated, this thing is driving me nuts and I have been changing shit around even looking at the teleporter code in the source of EDuke.

gamearray TEMPARRAY 1


state binary_elevator_switch_teleport
{
	setvar target -1

	ifactor ELEVATORSWITCHDOWN // find ELEVATORSWITCHUP to teleport to
	{
		for GTEMP1 allsprites
		{
			ifvare sprite[GTEMP1].picnum ELEVATORSWITCHUP
			{
				ifvarvare actorvar[GTEMP1].HITAGSAVED HITAGSAVED
				{
					setvarvar target GTEMP1
				}
			}
		}
	}
	else ifactor ELEVATORSWITCHUP // find ELEVATORSWITCHDOWN to teleport to
	{
		for GTEMP1 allsprites
		{
			ifvare sprite[GTEMP1].picnum ELEVATORSWITCHDOWN
			{
				ifvarvare actorvar[GTEMP1].HITAGSAVED HITAGSAVED
				{
					setvarvar target GTEMP1
				}
			}
		}
	}


	setvar GTEMP2 0

	// Count all sprites
	ifvarn target -1
	{
		for GTEMP1 spritesofsector sprite[THISACTOR].sectnum
		{
			ifvarvarn GTEMP1 THISACTOR
			{
				ifvarn sprite[GTEMP1].picnum APLAYER
				{
					addvar GTEMP2 1
				}
			}
		}
	}

	// Only if we got any sprites
	ifvarg GTEMP2 0
	{
		resizearray TEMPARRAY GTEMP2

		setvar GTEMP2 0

		for GTEMP1 spritesofsector sprite[THISACTOR].sectnum
		{
			ifvarvarn GTEMP1 THISACTOR
			{
				ifvarn sprite[GTEMP1].picnum APLAYER
				{
					setarray TEMPARRAY[GTEMP2] GTEMP1

					addvar GTEMP2 1
				}
			}
		}
	}

	

	// Teleport all sprites in
	ifvarn target -1
	{
		// Get this switch pos
		getactor[THISACTOR].x MX
		getactor[THISACTOR].y MY
		getactor[THISACTOR].z MZ

		getactor[THISACTOR].sectnum MSECTOR

		// Get target switch pos
		getactor[target].x x
		getactor[target].y y
		getactor[target].z z

		getactor[target].sectnum sect


		setvar GTEMP5 0
		getarraysize TEMPARRAY GTEMP6

		// for GTEMP1 spritesofsector sprite[THISACTOR].sectnum
		whilevarvarn GTEMP5 GTEMP6
		{
			// Get sprite
			setvarvar GTEMP1 TEMPARRAY[GTEMP5]
			addvar GTEMP5 1

			// Get actor position
			getactor[GTEMP1].x GTEMP2
			getactor[GTEMP1].y GTEMP3
			getactor[GTEMP1].z GTEMP4

			// Make relative to this switch position
			subvarvar GTEMP2 MX
			subvarvar GTEMP3 MY
			subvarvar GTEMP4 MZ

			// Add other switch position
			addvarvar GTEMP2 x
			addvarvar GTEMP3 y
			addvarvar GTEMP4 z

			// Set actor position
			setactor[GTEMP1].x GTEMP2
			setactor[GTEMP1].y GTEMP3
			setactor[GTEMP1].z GTEMP4


			// Update old position ( getting desperate)
			setactor[GTEMP1].htbposx GTEMP2
			setactor[GTEMP1].htbposy GTEMP3
			setactor[GTEMP1].htbposz GTEMP4

			// Set sector
			setactor[GTEMP1].sectnum sect
		}
		
		// Teleport the player (todo: add sector check)		


		getplayer[THISACTOR].i GTEMP1

		// Get player position
		getplayer[THISACTOR].posx GTEMP2
		getplayer[THISACTOR].posy GTEMP3
		getplayer[THISACTOR].posz GTEMP4

		// Make relative to this switch position
		subvarvar GTEMP2 MX
		subvarvar GTEMP3 MY

		// Add other switch position
		addvarvar GTEMP2 x
		addvarvar GTEMP3 y

		// Get old sector hight
		getflorzofslope MSECTOR MX MY MZ
		// Get new sector hight
		getflorzofslope sect GTEMP2 GTEMP3 z


		// subtract old sector height
		subvarvar GTEMP4 MZ
		// Add new sector height
		addvarvar GTEMP4 z
		
		// Set player position
		setplayer[THISACTOR].posx GTEMP2
		setplayer[THISACTOR].posy GTEMP3
		setplayer[THISACTOR].posz GTEMP4
		
		setplayer[THISACTOR].oposx GTEMP2
		setplayer[THISACTOR].oposy GTEMP3
		setplayer[THISACTOR].oposz GTEMP4
		
		// Set sector
		setplayer[THISACTOR].cursectnum sect


		// Set actor position
		setactor[GTEMP1].x GTEMP2
		setactor[GTEMP1].y GTEMP3
		setactor[GTEMP1].z GTEMP4

		
		setactor[GTEMP1].htbposx GTEMP2
		setactor[GTEMP1].htbposy GTEMP3
		setactor[GTEMP1].htbposz GTEMP4
		

		// Set sector
		setactor[GTEMP1].sectnum sect

		sound MONITOR_ACTIVE

	}
}
ends



This post has been edited by Jaap: 05 December 2016 - 11:49 AM

0

User is online   Danukem 

  • Duke Plus Developer

#2

When you move sprites to a different sector, it's best to use changespritesect, rather than directly setting the sectnum with setactor. Also, when updating the player's sector, I would set player.cursectnum to the new sector in addition to setting the coordinates, but I would not change oposx, oposy, or oposz.
0

User is offline   Mblackwell 

  • Evil Overlord

#3

Quote

"It was once not suggested to manually change sectnum via "setactor[].sectnum" because it would not update the game's internal linked lists, causing oddball occurrences like sprites freezing in midair and general game state corruption. "setactor[].sectnum" is an alias of changespritesect in newer versions of EDuke32, preventing this corruption from occurring. "


http://wiki.eduke32.com/wiki/Sectnum
0

User is online   Danukem 

  • Duke Plus Developer

#4

OK, I didn't know that about changespritesect (or if I had been told before, I forgot).

Here's something else, though. Why is Jaap doing all that stuff with old sector height versus new sector height when setting the new player z coord? Why not just do the button z offset subtraction like he did with the height of the sprites? The only reason I can think of is if he wants to make sure the player ends up on the floor after the teleport, as opposed to being in the air (e.g. if the player had been jumping during the teleport). But I don't see why that would be desirable and the complications could easily get things mixed up.

EDIT: My only other thought is (and I admit I'm not going to take time to seriously look at the code), when you have something that works the first time and then doesn't work on subsequent attempts, that is often an indication that one or more values are not getting reset. So I would look seriously at how the arrays are being utilized and whether they should be re-initialized each time.

This post has been edited by Trooper Dan: 05 December 2016 - 01:49 AM

0

User is offline   Mblackwell 

  • Evil Overlord

#5

I don't understand why you need more than one loop going on. Realistically you should only need a single FOR loop and a triggering mechanism, no arrays or other things necessary. Your code seems overly complicated and it is probably obscuring bugs.
0

User is offline   Jaap 

#6

View PostMblackwell, on 05 December 2016 - 07:53 AM, said:

I don't understand why you need more than one loop going on. Realistically you should only need a single FOR loop and a triggering mechanism, no arrays or other things necessary. Your code seems overly complicated and it is probably obscuring bugs.


I could do with one less loop I guess. But I don't like resizing the arrays element after element. I prefer figuring out how big the array should be and then placing what is needed. As for why I got multiple loops.

First loop count how many elements should fit in the array.

Second loop place elements in array

Third loop move element from one sector to the other.

The reason I put the sprites in an array and then move them is because I wasn't sure how the for spritesofsector loop would respond if I'd for each iteration I would move a sprite to another sector. I am used to languages not allowing this sort of behavior, so I avoided it.

By the way, I figured it out. I am an idiot! It works perfectly, some times it just works so darn fast that you don't notice it. I decided to add an delay (for now) normally you would not notice since there always is a delay (waiting for the elevator doors to close, waiting for the elevator to move *teleport* waiting for the doors to open.

Thanks for thinking along with me!

P.S. Why can't I vote any posts up? Do I have to reach a certain level before I am allowed to do so?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic


All copyrights and trademarks not owned by Voidpoint, LLC are the sole property of their respective owners. Play Ion Fury! ;) © Voidpoint, LLC

Enter your sign in name and password


Sign in options