Advertisement
Guest User

Caminhoneiro / Truck Driver - Script

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