Next I want to hammer home a few points:
1) test every change you make, no matter how small or innocuous it is because sooner or later you will add one too many 'small' changes and find you have a devil of a job working out which one is to blame for screwing up your script.
2) no matter what you change, always make sure you leave your script in a position where it can execute. There is nothing worse than coming back to a script after a weekend away (or longer) and trying to work out why it doesn't work!
3) before embarking on huge changes make a copy of the script that works! You will thank me for this tip more than any other and it's the one I find hardest to do myself!
Right so now lets make our new little function work properly, note how I have made it return a value which can then be fed into the existing logic, also note how I made this a named variable to remind me what it is for! Try to get in a habit of making 'magic numbers' in your scripts into named local variables in this way, not only does it make scripts easier to read but it means that at some point you can collect them together at the top of the script where it is easier to change them quickly to 'fine-tune' the way a script works.
So far we have used this script on a standard (well I have at least) sized door but what happens if we have a really big door? In theory with a named variable we can simply copy the script as 'big_clever_door.lua' tweak the value of that variable and bingo the script will work for the bigger door.
The idea here is that the function should return a negative value if we are 'in front' of the door when we open it and a positive value if we are behind the door, then when we check to see if we have 'entered' the door we simply negate this value in the check.
local function calcDoorSide( obj )
local x, y, z, xa, ya, za = GetObjectPosAng( obj )
local osx, osy, osz = U.Rotate3D( 0, 0, -doorDistance,
rad( xa ), rad( ya ), rad( za ) )
-- are we 'in front' of the door?
if U.PlayerCloserThanPos( x + osx, y + osy, z + osz, doorDistance ) then
return -doorDistance
end
osx, osy, osz = U.Rotate3D( 0, 0, doorDistance,
rad( xa ), rad( ya ), rad( za ) )
-- are we 'behind' the door?
if U.PlayerCloserThanPos( x + osx, y + osy, z + osz, doorDistance ) then
return doorDistance
end
end
Note how similar this is to the code we already have to detect 'entering' the door.
We also need a tiny change to that original code:
if doors[ e ].opened then
local x, y, z, xa, ya, za = GetObjectPosAng( Ent.obj )
local osx, osy, osz = U.Rotate3D( 0, 0, -doors[ e ].side,
rad( xa ), rad( ya ), rad( za ) )
osx, osy, osz = x + osx, y + osy, z + osz
if U.PlayerCloserThanPos( osx, osy, osz, doorDistance ) then
doors[ e ].entered = true
end
end
Here we have negated the 'side' value because we are now detecting the 'other' side of the door.
Guess what we do now? We test the script! Note how now it doesn't matter which side of the door we are on when we open it we only see the 'entered' state change when we are on the other side.
Phew, next we'll do some more tidying up and fix those small inefficiencies I mentioned earlier.
Been there, done that, got all the T-Shirts!