Automatic Gate

Started by soulshaker, Feb 28, 2015, 06:22 AM

Previous topic - Next topic

soulshaker

Well, there is already a automatic gate function released, but for me its not automatic(you have to press keys to open/close gate). So I tried making something which is really automatic.

///VCMP 0.4 Automatic gate system by SouLy^///
Thanks to Gudio for helping me with onPlayerPart function :D
 
This function adds a automatic gate at mansion. Enjoy
 
 
function onScriptLoad()
{
        gate <- CreateObject( 310, 1, -276.618, -495.992, 10.2778, 255 );
        gate_timer <- NewTimer("gatesystem", 500, 0 );
        gate_timer.Paused = true;
        timer_status <- false;
}
 
function onPlayerJoin( player )
{
        if(timer_status == false)
        {
                gate_timer.Paused = false;
                timer_status = true;
                print("Timer Resumed");
        }
}
 
function onPlayerPart( player, reason )
{
        if ( GetPlayers()-1 == 0 )
        {
                gate_timer.Paused = true;
                timer_status = false;
                print( "Timer Paused" );
        }
}
function gatesystem()
{
    local gatestatus = 0;
        for ( local i = 0; i < GetMaxPlayers(); i++ ) 
        {
        local player = FindPlayer( i );
        if ( player )
        {
            if ( player.IsSpawned )
            {
                if ( InPoly( player.Pos.x, player.Pos.y, -276.2,-503.462,-268.675,-502.296,-268.887,-488.42,-276.471,-487.991 ) ) gatestatus = 1;                     
            }
        }
    }
        if ( gatestatus == 1 ) gate.MoveTo( Vector( -268.576, -495.99, 10.2778 ), 2800 );
    else gate.MoveTo( Vector( -276.618, -495.992, 10.2778 ), 2800 );     
}
Pastebin

MacTavish

#1
Quote from: soulshaker on Feb 28, 2015, 06:22 AMWell, there is already a automatic gate function released, but for me its not automatic(you have to press keys to open/close gate). So I tried making something which is really automatic.

http://pastebin.com/VP1ZrJUn
What if more than 1 players will move on same poly... I am not sure about it cause problem or not :/

Edit: You've made loop so perhaps its works well i will test it :)

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

Sebastian

Why don't you guys create a whole system for those gates, some functions and events ?!
Generalize it, don't let it the same example (mansion's gate), forever.
Let the player choose where their gates will be positionated, and where they will move.



I see it like this:
CreateMovableGate( model, world, pos, rot, alpha, nextPos, newRot )model = model ID of object
world = world ID
pos = x,y,z
rot = rotation x,y,z (maybe w)
newPos = the new position, where the gate will move
newRot = the new rotation, where the gat will move

And some events too:
onGateOpened( player, gateID )onGateClosed( player, gateID )
This was just an example, I hope you understand the idea.

.

Quote from: sseebbyy on Feb 28, 2015, 08:31 AMWhy don't you guys create a whole system for those gates, some functions and events ?!

I'm actually incorporating this in my Squirrel plugin :D
.

Sebastian

Hehe, nice ! :)
But a version using default functions will also be good, I guess.

MacTavish

I Hope Vcmp will break the record of Samp in the future

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

Sk

#6
great work but you are going through the loop 100 times which i believe is not wise thing to do
so you can use a global array to decrease the loop size and to stop the timer when no one is online or spawned.
So you can do something like this.
Create a global variable for array and timer.
function onScriptLoad()
{
        gate <- CreateObject( 310, 1, -276.618, -495.992, 10.2778, 255 );
        gate_timer<-NewTimer("gatesystem", 500, 0 );
        //Using Global array for timer so that timer should not be running when no player is online of no one spawned.
gate_timer.Stop(); // No one will be spawned on startup so no need for that.
        g_Spawned<-array(1,101); // This is the global array to insert the id of player who are spawned.
}
function onPlayerSpawn(player){
//and then onPlayerSpawn insert the id of spawned player by calling Add_p function
Add_p(player);
//you stuff after this.

}
// and then call this function Remove_p where player dies or leaves.
 onPlayerDeath( player, reason )
{
Remove_p(player);
//you stuff after this.

}
onPlayerKill( killer, player, reason, bodypart )
{
Remove_p(player);
//you stuff after this.

}
onPlayerTeamKill( killer, player, reason, bodypart )
{
Remove_p(player);
//you stuff after this.

}
onPlayerPart(player,reason)
{
Remove_p(player);
//you stuff after this.

}
function Add_p(player)
{
local id = player.ID;
g_Spawned.push(id); //pushing the id of player who just spawned.
if (gate_timer.Paused) // if the timer was stopped or wasn't started.
{
gate_timer.Start(); //Start the timer as one or someone is spawned.
}
}
function Remove_p(player)
{
local id = player.ID;
if (g_Spawned.find(id)) //if player's id was inside the spawned players array.
{
local i = g_Spawned.find(id);
// array.find(val) returns the slot or index of the array where val is present.
g_Spawned.remove(i);
// removing player's id from the array.
}
//now to decrease further lag we will stop the timer if no one is spawned.
if (g_Spawned.len()<2) gate_timer.Stop();
//Smaller then 2 equals 1 which means no one is spawned and now we can stop the timer.
//as i have already created an extra value in array 101 at index 0.
}

//So now you can use your function something like this.
function gatesystem()
{
local gatestatus = 0;
foreach(i in g_Spawned ) //This will get the value from each array.
{
if (i < 100)
{
local player = FindPlayer( i);
if ( player )
{
if ( InPoly( player.Pos.x, player.Pos.y, -276.2,-503.462,-268.675,-502.296,-268.887,-488.42,-276.471,-487.991 ) ) gatestatus = 1;
}
}
}
if ( gatestatus ) gate.MoveTo( Vector( -268.576, -495.99, 10.2778 ), 2800 );
else gate.MoveTo( Vector( -276.618, -495.992, 10.2778 ), 2800 );     
}
And i removed that == 1 condition because anything which is not 0, false and null will return true.
Note : I haven't tested it but if somewhere i have some error please inform me(i may be wrong somewhere).
http://pastebin.com/EEeAC5XV just in case you like it short.

ysc3839

You needn't use a timer. Just use onPlayerMove event!

soulshaker

Quote from: ysc3839 on Feb 28, 2015, 02:36 PMYou needn't use a timer. Just use onPlayerMove event!
I believe that onPlayerMove event create too much lag, thats why I used timer :)

MacTavish

Yea as S.L.C said
Quote from: S.L.C on Dec 13, 2014, 07:17 AMThis will lag the sh!t out of a full server :D Imagine 50 players moving which can almost cause 10 onPlayerMove() calls per second for each player. Therefore 50 * ~10 = ~500 Gate1() calls per second. I know it's not much but think of it when you start adding more of those gates and other things like that where you depend on the onPlayerMove() function. There wont be any room left for the rest of the code.

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

MacTavish

I've Got This Error Seems Timer Not Created

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

.

Quote from: Beztone on Feb 28, 2015, 04:53 PMI've Got This Error Seems Timer Not Created

Move the code at line 125 above the one at 124. The order of operations is wrong in that case since the 'gate_timer' variable is created after it's being used.
.

Sk

Quote from: S.L.C on Feb 28, 2015, 05:17 PM
Quote from: Beztone on Feb 28, 2015, 04:53 PMI've Got This Error Seems Timer Not Created

Move the code at line 125 above the one at 124. The order of operations is wrong in that case since the 'gate_timer' variable is created after it's being used.
Exactly just as S.L.C said how can you use the variable even before defining it?
Just to Make Sure every thing is working now i have tested it on blank script works fine for me.

MacTavish

#13
Well Thanks @S.L.C and Sk
 i am facing another problem now the gate function is continuesly getting calledGate Closed
Gate Opened
Gate Closed
Gate Opened
Gate Closed

Continuesly :-[

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

soulshaker

As Sk said that the timer may lag the server, I update the function regarding that. Have a look on it :)