07.06.2012, 06:39
(
Last edited by MP2; 29/06/2013 at 03:26 PM.
)
mGates 2.1
by MP2
Introductionby MP2
This include makes it EXTREMELY easy to create automatic gates, with only one single line of code required!
I made this for my own server that I am developing, but I like to share, so here it is.
It also has an option for you to deny access for certain players based on things such as their team, so if you want to make a gate that only opens for the police, it's extremely easy.
Perhaps with this we can reduce the number of 'HELP PLS MY GATE WHY HE NO OPEN?? [REP++++]' topics.
It also makes use of a custom iterator in foreach() - so is very efficient! No big loops.
[ame]http://www.youtube.com/watch?v=PcOD5AThnFE[/ame]
Code used in video (messy long version - this should be spread over multiple lines to make it clearer to read/edit, which it will be later in the topic):
pawn Code:
CreateAutomaticGate(968, 1544.692993, -1630.822509, 13.08, 0.000000, 90.000000, 90.000000, 1544.692993, -1630.822509, 13.08+0.01, 0.000000, 10.000000, 90.000000, 1544.6627, -1627.4036, 13.1099, 20.0, 0.003);
Usage
==================================================
FUNCTIONS
==================================================
To create an automatic gate (opens when players are near)
CreateAutomaticGate(modelid, Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz, Float:x2, Float:y2, Float:z2, Float:rx2, Float:ry2, Float:rz2, Float:trigx, Float:trigy, Float:trigz, Float:trig_range, Float:movespeed, condition=0);
Parameters:
modelid - The object model to use for the gate
x y z - The position of the CLOSED gate
rx ry rz - The rotation of the CLOSED gate
x2 y2 z2 - The position of the OPEN gate
rx2 ry2 rz2 - The rotation of the OPEN gate
Set ALL to -1000 or the same as rx, ry, rz to NOT change rotation
trigx trigy trigz - The trigger point. Players in range of this point will
cause the gate to open (or close if nobody in range)
trig_range - The range from the trigger point (trigx trigy trigz) at which
players will cause the gate to open (or close if not in range)
movespeed - The movement speed for the object. The differs drastically for
rotating and sliding gates. See tutorial HERE
condition - If condition is set to 1, you must return 1 in the the
OnPlayerRequestGate callback for the player to pass,
there you can check if the player meets a certain condition,
for example you may only want cops to be able to trigger the
gate to open. If set to 0 the gate will open for anyone.
Do note however, someone that is NOT a cop could still get in
after a cop triggers it to open.
This function has many parameters. Typing 'CreateAutomaticGate(' in pawno will pop-up a syntax box that will update in real-time as you specify parameters. In order for this to work you must include the .inc file and re-open pawno.
Returns:
The ID of the gate that was created (NOTE: NOT the object ID!)
-1 if the gate wasn't created (limit reached)
Example: Create a barrier at the LSPD that will have a condition (cops only):
pawn Code:
new BARRIER_LSPD;
public OnGameModeInit()
{
// LSPD Barrier
BARRIER_LSPD = CreateAutomaticGate(
968, // Model
1544.69, -1630.8739, 13.0728, // Closed Position
0.0, 90.0, 90.0, // Closed Rotation
1544.69, -1630.8739, 13.0728+0.001, // Open Position
0.0, 00.0, 90.0, // Open Rotation
1542.87, -1627.0, 13.37, // Trigger Point Position
20.0, // Trigger Point Radius
0.001, // Object move speed
true // Condition? True/False
);
return 1;
}
You should also comment them (//) so you know which is which - as there are quite a few parameters to pass.
==================================================
To force a gate to open:
OpenGate(gateid);
Parameters:
gateid - The ID of the gate. Returned by CreateAutomaticGate().
Returns:
-1 - Gate doesn't exist
0- Gate already open(ing)
1 - Gate opening - success!
Example:
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
if(!strcmp(cmdtext, "/LSPD", true))
{
OpenGate(BARRIER_LSPD);
return 1;
}
return 0;
}
To force a gate to close:
CloseGate(gateid);
Parameters:
gateid - The ID of the gate. Returned by CreateAutomaticGate().
Returns:
-1 - Gate doesn't exist
0- Gate already close(ing)
1 - Gate closing - success!
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
if(!strcmp(cmdtext, "/LSPD", true))
{
CloseGate(BARRIER_LSPD);
return 1;
}
return 0;
}
To check the state of a gate (open/closed/opening/closing):
GateStatus(gateid);
Parameters:
gateid - The ID of the gate. Returned by CreateAutomaticGate().
Returns:
-1 - Gate doesn't exist
GATE_STATUS_CLOSED 0 - Gate is fully closed
GATE_STATUS_OPENING 1 - Gate is opening, not yet fully open
GATE_STATUS_OPEN 2 - Gate is fully open
GATE_STATUS_CLOSING 3 - Gate is closing, not yet fully closed
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
if(!strcmp(cmdtext, "/LSPD", true))
{
if(GateStatus(BARRIER_LSPD) == GATE_STATUS_OPEN || GateStatus(BARRIER_LSPD) == GATE_STATUS_OPENING) return SendClientMessage(playerid, COLOR_RED, "Gate is already open!");
OpenGate(BARRIER_LSPD);
return 1;
}
return 0;
}
To remove/delete/destroy a gate:
DestroyGate(gateid);
or
DeleteGate(gateid);
or
RemoveGate(gateid);
Parameters:
gateid -
The ID of the gate to destroy/delete. Returned by CreateAutomaticGate().
Returns:
-1 - Invalid gate ID
0 - Gate doesn't exist
1 - Gate was destroyed successfully
Example:
pawn Code:
new BARRIER_LSPD;
public OnGameModeInit()
{
BARRIER_LSPD = CreateAutomaticGate(...);
return 1;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
if(!strcmp(cmdtext, "/LSPD", true))
{
DestroyGate(BARRIER_LSPD);
return 1;
}
return 0;
}
CALLBACKS
==================================================
public OnGateOpenClose(gateid, openclose, Float:gatex, Float:gatey, Float:gatez)
OnGateOpenClose is called when a gate opens or closes.
It is called 4 times per gate: when the gate starts to open, when it's fully open, when it starts to close and when it's fully closed again.
'gateid' is the ID of the gate that triggered this callback (returned by CreateAutomaticGate).
GATE_STATUS_CLOSED 0 - Gate is fully closed
GATE_STATUS_OPENING 1 - Gate is opening, not yet fully open
GATE_STATUS_OPEN 2 - Gate is fully open
GATE_STATUS_CLOSING 3 - Gate is closing, not yet fully closed
gatex, gatey, and gatez are the coordinates of the gate currently (whether opening or closing) - can be used, for example, to play a sound for players near-by
==================================================
public OnPlayerRequestGate(playerid, gateid)
The core of the 'condition' system.
Returning 0/false in this callback will deny the player access to the gate.
Is it important to note, however, if one player triggers the gate open, it opens for everyone, even those without access. This is unavoidable unless the gate objects are per-player, which would not look or work well.
See the second post below for detailed explanation of condition system.
==================================================
public OnPlayerTriggerGate(playerid, gateid)
Called when a player triggers a gate to open.
One use I had for this was playing the 'They'll give a pilots license to anybody these days' sound when players triggered the airport gates to open.
This callback may not be needed, as the code could simply be put under OnPlayerRequestGate, but this makes things easier.
==================================================
Download
http://pastebin.com/jS0mWwaL
Save as mGates.inc and place in pawno/include/ folder.
Installation
You will need to download foreach and y_hooks (YSI) to use this include. Put all the .inc files (and the YSI folder) in your \pawno\include folder.
Add '#include <mGates>' under '#include <a_samp>' in your script.
NOTE: It's mGates, not mgates.
If you're going to create more than 100 gates, re-define MAX_GATES in mGates.inc.
You can also set how often the gates are all checked. This is once a second by default.
Use it (see usage).
Changelog
See in file.
Credits
Big thanks to Y_Less for foreach and y_hooks (YSI). foreach() makes this include very efficient.
Thanks to iGetty for testing
Enjoy, and please report any bugs.