New .inc SourcePawn samples

This commit is contained in:
Paul Chaignon
2015-07-05 12:48:12 +02:00
parent b0073d461c
commit 849eb28b01
2 changed files with 535 additions and 0 deletions

View File

@@ -0,0 +1,246 @@
// Checkpoint Manager
// (c) João Pedro Lopes, All right's reserved
//
// Feel free to change any line above, as long as you post the 'fix' / 'patch' at the forum
// and send a copy to jplopes@live.com.pt
/* natives
native CreateCheckpoint(ownerid, chpid, Float:posX, Float:posY, Float:posZ, Float:size); // Creates a checkpoint
native SetCheckpointInterior(chpid, interiorid); // Changes the checkpoint interior
native SetCheckpointVirtualWorld(chpid, VirtualWorldID); // Changes the Checkpoint vWorld
native ToggleCheckpointActive(chpid, bool:active); // Deactivates / Activates the checkpoint
native ChangeCheckpointOwner(chpid, owner); // Change the checkpoint owner
native RemoveCheckpoint(chpid); // Removes the specified checkpoint
native StartCheckpointSeeking(); // Starts seeking for each individual
native StopCheckpointSeeking(); // Stops the system
native VerifyCheckpoint(playerid); // Place this at OnPlayerEnterCheckpoint
*/
// Function Forwards
forward public OnCheckpointEnter(playerid, checkpointid);
#if defined _CHECKPOINT_MANAGER_INCLUDED
#endinput
#endif
#define _CHECKPOINT_MANAGER_INCLUDED
#pragma library CheckpointManager
#include <a_samp>
#define MAX_CHECKPOINTS 200
#define CHECKPOINT_SEEKER_DELAY 300
#define GLOBAL_OWNER_ID -1
// CHECKPOINT ENUMERATION
enum _checkpointEnum{
_chp_populated, // Is this slot of the memory populated?
_chp_id, // The ID of the checkpoint
_chp_owner, // The ID of the player who this checkpoint is visible too
Float:_chp_posX, // The X position of this checkpoint
Float:_chp_posY, // The Y position of this checkpoint
Float:_chp_posZ, // The Z position of this checkpoint
Float:_chp_size, // The checkpoint size
Float:_chp_viewDistance, // The checkpoint view distance
bool:_chp_active, // Is this checkpoint active?
_chp_interior_id, // The interior id of this checkpoint
_chp_world_id // The world id of this checkpoint
};
// DATA ARRAYS
new _checkpoints[MAX_CHECKPOINTS][_checkpointEnum];
new _p_VisibleCheckpoint[MAX_PLAYERS];
new _chp_manager_timer_id;
// DATA VARIABLES
new _totalCheckpoints;
// --------------------------------------------------------------------------------------------------------
// Creates a new checkpoint with some initial data
stock CreateCheckpoint(__ownerid, __chpid, Float:__posX, Float:__posY, Float:__posZ, Float:__size){
// Max checkpoint reached?
if(_totalCheckpoints == MAX_CHECKPOINTS) return 0;
// First checkpoint? Setting everything to unpopulated
if(!_totalCheckpoints){
for(new i; i < MAX_PLAYERS; i++) _p_VisibleCheckpoint[i] = -1;
for(new i; i < MAX_CHECKPOINTS; i++){
_checkpoints[i][_chp_populated] = false;
}
// Sending the Initialization Info
printf("[Checkpoint Manager : Version 0.1.1b] System Initialized...", __chpid);
}
// Getting the first open slot
new _slot;
for(new i = 0; i < MAX_CHECKPOINTS; i++){
if(!_checkpoints[i][_chp_populated]){
_slot = i;
break;
}
}
// Adding the new checkpoint
_checkpoints[_slot][_chp_populated] = true;
_checkpoints[_slot][_chp_id] = __chpid;
_checkpoints[_slot][_chp_owner] = __ownerid;
_checkpoints[_slot][_chp_posX] = __posX;
_checkpoints[_slot][_chp_posY] = __posY;
_checkpoints[_slot][_chp_posZ] = __posZ;
_checkpoints[_slot][_chp_size] = __size;
_checkpoints[_slot][_chp_viewDistance] = 50.0;
_checkpoints[_slot][_chp_active] = true;
_checkpoints[_slot][_chp_interior_id] = 0;
_checkpoints[_slot][_chp_world_id] = 0;
printf("[Checkpoint Manager] Checkpoint created (%d) at slot %d", __chpid, _slot);
printf("Checkpoint Position: { %f, %f, %f }", _checkpoints[_slot][_chp_posX], _checkpoints[_slot][_chp_posY], _checkpoints[_slot][_chp_posZ]);
_totalCheckpoints++;
return 1;
}
//---------------------------------------------------------------------------------------------
stock SetCheckpointInterior(__chpid, __interiorid){
new _slot = __ChpSlotByID(__chpid);
if(_slot > -1){
// Valid slot?
_checkpoints[_slot][_chp_interior_id] = __interiorid;
return 1;
}
return 0;
}
//---------------------------------------------------------------------------------------------
stock SetCheckpointVirtualWorld(__chpid, __virtual_world_id){
new _slot = __ChpSlotByID(__chpid);
if(_slot > -1){
_checkpoints[_slot][_chp_world_id] = __virtual_world_id;
return 1;
}
return 0;
}
stock ToggleCheckpointActive(__chpid, bool:__active){
new _slot = __ChpSlotByID(__chpid);
if(_slot > -1){
_checkpoints[_slot][_chp_active] = __active;
return 1;
}
return 0;
}
stock ChangeCheckpointOwner(__chpid, __owner){
new _slot = __ChpSlotByID(__chpid);
if(_slot > -1){
_checkpoints[_slot][_chp_owner] = __owner;
return 1;
}
return 0;
}
stock RemoveCheckpoint(__chpid){
new _slot = __ChpSlotByID(__chpid);
if(_slot > -1){
// Deleting the checkpoint
_checkpoints[_slot][_chp_populated] = false;
_checkpoints[_slot][_chp_id] = -1;
_checkpoints[_slot][_chp_owner] = 255;
_checkpoints[_slot][_chp_posX] = -1;
_checkpoints[_slot][_chp_posY] = -1;
_checkpoints[_slot][_chp_posZ] = -1;
_checkpoints[_slot][_chp_size] = -1;
_checkpoints[_slot][_chp_viewDistance] = -1;
_checkpoints[_slot][_chp_active] = false;
_checkpoints[_slot][_chp_interior_id] = -1;
_checkpoints[_slot][_chp_world_id] = -1;
_totalCheckpoints--;
printf("\n[Checkpoint Manager] Checkpoint removed (ID: %d)", __chpid);
return 1;
}
return 0;
}
//---------------------------------------------------------------------------------------------
// Gets the checkpoint slot by id
stock __ChpSlotByID(__chpid){
for(new i; i < MAX_CHECKPOINTS; i++){
if(_checkpoints[i][_chp_id] == __chpid) return i;
}
return -1;
}
forward CheckpointSeeker();
stock StartCheckpointSeeking(){
_chp_manager_timer_id = SetTimer("CheckpointSeeker", CHECKPOINT_SEEKER_DELAY, 1);
return 1;
}
stock StopCheckpointSeeking(){
KillTimer(_chp_manager_timer_id);
return 1;
}
public CheckpointSeeker(){
new Float:__posX, Float:__posY, Float:__posZ;
new __interior;
new __virtualWorld;
for(new i; i < MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i)) continue;
GetPlayerPos(i, Float:__posX, Float:__posY, Float:__posZ);
// Is the player near a checkpoint?
if(_p_VisibleCheckpoint[i] > -1)
{
// If the player is no longer near that point
if(__posX < (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posX] - _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
|| __posX > (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posX] + _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
|| __posY < (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posY] - _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])
|| __posY > (_checkpoints[_p_VisibleCheckpoint[i]][_chp_posY] + _checkpoints[_p_VisibleCheckpoint[i]][_chp_viewDistance])){
DisablePlayerCheckpoint(i);
_p_VisibleCheckpoint[i] = -1;
}
}
else
{
// Getting the player Interior and virtual world
__interior = GetPlayerInterior(i);
__virtualWorld = GetPlayerVirtualWorld(i);
// Looking for a new checkpoint
for(new j = 0; j < MAX_CHECKPOINTS; j++){
if(!_checkpoints[j][_chp_populated]) continue;
if((_checkpoints[j][_chp_owner] != i) && (_checkpoints[j][_chp_owner] != -1)) continue;
if(_checkpoints[j][_chp_interior_id] != __interior) continue;
if(_checkpoints[j][_chp_world_id] != __virtualWorld) continue;
if(__posX > (_checkpoints[j][_chp_posX] - _checkpoints[j][_chp_viewDistance])
&& __posX < (_checkpoints[j][_chp_posX] + _checkpoints[j][_chp_viewDistance])
&& __posY > (_checkpoints[j][_chp_posY] - _checkpoints[j][_chp_viewDistance])
&& __posY < (_checkpoints[j][_chp_posY] + _checkpoints[j][_chp_viewDistance])){
SetPlayerCheckpoint(i, _checkpoints[j][_chp_posX], _checkpoints[j][_chp_posY], _checkpoints[j][_chp_posZ], _checkpoints[j][_chp_size]);
_p_VisibleCheckpoint[i] = j;
break;
}
}
}
}
return 1;
}
stock VerifyCheckpoint(__playerid){
if(_p_VisibleCheckpoint[__playerid] >= 0){
OnCheckpointEnter(__playerid, _checkpoints[_p_VisibleCheckpoint[__playerid]][_chp_id]);
return 1;
}
return 0;
}

View File

@@ -0,0 +1,289 @@
/* mFile 1.1 by Minokon
*
* (c) Copyright by Minokon 2010
* (c) y_files Copyright by Y_Less 2010
*
*/
#if defined _mfile_included
#endinput
#endif
#define _mfile_included
//Defines
#define MAX_LINE_SIZE 80 //Max lenght of one line in file
#define MAX_KEY_SIZE 50 //Max lenght of key in line (to =)
#define MAX_VALUE_SIZE 50 //Max lenght of value in line (past =)
#define TMP_FILE "tmpfile.txt"
//Natives
/*
native mCreateFile(const file[]);
native mRemoveFile(const file[]);
native mClearFile(const file[]);
native bool:mFileExist(const file[]);
native mRenameFile(const file[], const newname[]);
native mFindFile(const name[], dest[], &index, sizename=sizeof dest);
native mCreateDir(const name[]);
native mRenameDir(const dir[], const newname[]);
native mFindDir(const name[], dest[], &index, sizename=sizeof dest);
native mHowLines(const file[]);
native mRemoveNewLine(string[]); //removes "\n" from string
native mGetString(const file[], const key[], bool:ignorecase=false);
native mGetInt(const file[], const key[], bool:ignorecase=false);
native mGetFloat(const file[], const key[], &Float:value);
native mSetString(const file[], const key[], const new_value[]);
native mSetInt(const file[], const key[], new_value);
native mSetFloat(const file[], const key[], Float:new_value);
native mIsSet(const file[], const key[], bool:ignorecase=false);
native mUnSet(const file[], const key[]);
native mIsFileEmpty(const file[]);
native y_files
*/
//y_files by Y_Less Functions
native ffind(const pattern[], filename[], len, &idx);
native frename(const oldname[], const newname[]);
native dfind(const pattern[], filename[], len, &idx);
native dcreate(const name[]);
native drename(const oldname[], const newname[]);
//Forwards
forward OnFileCreated(file[]);
forward OnFileRemoved(file[]);
forward OnFileCleared(file[]);
forward OnFileRenamed(oldname[], newname[]);
forward OnDirCreated(dir[]);
forward OnDirRenamed(oldname[], newname[]);
stock mCreateFile(const file[])
{
if(fexist(file)) fremove(file);
new File:mfile = fopen(file, io_write);
fclose(mfile);
CallLocalFunction("OnFileCreated", "s", file);
return 1;
}
stock mRemoveFile(const file[])
{
CallLocalFunction("OnFileRemoved", "s", file);
return fremove(file);
}
stock mClearFile(const file[])
{
if(!fremove(file)) return 0;
new File:mfile = fopen(file, io_write);
fclose(mfile);
CallLocalFunction("OnFileCleared", "s", file);
return 1;
}
stock bool:mFileExist(const file[])
if(fexist(file)) return true; //dla ciekawych: fexist nie zwraca booleanu
else return false;
stock mRenameFile(const file[], const newname[])
{
CallLocalFunction("OnFileRenamed", "ss", file, newname);
return frename(file, newname);
}
stock mFindFile(const name[], dest[], &index, sizename=sizeof dest)
return ffind(name, dest, sizename, index);
stock mCreateDir(const name[])
{
CallLocalFunction("OnDirCreated", "s", name);
return dcreate(name);
}
stock mRenameDir(const dir[], const newname[])
{
CallLocalFunction("OnDirRenamed", "ss", dir, newname);
return drename(dir, newname);
}
stock mFindDir(const name[], dest[], &index, sizename=sizeof dest)
return dfind(name, dest, sizename, index);
stock mHowLines(const file[])
{
new lines, str[MAX_LINE_SIZE];
new File:mfile = fopen(file, io_read);
while(fread(mfile, str)) lines++;
fclose(mfile);
return lines;
}
stock mRemoveNewLine(string[])
{
new pos = strfind(string, "\n");
if(pos != -1)
{
strdel(string, pos, pos+2);
return 1;
}
return 0;
}
stock mGetString(const file[], const key[], bool:ignorecase=false)
{
//Create file if not exists
if(!fexist(file))
{
new File:created = fopen(file, io_write);
fclose(created);
}
new //Variables
File:mfile = fopen(file, io_read), //Open file
str[MAX_LINE_SIZE],
str2[MAX_KEY_SIZE],
str3[MAX_VALUE_SIZE],
pos;
//Find key in file
while(fread(mfile, str))
{
pos = strfind(str, "=", ignorecase);
strmid(str2, str, 0, pos);
if(!strcmp(str2, key, ignorecase, strlen(key)+1))
{
strmid(str3, str, pos+1, strlen(str));
mRemoveNewLine(str3);
break;
}
}
fclose(mfile);
return str3;
}
stock mGetInt(const file[], const key[], bool:ignorecase=false)
return strval(mGetString(file, key, ignorecase));
stock mGetFloat(const file[], const key[], &Float:value, bool:ignorecase=false)
value = floatstr(mGetString(file, key, ignorecase));
stock mSetString(const file[], const key[], const new_value[])
{
//Create file if not exists
if(!fexist(file))
{
new File:created = fopen(file, io_write);
fclose(created);
}
new //Variables
str[MAX_LINE_SIZE],
str2[MAX_KEY_SIZE],
str3[MAX_LINE_SIZE],
bool:lFinded = false;
//Open file
new File:mfile = fopen(file, io_read);
//Create cache file
new File:tmpfile = fopen(TMP_FILE, io_write);
fclose(tmpfile);
//Open cache file
tmpfile = fopen(TMP_FILE, io_append);
format(str3, sizeof str3, "%s=%s\n", key, new_value);
while(fread(mfile, str))
{
strmid(str2, str, 0, strfind(str, "="));
if(!strcmp(str2, key))
{
fwrite(tmpfile, str3);
lFinded = true;
}
else
fwrite(tmpfile, str);
}
if(!lFinded) //if line not found
fwrite(tmpfile, str3);
//Close and remove old file
fclose(mfile);
fremove(file);
//Close cache file and rename it
fclose(tmpfile);
frename(TMP_FILE, file);
return 1;
}
stock mSetInt(const file[], const key[], new_value)
{
new str[MAX_VALUE_SIZE];
valstr(str, new_value);
mSetString(file, key, str);
return 1;
}
stock mSetFloat(const file[], const key[], Float:new_value)
{
new str[MAX_VALUE_SIZE];
format(str, sizeof str, "%f", new_value);
return mSetString(file, key, str);
}
stock bool:mIsSet(const file[], const key[], bool:ignorecase=false)
{
//If fexists return false
if(!fexist(file)) return false;
new //Variables
File:mfile = fopen(file, io_read), //Open file
str[MAX_LINE_SIZE],
str2[MAX_KEY_SIZE],
bool:finded = false;
//Find key in file
while(fread(mfile, str))
{
strmid(str2, str, 0, strfind(str, "="));
if(!strcmp(str2, key, ignorecase, strlen(key)+1))
{
finded = true;
break;
}
}
fclose(mfile);
return finded;
}
stock mUnSet(const file[], const key[])
{
if(!fexist(file)) return 0;
new //Variables
str[MAX_LINE_SIZE],
str2[MAX_KEY_SIZE];
//Open file
new File:mfile = fopen(file, io_read);
//Create cache file
new File:tmpfile = fopen(TMP_FILE, io_write);
fclose(tmpfile);
//Open cache file
tmpfile = fopen(TMP_FILE, io_append);
while(fread(mfile, str))
{
strmid(str2, str, 0, strfind(str, "="));
if(strcmp(str2, key) != 0)
fwrite(tmpfile, str);
}
//Close and remove old file
fclose(mfile);
fremove(file);
//Close cache file and rename it
fclose(tmpfile);
frename(TMP_FILE, file);
return 1;
}
stock bool:mIsFileEmpty(const file[])
{
if(!fexist(file)) return true;
new File:mfile = fopen(file, io_read);
if(flength(mfile) <= 1)
{
fclose(mfile);
return true;
}
else
{
fclose(mfile);
return false;
}
}