Advertisement
Guest User

Caminhoneiro / Truck Driver - Script

a guest
Jan 30th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.90 KB | None | 0 0
  1. /*
  2. *
  3. *   Caminhoneiro / Truck Driver Script
  4. *   Lucas Gomes ( Dry )   Version: 1.0
  5. *
  6. */
  7.  
  8. /*
  9.     Commands:
  10.    
  11.         CMD:newtruck (RCON)                                     » Usado para iniciar o processo de CRIAÇÃO de caminhões no jogo;
  12.         CMD:deltruck (RCON)                                     » Usado para iniciar o processo de REMOÇÃO de caminhões no jogo;
  13.         CMD:setmaincp(RCON)                                     » Usado para criar o checkpoint principal;
  14.         CMD:newcp(RCON)                                         » Usado para criar um checkpoint de entregas;
  15. */
  16.  
  17. /*
  18.     Developer Functions:
  19.  
  20. -   addTruck(Float: X, Float: Y, Float: Z);     » Adiciona um novo caminhão de entregas ao servidor.
  21. -   removeTruck(TruckID);                       » Remove um caminhão de entrega do servidor.
  22. -   loadTrucks();                               » Fazer a atualização dos caminhões.
  23. -   unLoadTrucks();                             » Usado para retirar os caminhões do servidor(unload);
  24. -   startCreation(playerid);                    » Inicia o processo de criação de caminhões no jogo;
  25. -   checkTruckInfo(playerid);                   » Função final para o processo de criação de caminhões;
  26. -   deleteTruck(playerid);                      » Remover o caminhão do jogo ( permanentemente );
  27. -   setMPoint(playerid);                        » Cria o checkpoint principal;
  28. -   LoadMainPoint();                            » Carrega as vars do checkpoint principal;
  29. -   StartJobForPlayer(playerid);                » Inicia o processo de entregas para o jogador;
  30. -   SetNewCheckpoint(playerid);                 » Sega um novo checkpoint para fazer entregas;
  31. -   SetPlayerLocal(playerid);                   » Escolhe de forma randomica um checkpoint para o jogador fazer as entregas;
  32.    
  33. */
  34.  
  35. #include <a_samp>
  36. #include <zcmd>
  37. #include <dof2>
  38.  
  39. #define cPadrao 0xFF8080FF
  40. #define cAzul   0x0080C0FF
  41. #define cBranco -1
  42.  
  43. #define MAX_PAY     10000
  44. #define MAX_TRUCKS  50  // default: 50 (recomendado);
  45. #define MAX_CP      50  // Max Checkpoints;
  46. #define Truck_ID    515 // ID do veículo: caminhão;
  47. #define Trailer_ID  435 // ID da carga;
  48.  
  49. #define PRESSED(%0) \
  50.     (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  51.  
  52. #define truckConfig     "TruckDriver/%d.ini"
  53. #define main_point      "TruckDriver/main_point.ini"
  54. #define local_cp        "TruckDriver/CP/%d.ini"
  55.  
  56. enum tInfo
  57. {
  58.     bool:created,
  59.     id,
  60.     Float:posX,
  61.     Float:posY,
  62.     Float:posZ,
  63.     Float:posA,
  64. };
  65.  
  66. enum i_player
  67. {
  68.     bool:working,
  69.     bool:building,
  70.     veh,
  71.     step,
  72.     Float: pX,
  73.     Float: pY,
  74.     Float: pZ,
  75.     trailer
  76. };
  77.  
  78. enum m_point
  79. {
  80.     Float: cX,
  81.     Float: cY,
  82.     Float: cZ,
  83.     bool:created
  84. };
  85.  
  86. new mPoint[m_point];
  87. new pInfo[MAX_PLAYERS][i_player];
  88. new TruckInfo[MAX_TRUCKS][tInfo];
  89.  
  90. addTruck(Float: X, Float: Y, Float: Z, Float: A)
  91. {
  92.     for ( new i = 0; i < MAX_TRUCKS; i++)
  93.     {
  94.         if ( i > MAX_TRUCKS )
  95.         {
  96.             printf("Limite de caminhôes atingido. (%d)", MAX_TRUCKS);
  97.             break;
  98.         }
  99.         new Str[500];
  100.         format ( Str, sizeof Str, truckConfig, i);
  101.         if ( !DOF2::FileExists(Str))
  102.         {
  103.             DOF2::CreateFile(Str);
  104.             if (!DOF2::FileExists(Str))
  105.             {
  106.                 print("Erro ao adicionar um novo caminhão.");
  107.                 print("Certifique-se de criar as pastas necessárias.");
  108.                 return 1;
  109.             }
  110.             DOF2::SetFloat(Str, "PosX", X);
  111.             DOF2::SetFloat(Str, "PosY", Y);
  112.             DOF2::SetFloat(Str, "PosZ", Z);
  113.             DOF2::SetFloat(Str, "Angle", A);
  114.             DOF2::SaveFile();
  115.             LoadTrucks();
  116.             printf("Caminhão %d adicionado", i);
  117.             break;
  118.         }
  119.     }
  120.     return 1;
  121. }
  122.  
  123. LoadTrucks()
  124. {
  125.     new Str[500];
  126.     for ( new i = 0; i < MAX_TRUCKS; i++)
  127.     {
  128.         format ( Str, sizeof Str, truckConfig, i);
  129.         if ( TruckInfo[i][created] == false && DOF2::FileExists(Str))
  130.         {
  131.             TruckInfo[i][created] = true;
  132.             TruckInfo[i][posX] = DOF2::GetFloat(Str, "PosX");
  133.             TruckInfo[i][posY] = DOF2::GetFloat(Str, "PosY");
  134.             TruckInfo[i][posZ] = DOF2::GetFloat(Str, "PosZ");
  135.             TruckInfo[i][posA] = DOF2::GetFloat(Str, "Angle");
  136.             TruckInfo[i][id] = CreateVehicle(Truck_ID, TruckInfo[i][posX],TruckInfo[i][posY],TruckInfo[i][posZ],TruckInfo[i][posA], 0, 0, -1);
  137.             printf("Caminhão %d carregado!", i);
  138.         }
  139.     }
  140.     return 1;
  141. }
  142.  
  143. unLoadTrucks()
  144. {
  145.     for ( new i = 0; i < MAX_TRUCKS; i++)
  146.     {
  147.         if ( TruckInfo[i][created] == true )
  148.         {
  149.             TruckInfo[i][created] = false;
  150.             DestroyVehicle(TruckInfo[i][id]);
  151.             printf("Encerrando script (%d)",i);
  152.         }
  153.     }
  154.     for ( new i = 0; i < GetMaxPlayers(); i++)
  155.     {
  156.         DestroyVehicle(pInfo[i][trailer]);
  157.     }
  158.     return 1;
  159. }
  160.  
  161. startCreation(playerid)
  162. {
  163.     if ( pInfo[playerid][building] == true ) SendClientMessage(playerid, cPadrao, "Você já está criando um caminhão!");
  164.     if ( IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, cPadrao, "Saia do seu veículo atual!");
  165.     new Float: X, Float: Y, Float: Z, Float: A;
  166.     GetPlayerPos(playerid, X, Y, Z);
  167.     GetPlayerFacingAngle(playerid, A);
  168.     pInfo[playerid][veh] = CreateVehicle(Truck_ID, X, Y, Z, A, 0, 0, -1);
  169.     PutPlayerInVehicle(playerid, pInfo[playerid][veh], 0);
  170.     pInfo[playerid][building] = true;
  171.     SendClientMessage(playerid, cPadrao, "Vamos criar um novo caminhão?");
  172.     SendClientMessage(playerid, cPadrao, "Você foi setado para dentro de um deles!");
  173.     SendClientMessage(playerid, cPadrao, "Quando estiver pronto para cria-lo pressione: 'Y'");
  174.     return 1;
  175. }
  176.  
  177. checkTruckInfo(playerid)
  178. {
  179.     if ( IsPlayerInAnyVehicle(playerid) && pInfo[playerid][building] == true )
  180.     {
  181.         new Float: X, Float: Y, Float: Z, Float: A, v;
  182.         v = GetPlayerVehicleID(playerid);
  183.         GetVehiclePos(v, X, Y, Z);
  184.         GetVehicleZAngle(v, A);
  185.         DestroyVehicle(GetPlayerVehicleID(playerid));
  186.         addTruck(X, Y, Z, A);
  187.         GetPlayerPos(playerid, X, Y, Z);
  188.         SetPlayerPos(playerid, X, Y, Z+3);
  189.         pInfo[playerid][building] = false;
  190.         SendClientMessage(playerid, cPadrao, "Veículo adicionado!");
  191.         return 1;
  192.     }
  193.     else
  194.     {
  195.         SendClientMessage(playerid, cPadrao, "Algo de errado aconteceu!");
  196.         SendClientMessage(playerid, cPadrao, "Erro: 1A");
  197.         return 1;
  198.     }
  199. }
  200.  
  201. deleteTruck(playerid)
  202. {
  203.     if ( !IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, cPadrao, "Entre em um veículo!");
  204.     for ( new i = 0; i < MAX_TRUCKS; i++)
  205.     {
  206.         if ( IsPlayerInVehicle(playerid, TruckInfo[i][id]))
  207.         {
  208.             new Str[500];
  209.             format ( Str, sizeof Str, truckConfig, i);
  210.             if(!DOF2::FileExists(Str)) return SendClientMessage(playerid, cPadrao, "Caminhão não encontrado!");
  211.             TruckInfo[i][created] = false;
  212.             DestroyVehicle(TruckInfo[i][id]);
  213.             DOF2::RemoveFile(Str);
  214.             if(!DOF2::FileExists(Str)) SendClientMessage(playerid, cPadrao, "Caminhão removido com sucesso!");
  215.             break;
  216.         }
  217.     }
  218.     return 1;
  219. }
  220.  
  221. setMPoint(playerid)
  222. {
  223.     new Str[500], Float: X, Float: Y, Float: Z;
  224.     GetPlayerPos(playerid, X, Y, Z);
  225.     format ( Str, sizeof Str, main_point);
  226.     if (!DOF2::FileExists(Str))
  227.     {
  228.         DOF2::CreateFile(Str);
  229.         if ( !DOF2::FileExists(Str))
  230.         {
  231.             print("Erro ao criar CheckPoint principal!");
  232.             SendClientMessage(playerid, cPadrao, "Erro ao criar CheckPoint principal");
  233.             return 1;
  234.         }
  235.     }
  236.     if ( DOF2::FileExists(Str))
  237.     {
  238.         mPoint[created] = true;
  239.         DOF2::SetFloat(Str, "cX", X);
  240.         DOF2::SetFloat(Str, "cY", Y);
  241.         DOF2::SetFloat(Str, "cZ", Z);
  242.         DOF2::SaveFile();
  243.         SendClientMessage(playerid, cPadrao, "Check Point principal criado!");
  244.         LoadMainPoint();
  245.         return 1;
  246.     }
  247.     return 1;
  248. }
  249.  
  250. LoadMainPoint()
  251. {
  252.     new Str[500];
  253.     format ( Str, sizeof Str, main_point);
  254.     if ( !DOF2::FileExists(Str))
  255.     {
  256.         print("Crie o check point principal!");
  257.         mPoint[created] = false;
  258.         return 1;
  259.     }
  260.     mPoint[cX] = DOF2::GetFloat(Str, "cX");
  261.     mPoint[cY] = DOF2::GetFloat(Str, "cY");
  262.     mPoint[cZ] = DOF2::GetFloat(Str, "cZ");
  263.     mPoint[created] = true;
  264.     print("Ponto principal carregado!");
  265.     return 1;
  266. }
  267.  
  268. StartJobForPlayer(playerid)
  269. {
  270.     if ( pInfo[playerid][building] == true ) return SendClientMessage(playerid, cPadrao, "Ops! Você está criando um veículo!");
  271.     if ( pInfo[playerid][working] == true ) return SendClientMessage(playerid, cPadrao, "Você já está fazendo uma entrega!");
  272.     if ( mPoint[created] == false) return SendClientMessage(playerid, cPadrao, "Ponto de carga não definido!");
  273.     if ( pInfo[playerid][step] == 0)
  274.     {
  275.         SetPlayerCheckpoint(playerid, mPoint[cX], mPoint[cY], mPoint[cZ], 5.0);
  276.         SendClientMessage(playerid, cPadrao, "Siga o checkpoint para iniciar a entrega!");
  277.         pInfo[playerid][step]++;
  278.         pInfo[playerid][working] = true;
  279.         return 1;
  280.     }
  281.     return 1;
  282. }
  283.  
  284. SetNewCheckpoint(playerid)
  285. {
  286.     new Str[500];
  287.     for ( new i = 0; i < MAX_PLAYERS; i++)
  288.     {
  289.         format ( Str, sizeof Str, local_cp, i);
  290.         if ( !DOF2::FileExists(Str))
  291.         {
  292.             DOF2::CreateFile(Str);
  293.             if ( !DOF2::FileExists(Str))
  294.             {
  295.                 printf("Checkpoint %d não criado.", i);
  296.                 SendClientMessage(playerid, cPadrao, "Erro ao criar novo checkpoint!");
  297.                 break;
  298.             }
  299.             new Float: X, Float: Y, Float: Z;
  300.             GetPlayerPos(playerid, X, Y, Z);
  301.             DOF2::SetFloat(Str, "pX", X);
  302.             DOF2::SetFloat(Str, "pY", Y);
  303.             DOF2::SetFloat(Str, "pZ", Z);
  304.             DOF2::SaveFile();
  305.             printf("Checkpoint %d criado!", i);
  306.             SendClientMessage(playerid, cPadrao, "Checkpoint criado!");
  307.             break;
  308.         }
  309.     }
  310.     return 1;
  311. }
  312.  
  313. SetPlayerLocal(playerid)
  314. {
  315.     new xmax;
  316.     new Str[500];
  317.     for ( new i = 0; i < MAX_CP; i++)
  318.     {
  319.         format ( Str, sizeof Str, local_cp, i );
  320.         if ( DOF2::FileExists(Str))
  321.         {
  322.             xmax++;
  323.         }
  324.     }
  325.     if ( xmax == 0)
  326.     {
  327.         SendClientMessage(playerid, cPadrao, "Nenhum ponto de entrega definido!");
  328.         DisablePlayerCheckpoint(playerid);
  329.         return 1;
  330.     }
  331.     new target = random(xmax);
  332.     format ( Str, sizeof Str, local_cp, target);
  333.     new Float: X, Float: Y, Float: Z;
  334.     X = DOF2::GetFloat(Str, "pX");
  335.     Y = DOF2::GetFloat(Str, "pY");
  336.     Z = DOF2::GetFloat(Str, "pZ");
  337.     SetPlayerCheckpoint(playerid, X, Y, Z, 5.0);
  338.     GetPlayerPos(playerid, X, Y, Z);
  339.     pInfo[playerid][trailer] = CreateVehicle(Trailer_ID, X, Y, Z-10, 0, 1, 1, -1);
  340.     SetTimerEx("attach", 1000, false, "i", playerid);
  341.     pInfo[playerid][step]++;
  342.     return 1;
  343. }
  344.  
  345. forward attach(playerid);
  346. public attach(playerid)
  347. {
  348.     AttachTrailerToVehicle(pInfo[playerid][trailer], GetPlayerVehicleID(playerid));
  349.     if ( IsTrailerAttachedToVehicle(GetPlayerVehicleID(playerid)))
  350.     {
  351.         pInfo[playerid][step] = 2;
  352.         SendClientMessage(playerid, cPadrao, "Siga o checkpoint para entregar a carga!");
  353.         SendClientMessage(playerid, cPadrao, "Se sair do caminhão a entrega é cancelada!");
  354.         SendClientMessage(playerid, cPadrao, "Boa sorte!");
  355.     }
  356.     return 1;
  357. }
  358.  
  359. CMD:newtruck(playerid)
  360. {
  361.     if ( IsPlayerAdmin(playerid)) startCreation(playerid);
  362.     return 1;
  363. }
  364.  
  365. CMD:deltruck(playerid)
  366. {
  367.     if ( !IsPlayerAdmin(playerid)) return SendClientMessage(playerid, cPadrao, "Error.");
  368.     deleteTruck(playerid);
  369.     return 1;
  370. }
  371.  
  372. CMD:setmaincp(playerid)
  373. {
  374.     if (!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, cPadrao, "Você é admin?");
  375.     setMPoint(playerid);
  376.     return 1;
  377. }
  378.  
  379. CMD:newcp(playerid)
  380. {
  381.     if ( IsPlayerAdmin(playerid)) SetNewCheckpoint(playerid);
  382.     return 1;
  383. }
  384.  
  385. public OnFilterScriptInit()
  386. {
  387.     LoadMainPoint();
  388.     LoadTrucks();
  389.     return 1;
  390. }
  391.  
  392. public OnFilterScriptExit()
  393. {
  394.     unLoadTrucks();
  395.     return 1;
  396. }
  397.  
  398. public OnPlayerConnect(playerid)
  399. {
  400.     pInfo[playerid][building] = false;
  401.     return 1;
  402. }
  403.  
  404. public OnPlayerDisconnect(playerid, reason)
  405. {
  406.     if ( IsPlayerAdmin(playerid) && pInfo[playerid][building] == true  && pInfo[playerid][step] == 1)
  407.     {
  408.         pInfo[playerid][building] = false;
  409.         DestroyVehicle(pInfo[playerid][veh]);
  410.         DestroyVehicle(pInfo[playerid][trailer]);
  411.     }
  412.     if ( pInfo[playerid][working] == true ) pInfo[playerid][working] = false;
  413.     return 1;
  414. }
  415.  
  416. public OnPlayerStateChange(playerid, newstate, oldstate)
  417. {
  418.     if(newstate == PLAYER_STATE_DRIVER && pInfo[playerid][building] == false && pInfo[playerid][working] == false)
  419.     {
  420.         for ( new i = 0; i < MAX_TRUCKS; i++)
  421.         {
  422.             if ( IsPlayerInVehicle(playerid, TruckInfo[i][id]))
  423.             {
  424.                 SendClientMessage(playerid, cPadrao, "Bem vindo caminhoneiro!");
  425.                 SendClientMessage(playerid, cPadrao, "Pressione: 'Y' para iniciar uma entrega!");
  426.                 break;
  427.             }
  428.         }
  429.     }
  430.     if(oldstate == PLAYER_STATE_DRIVER && pInfo[playerid][building] == true)
  431.     {
  432.         pInfo[playerid][building] = false;
  433.         DestroyVehicle(pInfo[playerid][veh]);
  434.         SendClientMessage(playerid, cPadrao, "Você saiu do veículo, e o processo foi cancelado.");
  435.     }
  436.     if(oldstate == PLAYER_STATE_DRIVER && pInfo[playerid][working] == true)
  437.     {
  438.         pInfo[playerid][working] = false;
  439.         pInfo[playerid][step] = 0;
  440.         DisablePlayerCheckpoint(playerid);
  441.         DestroyVehicle(pInfo[playerid][trailer]);
  442.         SendClientMessage(playerid, cPadrao, "Você saiu do caminhão no meio de uma entrega!");
  443.         SendClientMessage(playerid, cPadrao, "Ganhos: $0");
  444.     }
  445.     return 1;
  446. }
  447.  
  448. public OnPlayerEnterCheckpoint(playerid)
  449. {
  450.     if ( pInfo[playerid][step] == 1 && pInfo[playerid][working] == true)
  451.     {
  452.         SetPlayerLocal(playerid);
  453.         return 1;
  454.     }
  455.     if ( pInfo[playerid][step] == 2 && pInfo[playerid][working] == true)
  456.     {
  457.         if ( IsTrailerAttachedToVehicle(GetPlayerVehicleID(playerid)))
  458.         {
  459.             new Money = random(MAX_PAY);
  460.             if ( Money < Money / 10)
  461.                 Money = MAX_PAY / 10;
  462.             GivePlayerMoney(playerid, Money);
  463.             DisablePlayerCheckpoint(playerid);
  464.             DestroyVehicle(pInfo[playerid][trailer]);
  465.             pInfo[playerid][working] = false;
  466.             pInfo[playerid][step] = 0;
  467.             new Str[128];
  468.             format ( Str, sizeof Str, "Você recebeu: {FFFFFF}$%d", Money);
  469.             SendClientMessage(playerid, cPadrao, Str);
  470.             return 1;
  471.         }
  472.         return 1;
  473.     }
  474.     return 1;
  475. }
  476.  
  477. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  478. {
  479.     if (PRESSED(KEY_YES))
  480.     {
  481.         if ( IsPlayerAdmin(playerid) && pInfo[playerid][building] == true) return checkTruckInfo(playerid);
  482.         if ( pInfo[playerid][working] == false && pInfo[playerid][building] == false )
  483.         {
  484.             for ( new i = 0; i < MAX_TRUCKS; i++)
  485.             {
  486.                 if ( IsPlayerInVehicle(playerid, TruckInfo[i][id])) StartJobForPlayer(playerid);
  487.                 break;
  488.             }
  489.         }
  490.     }
  491.     return 1;
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement