Monday, November 29, 2010

SAMP Scripting Tutorial: Save/Load Settings/Stats using YSI (y_ini)

Here is the code

#define WHITE 0xFFFFFFFF
#define BLUE 0xFF0000FF

#include <a_samp>
//-------------------------------------------------------//
//  You must include this file. You can download YSI here: http://forum.sa-mp.com/showthread.php?p=884701
#include <YSI/y_ini>
//------------------------------------------------------//
#include "../include/gl_common.inc"

main()
{
 print("\n----------------------------------");
 print(" Tutorial by TheDarkJoker94");
 print("----------------------------------\n");
}

#define Skinid 1
#define Money 2
#define Faction 3 
#define Job 4
#define AdminLevel 5
#define Level 6
///////////////////*/
#define Civilian 0
#define Police 1
#define Taxi 2
//-----------------//
new bool:newPlayer,bar,lspddoor,armour;
new player[MAX_PLAYERS][7]; // The settings values are stored here
new setts[7][] = {"password","skin","money","faction","job","AdminLevel","level"}; // An array containing the textual identifiers
new playerPass[MAX_PLAYERS][128]; // The players passwords are stored in this array
new Text:mytext;

public OnGameModeInit()
{
 // Don't use these lines if it's a filterscript
 SetGameModeText("SAMP Tutorial");
 AddPlayerClass(0,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(165,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(166,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(280,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(282,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(286,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(287,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(163,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(164,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 AddPlayerClass(283,1544.7887,-1675.4630,13.5591,88.8707,38,999,0,0,0,0);
 
 AddStaticVehicle(463,1538.0513,-1694.6932,13.0872,181.3842,11,11);
 //LoadStaticVehiclesFromFile("vehicles/myplanes.txt"); // Only if you have myplanes.txt
 
 //Fences
 CreateObject(992, 1544.6911621094, -1622.0278320313, 14.102454185486, 0, 0, 90);
 CreateObject(992, 1544.6866455078, -1619.5272216797, 14.110140800476, 0, 0, 90);
 CreateObject(992, 1545.2962646484, -1634.3321533203, 14.109854698181, 0, 0, 90);
 CreateObject(992, 1545.2634277344, -1637.7028808594, 14.53718662262, 0, 0, 90);
 
 //Barrier
 bar = CreateObject(968, 1544.6640625, -1623.9732666016, 12.989388465881, 352, 270, 90);
 
 CreateObject(2634,266.29434204,83.02305603,1001.18328857,0.00000000,0.00000000,270.00000000); //object(ab_vaultdoor) (1)
 CreateObject(2634,266.28244019,87.49037933,1001.18328857,0.00000000,0.00000000,270.00000000); //object(ab_vaultdoor) (2)
 
 lspddoor = CreateObject(976,250.19511414,72.58469391,1002.64929199,0.00000000,0.00000000,180.00000000); //object(phils_compnd_gate) (1)
 armour = CreatePickup(1242,1,254.2683,77.9845,1003.6406,0);
 
 //ConnectNPC("Dave","npc_walk"); // Only if you have npc_walk
 return 1;
}

forward Spawn(playerid);
public OnPlayerRequestClass(playerid, classid)
{
 if(!IsPlayerNPC(playerid))
 {
  if(newPlayer)
  {
   SetPlayerPos(playerid, 1544.7887,-1675.4630,13.5591);
   SetPlayerFacingAngle(playerid,90.0);
   SetPlayerCameraPos(playerid, 1541.5293,-1675.4012,13.5527);
   SetPlayerCameraLookAt(playerid, 1544.7887,-1675.4630,13.5591);
  }
  else
  {
   SetTimerEx("Spawn",1,false,"i",playerid);
  }
 }
 return 1;
}

/////////////////////////////////--------------YSI--------------//////////////////////////////////////////////
forward LoadSettings(playerid, name[], value[]);
public LoadSettings(playerid, name[], value[])
{
 for(new i=0; i<7; i++)
 {
  if(i==0)
  { 
   // Read the value of the textual identifier(setts[i]) and save it in playerPass[playerid] (in this case the player's password is saved in the playePass array
   INI_String(setts[i],playerPass[playerid],128);
  }
  else 
  {
   // Same here but the values are integers now
   INI_Int(setts[i],player[playerid][i]);
  }
 }
 return 0;
}
//------------------------------------------------------------------------------------------//

public OnPlayerConnect(playerid)
{
 if(!IsPlayerNPC(playerid))
 {
  newPlayer = false;
  TogglePlayerSpectating(playerid,1);
  new string[256], name[MAX_PLAYER_NAME];
  GetPlayerName(playerid,name,sizeof(name));
  format(string,256,"~r~Hello %s !",name);
  GameTextForPlayer(playerid,string,10000,6);
  format(string,256,"Players/%s.ini",name);
  MyTextDraw(playerid);
  if(fexist(string))
  {
   // Parse the file using LoadSettings function and send additional parameters to the function (in this case, playerid)
   INI_ParseFile(string,"LoadSettings", .bExtra=true, .extra=playerid); 
   SetPlayerScore(playerid,player[playerid][Level]);
   ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Welcome! Please Login!","Login","Exit");
  }
  else
  {
   ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Welcome! Please Register!","Register","Exit");
   newPlayer = true;
  }
 }
 return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
 SavePlayerSettings(playerid);
 return 1;
}

public OnPlayerSpawn(playerid)
{
 if(!IsPlayerNPC(playerid))
 {
  if(newPlayer)
  {
   player[playerid][Skinid] = GetPlayerSkin(playerid);
  }
  GivePlayerMoney(playerid,player[playerid][Money]);
  SetPlayerSkin(playerid, player[playerid][Skinid]);
  TextDrawHideForPlayer(playerid,mytext);
  TextDrawDestroy(mytext);
 }
 else SetPlayerSkin(playerid, 167);
 return 1;
}

forward InitPos();
public OnPlayerCommandText(playerid, cmdtext[])
{
 new cmd[128],idx;
 cmd = strtok(cmdtext,idx);
 if(strcmp(cmd,"/tele",true)==0)
 {
  new tmp[128];
  tmp = strtok(cmdtext,idx);
  if(strlen(tmp)==0) return SendClientMessage(playerid,WHITE,"Usage: /tele 1");
  if(strval(tmp)==1)
   SetPlayerPos(playerid,1885.7004,-2592.2048,13.5469);
  else return SendClientMessage(playerid,WHITE,"Usage: /tele 1");
  return 1;
 }
 if(strcmp(cmdtext,"/veh",true)==0)
 {
  ShowPlayerDialog(playerid,3,DIALOG_STYLE_LIST,"Vehicles","1.Rhino\n2.Firetruck\n3.Infernus\n4.Turismo\n5.Dumper\n6.Sparrow","Spawn","Exit");
  return 1;
 }
 
 if(strcmp(cmdtext,"/openbarrier",true)==0)
 {
  SetObjectPos(bar,1544.6640625, -1623.9732666016, 12.989388465881);
  SetObjectRot(bar,352, 0, 90);
  SetTimer("InitPos",5000,false);
  return 1;
 }
 
 if(strcmp(cmdtext,"/myfaction",true)==0)
 {
  switch(player[playerid][Faction])
  {
   case 0: SendClientMessage(playerid,WHITE,"You are Civilian!");
   case 1: SendClientMessage(playerid,WHITE,"You are a Police Officer!");
   case 2: SendClientMessage(playerid,WHITE,"You are TaxiDriver!");
  }
  return 1;
 }
 
 if(strcmp(cmdtext,"/anim",true)==0)
 {
  ApplyAnimation(playerid,"ped","FALL_collapse",4.0,0,0,0,0,0);
  return 1;
 }
 
 return 0;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
 new Float:armr; GetPlayerArmour(playerid, armr);
 if(pickupid == armour && armr != 100.0)
 { 
  SetPlayerArmour(playerid,100.0);
  PlayerPlaySound(playerid,1150,0.0,0.0,0.0);
 }
 return 1;
}

public OnPlayerUpdate(playerid)
{
 // Infinite ammo
 if(GetPlayerAmmo(playerid) <= 10) SetPlayerAmmo(playerid,38,989);
 if(IsPlayerInRangeOfPoint(playerid,3.0,246.4075,72.4807,1003.6406))
 {
  MoveObject(lspddoor,250.19433594,72.58398438,1005.39929199,4.0);
  SetTimer("InitPos",2000,false);
 }
 return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
 new Float:x,Float:y,Float:z;
 switch(dialogid)
 {
  case 3:
  {
   if(response==1)
   {
    switch(listitem)
    {
     case 0:
     {
      GetPlayerPos(playerid,x,y,z);
      CreateVehicle(432,x-3.0,y-3.0,z,90.0,1,1,300);
     }
     case 1:
     {
      GetPlayerPos(playerid,x,y,z);
      CreateVehicle(407,x-3.0,y,z,0.0,1,3,300);
     }
     case 2:
     {
      GetPlayerPos(playerid,x,y,z);
      CreateVehicle(411,x-3.0,y,z,0.0,1,0,300);
     }
     case 3:
     {
      GetPlayerPos(playerid,x,y,z);
      CreateVehicle(451,x-3.0,y,z,0.0,0,1,300);
     }
     case 4:
     {
      GetPlayerPos(playerid,x,y,z);
      CreateVehicle(406,x-3.0,y,z,0.0,1,1,300);
     }
     case 5:
     {
      GetPlayerPos(playerid,x,y,z);
      CreateVehicle(469,x-3.0,y,z,0.0,1,1,300);
     }
    }
   }
  }
  case 1:
  {
   if(response==1)
   {
    if(strcmp(playerPass[playerid],inputtext,false,strlen(inputtext))!=0)
    {
     SendClientMessage(playerid,WHITE,"Invalid Password!");
     ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Welcome! Please Login!","Login","Exit");
    }
    else TogglePlayerSpectating(playerid,0);
   }
   else Kick(playerid);
  }
  case 2:
  {
   if(response==1)
   {
    strcat(playerPass[playerid],inputtext,128);
    TogglePlayerSpectating(playerid,0);
   }
   else Kick(playerid);
  }
 }
 return 1;
}

/////////////////////////////////--------------YSI--------------//////////////////////////////////////////////
SavePlayerSettings(playerid)
{
 new string[256],name[MAX_PLAYER_NAME];
 GetPlayerName(playerid,name,sizeof(name));
 format(string,256,"Players/%s.ini",name);
 // Open the file (if doesn't exist it will be created)
 new INI:acc = INI_Open(string);
 for(new i=0; i<7; i++)
 {
  if(i==0)
   INI_WriteString(acc,setts[i],playerPass[playerid]); // Write the passwords to the player's file
  else INI_WriteInt(acc,setts[i],player[playerid][i]); // Write the other values to the player's file
 }
 INI_Close(acc); // Close the file
}
//-----------------------------------------------------------------------------------------//

public Spawn(playerid)
{
 SpawnPlayer(playerid);
}

public InitPos()
{
 SetObjectPos(bar,1544.6640625, -1623.9732666016, 12.989388465881);
 SetObjectRot(bar,352,270,90);
 MoveObject(lspddoor,250.19511414,72.58469391,1002.64929199,4.0);
}

MyTextDraw(playerid)
{
 mytext = TextDrawCreate(150.0,360.0,"Welcome to my SAMP Server");
 TextDrawColor(mytext,BLUE);
 TextDrawSetOutline(mytext,1);
 TextDrawSetShadow(mytext,0);
 TextDrawFont(mytext,3);
 TextDrawLetterSize(mytext,0.7,1.2);
 TextDrawShowForPlayer(playerid,mytext);
}

11 comments:

  1. hello!! GOOD Work!!

    Please you make Origin register/login for save the places

    Information i don't like false register / login that like true register / login and save other places.

    i need like /stats,/help,/admin,/buyhouse,/carkey,/unlock,/lock,/enter,/exit,/buy,/givecash 5million,/park,/confire,/jail,/unjail,/usepot,/bank,/fill,/upgrade,/myfaction,/pm,/ooc,/me,/report

    i need like "admin",/kick,/ban,/setlevel,/report,/weathers,/veh,/wanted.. i want need more somethings.

    Please you make if kick wrong password

    My work hard for wrong pawno following somethings the sa-mp today =D...

    ReplyDelete
  2. umm... where u put the YSI folder?

    ReplyDelete
  3. why does it say "unknown directive"????

    ReplyDelete
  4. it says "local variable "player" shadows a variable at a preceding level". how to I fix that?

    ReplyDelete
  5. Why not MySql? Saving is much more easy and secure :)

    ReplyDelete