SHARE
TWEET

Fuel System

a guest Jan 31st, 2016 1,525 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define     FILTERSCRIPT
  2. #include    <a_samp>        // SA-MP Team
  3. #include    <evf>           // http://forum.sa-mp.com/showthread.php?t=486060 - Emmet_
  4. #include    <evi>           // http://forum.sa-mp.com/showthread.php?t=438678 - Vince
  5. #include    <progress2>     // http://forum.sa-mp.com/showthread.php?t=537468 - [HLF]Southclaw
  6. #include    <streamer>      // http://forum.sa-mp.com/showthread.php?t=102865 - Incognito
  7. #include    <zcmd>          // http://forum.sa-mp.com/showthread.php?t=91354  - Zeex
  8.  
  9. #define     MAX_GAS_PUMPS       (78)
  10. #define     UPDATE_RATE         (1000)      // Fuel consume rate in milliseconds. (Default: 1000)
  11. #define     LITRE_PRICE         (3)         // Pretty obvious I guess. (Default: 3)
  12.  
  13. enum    e_pump
  14. {
  15.     Float: pumpX,
  16.     Float: pumpY,
  17.     Float: pumpZ,
  18.     pumpUser,
  19.     Text3D: pumpLabel
  20. }
  21.  
  22. new
  23.     Float: PumpData[MAX_GAS_PUMPS][e_pump] = {
  24.         {-85.2422, -1165.0312, 2.6328},
  25.         {-90.1406, -1176.6250, 2.6328},
  26.         {-92.1016, -1161.7891, 2.9609},
  27.         {-97.0703, -1173.7500, 3.0312},
  28.         {1941.6562, -1767.2891, 14.1406},
  29.         {1941.6562, -1771.3438, 14.1406},
  30.         {1941.6562, -1774.3125, 14.1406},
  31.         {1941.6562, -1778.4531, 14.1406},
  32.         {-1327.0312, 2685.5938, 49.4531},
  33.         {-1327.7969, 2680.1250, 49.4531},
  34.         {-1328.5859, 2674.7109, 49.4531},
  35.         {-1329.2031, 2669.2812, 49.4531},
  36.         {-1464.9375, 1860.5625, 31.8203},
  37.         {-1465.4766, 1868.2734, 31.8203},
  38.         {-1477.6562, 1859.7344, 31.8203},
  39.         {-1477.8516, 1867.3125, 31.8203},
  40.         {-1600.6719, -2707.8047, 47.9297},
  41.         {-1603.9922, -2712.2031, 47.9297},
  42.         {-1607.3047, -2716.6016, 47.9297},
  43.         {-1610.6172, -2721.0000, 47.9297},
  44.         {-1665.5234, 416.9141, 6.3828},
  45.         {-1669.9062, 412.5312, 6.3828},
  46.         {-1672.1328, 423.5000, 6.3828},
  47.         {-1675.2188, 407.1953, 6.3828},
  48.         {-1676.5156, 419.1172, 6.3828},
  49.         {-1679.3594, 403.0547, 6.3828},
  50.         {-1681.8281, 413.7812, 6.3828},
  51.         {-1685.9688, 409.6406, 6.3828},
  52.         {-2241.7188, -2562.2891, 31.0625},
  53.         {-2246.7031, -2559.7109, 31.0625},
  54.         {-2410.8047, 970.8516, 44.4844},
  55.         {-2410.8047, 976.1875, 44.4844},
  56.         {-2410.8047, 981.5234, 44.4844},
  57.         {1378.9609, 461.0391, 19.3281},
  58.         {1380.6328, 460.2734, 19.3281},
  59.         {1383.3984, 459.0703, 19.3281},
  60.         {1385.0781, 458.2969, 19.3281},
  61.         {603.4844, 1707.2344, 6.1797},
  62.         {606.8984, 1702.2188, 6.1797},
  63.         {610.2500, 1697.2656, 6.1797},
  64.         {613.7188, 1692.2656, 6.1797},
  65.         {617.1250, 1687.4531, 6.1797},
  66.         {620.5312, 1682.4609, 6.1797},
  67.         {624.0469, 1677.6016, 6.1797},
  68.         {655.6641, -558.9297, 15.3594},
  69.         {655.6641, -560.5469, 15.3594},
  70.         {655.6641, -569.6016, 15.3594},
  71.         {655.6641, -571.2109, 15.3594},
  72.         {1590.3516, 2193.7109, 11.3125},
  73.         {1590.3516, 2204.5000, 11.3125},
  74.         {1596.1328, 2193.7109, 11.3125},
  75.         {1596.1328, 2204.5000, 11.3125},
  76.         {1602.0000, 2193.7109, 11.3125},
  77.         {1602.0000, 2204.5000, 11.3125},
  78.         {2109.0469, 914.7188, 11.2578},
  79.         {2109.0469, 925.5078, 11.2578},
  80.         {2114.9062, 914.7188, 11.2578},
  81.         {2114.9062, 925.5078, 11.2578},
  82.         {2120.8203, 914.7188, 11.2578},
  83.         {2120.8203, 925.5078, 11.2578},
  84.         {2141.6719, 2742.5234, 11.2734},
  85.         {2141.6719, 2753.3203, 11.2734},
  86.         {2147.5312, 2742.5234, 11.2734},
  87.         {2147.5312, 2753.3203, 11.2734},
  88.         {2153.3125, 2742.5234, 11.2734},
  89.         {2153.3125, 2753.3203, 11.2734},
  90.         {2196.8984, 2470.2500, 11.3125},
  91.         {2196.8984, 2474.6875, 11.3125},
  92.         {2196.8984, 2480.3281, 11.3125},
  93.         {2207.6953, 2470.2500, 11.3125},
  94.         {2207.6953, 2474.6875, 11.3125},
  95.         {2207.6953, 2480.3281, 11.3125},
  96.         {2634.6406, 1100.9453, 11.2500},
  97.         {2634.6406, 1111.7500, 11.2500},
  98.         {2639.8750, 1100.9609, 11.2500},
  99.         {2639.8750, 1111.7500, 11.2500},
  100.         {2645.2500, 1100.9609, 11.2500},
  101.         {2645.2500, 1111.7500, 11.2500}
  102.     };
  103.  
  104. new
  105.     UsingPumpID[MAX_PLAYERS] = {-1, ...},
  106.     RefuelTimer[MAX_PLAYERS] = {-1, ...},
  107.     Float: FuelBought[MAX_PLAYERS],
  108.     PlayerText: FuelText[MAX_PLAYERS],
  109.     PlayerBar: FuelBar[MAX_PLAYERS] = {INVALID_PLAYER_BAR_ID, ...};
  110.  
  111. new
  112.     Float: Fuel[MAX_VEHICLES] = {100.0, ...},
  113.     Float: VehicleLastCoords[MAX_VEHICLES][3];
  114.  
  115. Pump_Update(id)
  116. {
  117.     new string[96];
  118.     format(string, sizeof(string), "Gas Pump\n\n{2ECC71}$%d / Litre\n%s/refuel", LITRE_PRICE, (IsPlayerConnected(PumpData[id][pumpUser])) ? ("{E74C3C}") : ("{FFFFFF}"));
  119.     return UpdateDynamic3DTextLabelText(PumpData[id][pumpLabel], 0xF1C40FFF, string);
  120. }
  121.  
  122. Pump_Closest(playerid, Float: range = 6.0)
  123. {
  124.     new id = -1, Float: dist = range, Float: tempdist;
  125.     for(new i; i < MAX_GAS_PUMPS; i++)
  126.     {
  127.         tempdist = GetPlayerDistanceFromPoint(playerid, PumpData[i][pumpX], PumpData[i][pumpY], PumpData[i][pumpZ]);
  128.  
  129.         if(tempdist > range) continue;
  130.         if(tempdist <= dist)
  131.         {
  132.             dist = tempdist;
  133.             id = i;
  134.         }
  135.     }
  136.  
  137.     return id;
  138. }
  139.  
  140. Fuel_InitPlayer(playerid)
  141. {
  142.     UsingPumpID[playerid] = -1;
  143.     RefuelTimer[playerid] = -1;
  144.     FuelBought[playerid] = 0.0;
  145.  
  146.     FuelText[playerid] = CreatePlayerTextDraw(playerid, 40.000000, 305.000000, "~b~~h~Refueling...~n~~n~~w~Price: ~g~~h~$0 ~y~~h~(0.00L)");
  147.     PlayerTextDrawBackgroundColor(playerid, FuelText[playerid], 255);
  148.     PlayerTextDrawFont(playerid, FuelText[playerid], 1);
  149.     PlayerTextDrawLetterSize(playerid, FuelText[playerid], 0.240000, 1.100000);
  150.     PlayerTextDrawColor(playerid, FuelText[playerid], -1);
  151.     PlayerTextDrawSetOutline(playerid, FuelText[playerid], 1);
  152.     PlayerTextDrawSetProportional(playerid, FuelText[playerid], 1);
  153.     PlayerTextDrawSetSelectable(playerid, FuelText[playerid], 0);
  154.  
  155.     FuelBar[playerid] = CreatePlayerProgressBar(playerid, 498.0, 104.0, 113.0, 6.2, 0xF1C40FFF, 100.0, 0);
  156.     return 1;
  157. }
  158.  
  159. Fuel_ResetPlayer(playerid)
  160. {
  161.     if(UsingPumpID[playerid] != -1)
  162.     {
  163.         PumpData[ UsingPumpID[playerid] ][pumpUser] = INVALID_PLAYER_ID;
  164.         Pump_Update(UsingPumpID[playerid]);
  165.     }
  166.  
  167.     if(RefuelTimer[playerid] != -1)
  168.     {
  169.         KillTimer(RefuelTimer[playerid]);
  170.         RefuelTimer[playerid] = -1;
  171.  
  172.         PlayerTextDrawHide(playerid, FuelText[playerid]);
  173.     }
  174.  
  175.     UsingPumpID[playerid] = -1;
  176.     FuelBought[playerid] = 0.0;
  177.     return 1;
  178. }
  179.  
  180. Vehicle_StartEngine(vehicleid)
  181. {
  182.     if(Fuel[vehicleid] < 0.1) return 0;
  183.     SetVehicleParams(vehicleid, VEHICLE_TYPE_ENGINE, 1);
  184.     return 1;
  185. }
  186.  
  187. Vehicle_IsANoFuelVehicle(model)
  188. {
  189.     switch(model)
  190.     {
  191.         case 481, 509, 510: return 1;
  192.         default: return 0;
  193.     }
  194.  
  195.     return 0;
  196. }
  197.  
  198. Float: Vehicle_GetSpeed(vehicleid)
  199. {
  200.     new Float: vx, Float: vy, Float: vz, Float: vel;
  201.     vel = GetVehicleVelocity(vehicleid, vx, vy, vz);
  202.     vel = (floatsqroot(((vx*vx)+(vy*vy))+(vz*vz)) * 181.5);
  203.     return vel;
  204. }
  205.  
  206. public OnFilterScriptInit()
  207. {
  208.     ManualVehicleEngineAndLights();
  209.  
  210.     for(new i; i < MAX_GAS_PUMPS; i++)
  211.     {
  212.         PumpData[i][pumpUser] = INVALID_PLAYER_ID;
  213.         PumpData[i][pumpLabel] = CreateDynamic3DTextLabel("Gas Pump", 0xF1C40FFF, PumpData[i][pumpX], PumpData[i][pumpY], PumpData[i][pumpZ] + 0.75, 7.5);
  214.         Pump_Update(i);
  215.     }
  216.  
  217.     for(new i, p = GetPlayerPoolSize(); i <= p; i++)
  218.     {
  219.         if(!IsPlayerConnected(i)) continue;
  220.         Fuel_InitPlayer(i);
  221.     }
  222.  
  223.     for(new i, v = GetVehiclePoolSize(); i <= v; i++)
  224.     {
  225.         if(!IsValidVehicle(i)) continue;
  226.         if(Vehicle_IsANoFuelVehicle( GetVehicleModel(i) )) SetVehicleParams(i, VEHICLE_TYPE_ENGINE, 1);
  227.     }
  228.  
  229.     SetTimer("ConsumeFuel", UPDATE_RATE, true);
  230.     return 1;
  231. }
  232.  
  233. public OnFilterScriptExit()
  234. {
  235.     for(new i, p = GetPlayerPoolSize(); i <= p; i++)
  236.     {
  237.         if(!IsPlayerConnected(i)) continue;
  238.         Fuel_ResetPlayer(i);
  239.         HidePlayerProgressBar(i, FuelBar[i]);
  240.     }
  241.  
  242.     return 1;
  243. }
  244.  
  245. public OnPlayerConnect(playerid)
  246. {
  247.     Fuel_InitPlayer(playerid);
  248.     return 1;
  249. }
  250.  
  251. public OnPlayerDisconnect(playerid, reason)
  252. {
  253.     Fuel_ResetPlayer(playerid);
  254.     return 1;
  255. }
  256.  
  257. public OnVehicleSpawn(vehicleid)
  258. {
  259.     Fuel[vehicleid] = 100.0;
  260.     return 1;
  261. }
  262.  
  263. public OnPlayerStateChange(playerid, newstate, oldstate)
  264. {
  265.     if(newstate == PLAYER_STATE_DRIVER)
  266.     {
  267.         new id = GetPlayerVehicleID(playerid);
  268.         if(Vehicle_IsANoFuelVehicle( GetVehicleModel(id) )) return SetVehicleParams(id, VEHICLE_TYPE_ENGINE, 1);
  269.         SendClientMessage(playerid, -1, "You can start/stop the engine by pressing N.");
  270.         ShowPlayerProgressBar(playerid, FuelBar[playerid]);
  271.         SetPlayerProgressBarValue(playerid, FuelBar[playerid], Fuel[id]);
  272.         GetVehiclePos(GetPlayerVehicleID(playerid), VehicleLastCoords[id][0], VehicleLastCoords[id][1], VehicleLastCoords[id][2]);
  273.     }
  274.  
  275.     if(oldstate == PLAYER_STATE_DRIVER)
  276.     {
  277.         Fuel_ResetPlayer(playerid);
  278.         HidePlayerProgressBar(playerid, FuelBar[playerid]);
  279.     }
  280.  
  281.     return 1;
  282. }
  283.  
  284. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  285. {
  286.     if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER && (newkeys & KEY_NO))
  287.     {
  288.         new id = GetPlayerVehicleID(playerid);
  289.         if(Vehicle_IsANoFuelVehicle( GetVehicleModel(id) )) return 1;
  290.  
  291.         if(GetVehicleParams(id, VEHICLE_TYPE_ENGINE)) {
  292.             SetVehicleParams(id, VEHICLE_TYPE_ENGINE, 0);
  293.         }else{
  294.             Vehicle_StartEngine(id);
  295.  
  296.             if(UsingPumpID[playerid] != -1)
  297.             {
  298.                 new Float: x, Float: y, Float: z;
  299.                 GetPlayerPos(playerid, x, y, z);
  300.                 CreateExplosionForPlayer(playerid, x, y, z, 6, 8.0);
  301.             }
  302.         }
  303.     }
  304.  
  305.     return 1;
  306. }
  307.  
  308. public OnPlayerUpdate(playerid)
  309. {
  310.     if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
  311.     {
  312.         new vid = GetPlayerVehicleID(playerid);
  313.         if(GetPVarInt(playerid, "FuelLastUpdate") < GetTickCount() && UsingPumpID[playerid] == -1 && !Vehicle_IsANoFuelVehicle( GetVehicleModel(vid) ))
  314.         {
  315.             SetPlayerProgressBarValue(playerid, FuelBar[playerid], Fuel[vid]);
  316.             SetPVarInt(playerid, "FuelLastUpdate", GetTickCount() + (UPDATE_RATE + 50));
  317.         }
  318.     }
  319.  
  320.     return 1;
  321. }
  322.  
  323. forward ConsumeFuel();
  324. public ConsumeFuel()
  325. {
  326.     new Float: mass, Float: speed, Float: dist;
  327.     for(new i = 1, ps = GetVehiclePoolSize(); i <= ps; i++)
  328.     {
  329.         if(!IsValidVehicle(i)) continue;
  330.         if(Vehicle_IsANoFuelVehicle( GetVehicleModel(i) )) continue;
  331.         if(!GetVehicleParams(i, VEHICLE_TYPE_ENGINE)) continue;
  332.         dist = GetVehicleDistanceFromPoint(i, VehicleLastCoords[i][0], VehicleLastCoords[i][1], VehicleLastCoords[i][2]);
  333.         mass = GetVehicleModelInfoAsFloat(GetVehicleModel(i), "fMass");
  334.         speed = Vehicle_GetSpeed(i) + 0.001;
  335.         Fuel[i] -= ((mass / (mass * 4.5)) * ((speed / 60) + 0.015) / 30) * ((dist / 10) + 0.001);
  336.         if(Fuel[i] < 0.1) SetVehicleParams(i, VEHICLE_TYPE_ENGINE, 0);
  337.         GetVehiclePos(i, VehicleLastCoords[i][0], VehicleLastCoords[i][1], VehicleLastCoords[i][2]);
  338.     }
  339.  
  340.     return 1;
  341. }
  342.  
  343. forward Refuel(playerid, vehicleid);
  344. public Refuel(playerid, vehicleid)
  345. {
  346.     new price = floatround(0.5 * LITRE_PRICE);
  347.     if(GetPlayerMoney(playerid) < price)
  348.     {
  349.         SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}You don't have enough money.");
  350.         Fuel_ResetPlayer(playerid);
  351.         Vehicle_StartEngine(vehicleid);
  352.         return 1;
  353.     }
  354.  
  355.     FuelBought[playerid] += 0.5;
  356.     Fuel[vehicleid] += 0.5;
  357.  
  358.     new string[64];
  359.     format(string, sizeof(string), "~b~~h~Refueling...~n~~n~~w~Price: ~g~~h~$%d ~y~~h~(%.2fL)", floatround(FuelBought[playerid] * LITRE_PRICE), FuelBought[playerid]);
  360.     PlayerTextDrawSetString(playerid, FuelText[playerid], string);
  361.     SetPlayerProgressBarValue(playerid, FuelBar[playerid], Fuel[vehicleid]);
  362.     GivePlayerMoney(playerid, -price);
  363.  
  364.     if(Fuel[vehicleid] > 100.0)
  365.     {
  366.         Fuel[vehicleid] = 100.0;
  367.         Fuel_ResetPlayer(playerid);
  368.         Vehicle_StartEngine(vehicleid);
  369.     }
  370.  
  371.     return 1;
  372. }
  373.  
  374. CMD:refuel(playerid, params[])
  375. {
  376.     if(GetPlayerState(playerid) != PLAYER_STATE_DRIVER) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}You can't use this command if you're not a driver.");
  377.     if(UsingPumpID[playerid] == -1) {
  378.         if(GetPlayerMoney(playerid) < 1) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}You don't have enough money.");
  379.         if(Fuel[ GetPlayerVehicleID(playerid) ] > 99.0) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}Your vehicle doesn't need a refuel.");
  380.         new id = Pump_Closest(playerid);
  381.         if(id == -1) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}You're not near a gas pump.");
  382.         if(IsPlayerConnected(PumpData[id][pumpUser])) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}The pump you want to use is not available.");
  383.         UsingPumpID[playerid] = id;
  384.         PumpData[id][pumpUser] = playerid;
  385.         Pump_Update(id);
  386.  
  387.         new vid = GetPlayerVehicleID(playerid);
  388.         SetVehicleParams(vid, VEHICLE_TYPE_ENGINE, 0);
  389.         PlayerTextDrawSetString(playerid, FuelText[playerid], "~b~~h~Refueling...~n~~n~~w~Price: ~g~~h~$0 ~y~~h~(0.00L)");
  390.         PlayerTextDrawShow(playerid, FuelText[playerid]);
  391.         RefuelTimer[playerid] = SetTimerEx("Refuel", 350, true, "ii", playerid, vid);
  392.  
  393.         SendClientMessage(playerid, -1, "You can write /refuel again to stop refueling.");
  394.     }else{
  395.         Fuel_ResetPlayer(playerid);
  396.         Vehicle_StartEngine( GetPlayerVehicleID(playerid) );
  397.     }
  398.  
  399.     return 1;
  400. }
  401.  
  402. CMD:arefuel(playerid, params[])
  403. {
  404.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}Only RCON admins can use this command.");
  405.     new id = GetPlayerVehicleID(playerid);
  406.     if(!IsValidVehicle(id)) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}You're not in a vehicle.");
  407.     Fuel[id] = 100.0;
  408.     SendClientMessage(playerid, -1, "Vehicle refueled.");
  409.     return 1;
  410. }
  411.  
  412. CMD:arefuelall(playerid, params[])
  413. {
  414.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xE74C3CFF, "ERROR: {FFFFFF}Only RCON admins can use this command.");
  415.     for(new i, v = GetVehiclePoolSize(); i <= v; i++) if(IsValidVehicle(i)) Fuel[i] = 100.0;
  416.     SendClientMessageToAll(-1, "A RCON admin has refueled all vehicles.");
  417.     return 1;
  418. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top