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);
}

Friday, November 26, 2010

SAMP Scripting Tutorial: Add NPCs | Record NPC Paths | Play Animations

Here is the code

#include <a_npc>
#define rec "npc_walking"
#define onfoot 2

main(){}

public OnRecordingPlaybackEnd() StartRecordingPlayback(onfoot,rec);
public OnNPCSpawn()
{
 SetMyPos(1521.5752,-1668.3490,13.5469);
 StartRecordingPlayback(onfoot,rec);
}
public OnPlayerStreamIn(playerid)
{
 SetTimer("SendCmd",3000,true);
}

forward SendCmd();
public SendCmd()
{
 SendCommand("/anim");
 return 1;
}[/cpp]
[cpp title="tutorial.pwn"]
// OnPlayerText
if(strcmp(cmdtext,"/anim",true)==0)
{
 ApplyAnimation(playerid,"ped","FALL_collapse",4.0,0,0,0,0,0);
 return 1;
}

// OnGameModeInit
ConnectNPC("Dave","npc_walk");

Tuesday, November 23, 2010

SAMP Scripting Tutorial: Create a TextDraw

Here is the code

// Defines and Declarations
#define BLUE 0xFF0000FF
new Text:mytext;

// OnPlayerSpawn
TextDrawHideForPlayer(playerid,mytext); // Hide the textdraw
TextDrawDestroy(mytext); // Destroy the textdraw 

// MyTextDraw Function
MyTextDraw(playerid)
{
 mytext = TextDrawCreate(150.0,360.0,"Welcome to my SAMP Server"); // Create the textdraw
 TextDrawColor(mytext,BLUE); // Set the color of the text
 TextDrawSetOutline(mytext,1); // Set the outline (stroke) of the text
 TextDrawSetShadow(mytext,0); // Set the shadow of the text
 TextDrawFont(mytext,3); // Set the Font
 TextDrawLetterSize(mytext,0.7,1.2); // Set the letter size
 TextDrawShowForPlayer(playerid,mytext); // Show the textdraw to a certain player
}

// OnPlayerConnect
MyTextDraw(playerid);

Monday, November 22, 2010

C# Tutorial: Create a Simple MP3 Player

Here is the code

string[] files, paths;
///////////////////////////////////////
private void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        files = openFileDialog1.SafeFileNames; // Save only the names
        paths = openFileDialog1.FileNames; // Save the full paths
        for (int i = 0; i < files.Length; i++)
        {
            listBox1.Items.Add(files[i]); // Add songs to the listbox
        }
    }
}
//////////////////////////////////////////
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex]; // Play the song  
}

Or download the solution

Sunday, November 21, 2010

SAMP Scripting Tutorial: Factions | Multi-Dimensional Arrays

Here is the code

// Declarations and Defines
#define Passw 1
#define Skinid 2
#define Money 3
#define Faction 4
#define Job 5
#define AdminLevel 6
#define Level 7
///////////////////
#define Civilian 0
#define Police 1
#define Taxi 2
//-----------------//
new player[MAX_PLAYERS][8];

// OnPlayerSpawn
GivePlayerMoney(playerid,player[playerid][Money]);
SetPlayerScore(playerid,player[playerid][Level]);

// OnPlayerCommandText
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;
}

// Read/Write Player Functions
ReadPlayer(players[][],playerid,maxset)
{
 new string[256],name[MAX_PLAYER_NAME];
 new File:acc;
 GetPlayerName(playerid,name,sizeof(name));
 format(string,256,"Players/%s.ini",name);
 acc = fopen(string, io_read);
 for(new i=1; i<maxset; i++)
 {
  fread(acc,string);
  if(i==1)
  {
   SetPVarString(playerid,"passw",string);
  }
  else players[playerid][i] = strval(string);
 }
 fclose(acc);
}

WritePlayer(text[],playerid, bool:filestart)
{
 new string[256],name[MAX_PLAYER_NAME];
 new File:acc;
 GetPlayerName(playerid,name,sizeof(name));
 format(string,256,"Players/%s.ini",name);
 acc = fopen(string, io_append);
 if(filestart)
 {
  fwrite(acc,text);
 }
 else
 {
  format(string,256,"\r\n%s",text);
  fwrite(acc,string);
 }
 fclose(acc);
}

Saturday, November 20, 2010

SAMP Scripting Tutorial: Movable Objects II | Create Objects/Pickupsinside Interiors [MTA]

Here is the code

// OnGameModeInit
CreateObject(2634,266.29434204,83.02305603,1001.18328857,0.00000000,0.00000000,270.00000000); 
CreateObject(2634,266.28244019,87.49037933,1001.18328857,0.00000000,0.00000000,270.00000000); 
lspddoor = CreateObject(976,250.19511414,72.58469391,1002.64929199,0.00000000,0.00000000,180.00000000); 
armour = CreatePickup(1242,1,254.2683,77.9845,1003.6406,-1);

// OnPlayerUpdate
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);
}

// OnPlayerPickup
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);
}

// InitPos()
MoveObject(lspddoor,250.19511414,72.58469391,1002.64929199,4.0);

Thursday, November 18, 2010

SAMP Scripting Tutorial: Movable Objects I - Create Objects [MTA MapEditor]

Here is the code

// Variable Declaration
new bar;

// OnGameModeInit
//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);[/cpp]
[cpp title="InitPos()"]
public InitPos()
{
 SetObjectPos(bar,1544.6640625, -1623.9732666016, 12.989388465881);
 SetObjectRot(bar,352,270,90);
}

// OnPlayerCommandText
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;
}

Wednesday, November 17, 2010

SAMP Scripting Tutorial: Skip Class Selection | Save Player's Skin

Here is the code

// Variable Declaration
new bool:newPlayer = false;

// Spawn Function
public Spawn(playerid)
{
 SpawnPlayer(playerid);
 SetPlayerSkin(playerid, strval(ReadPlayer(2,playerid)));
}

// OnPlayerRequestClass
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);
}

// OnPlayerSpawn
if(newPlayer)
{
 new skin[4];
 valstr(skin,GetPlayerSkin(playerid));
 WritePlayer(skin,playerid,false);
}

Tuesday, November 16, 2010

SAMP Scripting Tutorial: Login/Register Script

Here is the code

// Include this file
#include 

// OnDialogResponse
switch(dialogid)
{
 case 3:
 {
  if(response==1)
  {
   switch(listitem)
   {
    case 0:
    {
     GetPlayerPos(playerid,x,y,z);
     CreateVehicle(432,x-3.0,y,z,0.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 1:
 {
  if(response==1)
  {
   if(strcmp(ReadPlayer(1,playerid),inputtext,false)!=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)
  {
   WritePlayer(inputtext,playerid,true);
   TogglePlayerSpectating(playerid,0);
  }
  else Kick(playerid);
 }
}

// OnPlayerConnect
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);
if(fexist(string))
{
 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");
}

// ReadWrite Player Functions
ReadPlayer(line,playerid)
{
 new string[256],name[MAX_PLAYER_NAME];
 new File:player;
 GetPlayerName(playerid,name,sizeof(name));
 format(string,256,"Players/%s.ini",name);
 player = fopen(string, io_read);
 for(new i=1; i<=line; i++)
 {
  fread(player,string);
  if(line==i)
  {fclose(player); return string;}
 }
 return string;
}

WritePlayer(text[],playerid, bool:filestart)
{
 new string[256],name[MAX_PLAYER_NAME];
 new File:player;
 GetPlayerName(playerid,name,sizeof(name));
 format(string,256,"Players/%s.ini",name);
 player = fopen(string, io_append);
 if(filestart)
 {
  fwrite(player,text);
 }
 else
 {
  format(string,256,"\n%s",text);
  fwrite(player,string);
 }
 fclose(player);
}

Sunday, November 14, 2010

SAMP Scripting Tutorial: Dialog Box | Menus | Spawn Vehicles

Here is the code

// OnPlayerCommandText
if(strcmp(cmdtext,"/veh",true)==0)
{
 ShowPlayerDialog(playerid,1,DIALOG_STYLE_LIST,"Vehicles","1.Rhino\n2.Firetruck\n3.Infernus\n4.Turismo\n5.Dumper","Spawn","Exit");
 return 1;
}

// OnDialogResponse
new Float:x,Float:y,Float:z;
if(dialogid==1 && response==1)
{
 switch(listitem)
 {
  case 0:
  {
   GetPlayerPos(playerid,x,y,z);
   CreateVehicle(432,x-3.0,y,z,0.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);
  }
 }
}

Saturday, November 13, 2010

SAMP Scripting Tutorial: Commands | Teleports | Load Vehicles From File

Here is the code

AddStaticVehicle(463,1902.3207,-2628.8757,13.0872,331.3780,11,11); // plane1
AddStaticVehicle(463,1873.3972,-2632.1284,13.0865,327.7260,11,11); // plane 2
AddStaticVehicle(463,1832.3038,-2623.2688,13.0865,344.0137,11,11); // plane3
AddStaticVehicle(463,1793.9832,-2626.4341,13.0861,349.1461,11,11); // plane4
AddStaticVehicle(463,1889.4199,-2579.3552,13.0865,267.0306,11,11); // plane5
AddPlayerClass(280,1885.7004,-2592.2048,13.5469,174.9667,0,0,0,0,0,0); // tele
// OnPlayerConnect
new message[256], name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
format(message,sizeof(message),"~r~Hello %s !",name);
GameTextForPlayer(playerid,message,10000,6);

// OnPlayerCommandText
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;
}

Friday, November 12, 2010

SAMP Scripting: Save positions & Create a Player Selection Script

Here is the code

// The models (skins) from which the player can choose
// Add this inside of OnGameModeInit()
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);
/------------------------------------------------------------------------/

// OnPlayerRequestClass(playerid, classid)
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);
/------------------------------------------------------------------------/

// OnPlayerConnect(playerid)
GameTextForPlayer(playerid,"~r~Hello!",10000,6);

Wednesday, November 10, 2010

C# Tutorial: Load Text from XML File [Load Settings]

Here is the code

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
XmlTextReader xreader = new XmlTextReader(openFileDialog1.FileName);
string elem = ""; int ok = 0;
while (xreader.Read())
{
    switch (xreader.NodeType)
    {
        case XmlNodeType.Element:
            {
                if (xreader.Name == "Title1")
                    elem = "Title1";
                else if (xreader.Name == "Title2")
                    elem = "Title2";
            }break;
        case XmlNodeType.Text:
            {
                if (elem == "Title1")
                {
                    if (ok == 0)
                    {  label1.Text = xreader.Value; ok=1;}
                    else listBox1.Items.Add(xreader.Value);
                }
                else if (elem == "Title2")
                {
                    if (ok == 1)
                    { label2.Text = xreader.Value; ok = 0; }
                    else listBox2.Items.Add(xreader.Value);
                }
            }break;
    }
}
xreader.Close();
}

C# Tutorial: Save Text to XML File [Save ListBox Items]

Here is the code

using System.Xml;
///////////////////////////
XmlTextWriter xwriter = new XmlTextWriter("savefile.xml", Encoding.Unicode);
xwriter.WriteStartDocument();
xwriter.WriteStartElement("XMLFILE");

xwriter.WriteStartElement("Title");
xwriter.WriteString(textBox1.Text);
xwriter.WriteEndElement();

foreach(String item in listBox1.Items)
{
    xwriter.WriteStartElement("Item");
    xwriter.WriteString(item);
    xwriter.WriteEndElement();
}

xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Close();

Sunday, November 7, 2010

C# Tutorial: Create a Simple AutoTyper/Spammer

Here is the code

using System.Threading;
////////////////////////////////////////////////
char[] keys; int i;
private void button1_Click(object sender, EventArgs e)
{
    keys = textBox1.Text.ToCharArray(); i = 0;
    // Pause the program so you have time to focus your program (Ex: Notepad)
    Thread.Sleep(800);
    timer1.Start(); 
}
        
private void timer1_Tick(object sender, EventArgs e)
{
    if ((int)keys[i] == 13)
        SendKeys.Send("{BACKSPACE}");
    else SendKeys.Send(keys[i].ToString()); i++;
    if (i >= keys.Length) timer1.Stop(); 
}

private void button2_Click(object sender, EventArgs e)
{
    timer1.Stop(); 
}

Or download the solution

Saturday, November 6, 2010

Ultimate Password Protection [C# Tutorial]

Here is the code

using Microsoft.Win32;
//////////////////////////////////////////////////

// Form1
//////////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP",true);
int fails = 0; bool passEntered = false;
private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text != PasswordEncrypter.Decrypt(key.GetValue("Pass").ToString()))
    { 
        ++fails; MessageBox.Show("Wrong Password!!! Fail: " + fails.ToString());
        if (fails > 4)
        {
            MessageBox.Show("This application is locked for 15 minutes!");
            key.SetValue("T", PasswordEncrypter.Encrypt(DateTime.Now.AddMinutes(15).ToString())); 
            Application.Exit();
        }
    }
    else { passEntered = true; this.Close(); }
}

private void button2_Click(object sender, EventArgs e)
{
    Form3 frm3 = new Form3();
    frm3.ShowDialog(); 
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!passEntered)
        Application.Exit(); 
}

// Form2
//////////////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP");
//////// Inside Form2() before InitializeComponent(); //////////////
if (key == null)
{
      Form3 frm3 = new Form3();
      frm3.ShowDialog();
}
else
{
      Form1 frm1 = new Form1(); 
      frm1.ShowDialog();
}

// Form3
/////////////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP",true);   
public Form3()
{
    InitializeComponent();
    if (key == null)
    {
        label1.Enabled = false;
        textBox1.Enabled = false;
        button1.Enabled = false;
    }
    else
        button2.Enabled = false;
}
bool passChanged = false;
private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text != key.GetValue("Pass").ToString())
    {
        MessageBox.Show("Old Password is incorrect!");
    }
    else
    {
        if (textBox2.Text == textBox3.Text && textBox2.Text != "" && textBox3.Text != "")
        { key.SetValue("Pass", PasswordEncrypter.Encrypt(textBox3.Text)); passChanged = true; this.Close(); }
        else MessageBox.Show("Password did not match!");  
    }
}

private void button2_Click(object sender, EventArgs e)
{
    if (textBox2.Text == textBox3.Text && textBox2.Text != "" && textBox3.Text != "")
    {
        key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\UPP");
        key.SetValue("Pass", PasswordEncrypter.Encrypt(textBox3.Text)); passChanged = true; this.Close(); 
    }
    else MessageBox.Show("Password did not match!");
}

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!passChanged)
        Application.Exit(); 
}

// Form4
/////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP",true);   
public Form4()
{
    InitializeComponent();
    if (DateTime.Now >= DateTime.Parse(PasswordEncrypter.Decrypt(key.GetValue("T").ToString())))
    {
        label1.Text = "Application Unlocked! Restart it!";
        key.DeleteValue("T"); 
    }
}
int c = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    Opacity -= 0.01; c++;
    if (c >= 90) Application.Exit(); 
}

// Program.cs
////////////////////////////////////////////
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\UPP");

if (key.GetValue("T") == null)
    Application.Run(new Form2());
else Application.Run(new Form4());

// PasswordEncrypter
///////////////////////////////////////////
public static string Encrypt(string password)
{
    char[] pass = password.ToCharArray(); string[] symb = { "@", "#", "$", "%", "&" }; Random rnd = new Random();
    string newpass = "";
    for (int i = pass.Length - 1; i >= 0; i--)
    {
        newpass += ((int)pass[i]).ToString() + symb[rnd.Next(0, 5)];  
    }
    return newpass;
}

public static string Decrypt(string password)
{
    string[] tokens = password.Split('@', '#', '$', '%', '&'); string pass = "";
    for (int i = tokens.Length - 2; i >= 0; i--)
    {
        pass += ((char)Convert.ToInt32(tokens[i])).ToString();  
    }
    return pass;
}

Or download the solution

Tuesday, November 2, 2010

C# Tutorial: Get Mouse Coordinates [Display them]

Here is the code

int x = 0, y = 0;
protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);
    // Create the graphics object
    Graphics g = CreateGraphics();
    // Create the pen that will draw the line
    Pen p = new Pen(Color.Navy);
    // Create the pen that will erase the line
    Pen erase = new Pen(Color.White);
    g.DrawLine(erase, 0, 0, x, y);
    // Save the mouse coordinates
    x = e.X; y = e.Y;
    g.DrawLine(p, 0, 0, x, y);
    label1.Location = new Point(x - label1.Width, y); // Place the label on the mouse left 
    label2.Location = new Point(x, y - label2.Height); // Place the label on the mouse right
    label1.Text = x.ToString(); label2.Text = y.ToString();   
}

Or download the solution