Southclaw

Southclaw's Vehicle Streaming Script

Aug 12th, 2012
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.67 KB | None | 0 0
  1. #include <a_samp>
  2. #include <streamer>
  3. #include <sscanf2>
  4. #include <colours>
  5. #include <foreach>
  6.  
  7. #include <YSI\y_va>
  8. #include <formatex>
  9. #include "../scripts/System/PlayerFunctions.pwn"
  10.  
  11. #undef MAX_PLAYERS
  12. #define MAX_PLAYERS 16
  13.  
  14. #define VEHICLE_INDEX_FILE      "vehicles/index.ini"
  15. #define VEHICLE_DATA_FILE       "vehicles/%s.dat"
  16.  
  17. #define GRID_DIVIDE             16
  18. #define GRID_TOTAL              (GRID_DIVIDE * GRID_DIVIDE)
  19. #define MAX_STREAMED_VEHICLES   (MAX_VEHICLES / MAX_PLAYERS)
  20. #define VEHICLE_STREAM_RANGE    100.0
  21. #define MAX_PERSONAL_VEHICLE    16
  22. #define PERSONAL_VEHICLE_DATA   "vehicles/special/player.dat"
  23.  
  24.  
  25. enum
  26. {
  27.     VEHICLE_GROUP_NONE              = -1,
  28.     VEHICLE_GROUP_CASUAL,           // 0
  29.     VEHICLE_GROUP_CASUAL_DESERT,    // 1
  30.     VEHICLE_GROUP_CASUAL_COUNTRY,   // 2
  31.     VEHICLE_GROUP_SPORT,            // 3
  32.     VEHICLE_GROUP_OFFROAD,          // 4
  33.     VEHICLE_GROUP_BIKE,             // 5
  34.     VEHICLE_GROUP_FASTBIKE,         // 6
  35.     VEHICLE_GROUP_MILITARY,         // 7
  36.     VEHICLE_GROUP_POLICE,           // 8
  37.     VEHICLE_GROUP_BIGPLANE,         // 9
  38.     VEHICLE_GROUP_SMALLPLANE,       // 10
  39.     VEHICLE_GROUP_HELICOPTER,       // 11
  40.     VEHICLE_GROUP_BOAT              // 12
  41. }
  42.  
  43. enum E_VEHICLE_DATA
  44. {
  45.     v_id,
  46.     v_model,
  47.     Float:v_posX,
  48.     Float:v_posY,
  49.     Float:v_posZ,
  50.     Float:v_rot,
  51.     v_spawnArea
  52. }
  53.  
  54.  
  55. new
  56.     TotalVehicles,
  57.     TotalPersonalVehicles,
  58.     gVehicleData[GRID_TOTAL+1][MAX_STREAMED_VEHICLES][E_VEHICLE_DATA],
  59.     gAreaData[GRID_TOTAL+1],
  60.     gVehicleCount[GRID_TOTAL+1],
  61.     gAreaStreamed[GRID_TOTAL+1],
  62.     gLoadingVehicles,
  63.     gPlayerCurrentArea[MAX_PLAYERS];
  64.  
  65.  
  66. new gModelGroup[13][68]=
  67. {
  68.     // VEHICLE_GROUP_CASUAL
  69.     {
  70.         404,442,479,549,600,496,496,401,
  71.         410,419,436,439,517,518,401,410,
  72.         419,436,439,474,491,496,517,518,
  73.         526,527,533,545,549,580,589,600,
  74.         602,400,404,442,458,479,489,505,
  75.         579,405,421,426,445,466,467,492,
  76.         507,516,529,540,546,547,550,551,
  77.         566,585,587,412,534,535,536,567,
  78.         575,576, 0, ...
  79.     },
  80.     // VEHICLE_GROUP_CASUAL_DESERT,
  81.     {
  82.         404,479,445,542,466,467,549,540,
  83.         424,400,500,505,489,499,422,600,
  84.         515,543,554,443,508,525, 0, ...
  85.     },
  86.     // VEHICLE_GROUP_CASUAL_COUNTRY,
  87.     {
  88.         499,422,498,609,455,403,414,514,
  89.         600,413,515,440,543,531,478,456,
  90.         554,445,518,401,527,542,546,410,
  91.         549,508,525, 0, ...
  92.     },
  93.     // VEHICLE_GROUP_SPORT,
  94.     {
  95.         558,559,560,561,562,565,411,451,
  96.         477,480,494,502,503,506,541, 0, ...
  97.     },
  98.     // VEHICLE_GROUP_OFFROAD,
  99.     {
  100.         400,505,579,422,478,543,554, 0, ...
  101.     },
  102.     // VEHICLE_GROUP_BIKE,
  103.     {
  104.         509,481,510,462,448,463,586,468,
  105.         471, 0, ...
  106.     },
  107.     // VEHICLE_GROUP_FASTBIKE,
  108.     {
  109.         581,522,461,521, 0, ...
  110.     },
  111.     // VEHICLE_GROUP_MILITARY,
  112.     {
  113.         433,432,601,470, 0, ...
  114.     },
  115.     // VEHICLE_GROUP_POLICE,
  116.     {
  117.         523,596,598,597,599,490,528,427
  118.     },
  119.     // VEHICLE_GROUP_BIGPLANE,
  120.     {
  121.         519,553,577,592, 0, ...
  122.     },
  123.     // VEHICLE_GROUP_SMALLPLANE,
  124.     {
  125.         460,476,511,512,513,593, 0, ...
  126.     },
  127.     // VEHICLE_GROUP_HELICOPTER,
  128.     {
  129.         548,487,417,487,488,487,497,487,
  130.         563,477,469,487, 0, ...
  131.     },
  132.     // VEHICLE_GROUP_BOAT,
  133.     {
  134.         472,473,493,595,484,430,453,452,
  135.         446,454, 0, ...
  136.     }
  137. };
  138.  
  139. public OnFilterScriptInit()
  140. {
  141.     new
  142.         Float:xMax,
  143.         Float:xMin,
  144.         Float:yMax,
  145.         Float:yMin,
  146.         xLoop,
  147.         yLoop,
  148.         idx;
  149.  
  150.     while(idx < GRID_DIVIDE * GRID_DIVIDE)
  151.     {
  152.         xMin = ((6000 / GRID_DIVIDE) * xLoop) - 3000.0;
  153.         xMax = ((6000 / GRID_DIVIDE) * (xLoop+1)) - 3000.0;
  154.  
  155.         yMin = ((6000 / GRID_DIVIDE) * yLoop) - 3000.0;
  156.         yMax = ((6000 / GRID_DIVIDE) * (yLoop+1)) - 3000.0;
  157.  
  158.         gAreaData[idx] = CreateDynamicRectangle(xMin, yMin, xMax, yMax, -1, 0);
  159.  
  160.         xLoop++;
  161.         idx++;
  162.  
  163.         if(xLoop == GRID_DIVIDE)
  164.         {
  165.             xLoop = 0;
  166.             yLoop++;
  167.         }
  168.  
  169.     }
  170.     LoadAllVehicles();
  171.     LoadStaticVehiclesFromFile("vehicles/special/trains.dat");
  172.    
  173.     for(new i;i<MAX_PLAYERS;i++)gPlayerCurrentArea[i] = -1;
  174.    
  175.     printf("Total Dynamic Vehicles: %d", TotalVehicles);
  176. }
  177.  
  178. public OnFilterScriptExit()
  179. {
  180.     for(new i; i < GRID_TOTAL; i++)
  181.     {
  182.         for(new j; j < MAX_STREAMED_VEHICLES; j++)
  183.         {
  184.             if(IsValidVehicle(gVehicleData[i][j][v_id]))
  185.                 DestroyVehicle(gVehicleData[i][j][v_id]);
  186.         }
  187.     }
  188. }
  189.  
  190. LoadAllVehicles()
  191. {
  192.     gLoadingVehicles = true; // Don't try to spawn vehicles while this is loading!
  193.     new
  194.         File:f=fopen(VEHICLE_INDEX_FILE, io_read),
  195.         line[128],
  196.         str[128];
  197.  
  198.     while(fread(f, line))
  199.     {
  200.         if(line[strlen(line)-2] == '\r')line[strlen(line) - 2] = EOS;
  201.         format(str, 128, VEHICLE_DATA_FILE, line);
  202.         LoadVehiclesFromFile(str);
  203.     }
  204.    
  205.     fclose(f);
  206.     gLoadingVehicles = false;
  207. }
  208.  
  209. LoadStaticVehiclesFromFile(file[])
  210. {
  211.     if(!fexist(file))return print("VEHICLE FILE NOT FOUND");
  212.  
  213.     new
  214.         File:f=fopen(file, io_read),
  215.         line[128],
  216.         Float:tmp_posX,
  217.         Float:tmp_posY,
  218.         Float:tmp_posZ,
  219.         Float:tmp_rot,
  220.         tmp_model;
  221.  
  222.     while(fread(f, line))
  223.     {
  224.         if(!sscanf(line, "p<,>ffffD(0)", tmp_posX, tmp_posY, tmp_posZ, tmp_rot, tmp_model))
  225.         {
  226.             AddStaticVehicle(tmp_model, tmp_posX, tmp_posY, tmp_posZ, tmp_rot, -1, -1);
  227.         }
  228.     }
  229.     fclose(f);
  230.     return 1;
  231. }
  232.  
  233.  
  234. new gCurModelGroup;
  235. LoadVehiclesFromFile(file[])
  236. {
  237.     if(!fexist(file))return print("VEHICLE FILE NOT FOUND");
  238.     new
  239.         File:f=fopen(file, io_read),
  240.         line[128],
  241.         Float:tmp_posX,
  242.         Float:tmp_posY,
  243.         Float:tmp_posZ,
  244.         Float:tmp_rot,
  245.         tmp_model,
  246.         idx;
  247.  
  248.     while(fread(f, line))
  249.     {
  250.         idx = 0;
  251.         if(!sscanf(line, "p<,>ffffD(0)", tmp_posX, tmp_posY, tmp_posZ, tmp_rot, tmp_model))
  252.         {
  253.             while(idx < GRID_TOTAL)
  254.             {
  255.                 if(IsPointInDynamicArea(gAreaData[idx], tmp_posX, tmp_posY, tmp_posZ))break;
  256.                 idx++;
  257.             }
  258.            
  259.             if(tmp_model == 0)tmp_model = PickRandomVehicleFromGroup(gCurModelGroup);
  260.  
  261.             if( gVehicleData[idx][gVehicleCount[idx]][v_model] == 403 ||
  262.                 gVehicleData[idx][gVehicleCount[idx]][v_model] == 443 ||
  263.                 gVehicleData[idx][gVehicleCount[idx]][v_model] == 514 ||
  264.                 gVehicleData[idx][gVehicleCount[idx]][v_model] == 515)tmp_posZ+=2.0;
  265.  
  266.             gVehicleData[idx][gVehicleCount[idx]][v_model]      = tmp_model;
  267.             gVehicleData[idx][gVehicleCount[idx]][v_posX]       = tmp_posX;
  268.             gVehicleData[idx][gVehicleCount[idx]][v_posY]       = tmp_posY;
  269.             gVehicleData[idx][gVehicleCount[idx]][v_posZ]       = tmp_posZ;
  270.             gVehicleData[idx][gVehicleCount[idx]][v_rot]            = tmp_rot;
  271.             gVehicleData[idx][gVehicleCount[idx]][v_spawnArea]  = idx;
  272.  
  273.             gVehicleCount[idx]++;
  274.             TotalVehicles++;
  275.  
  276.             idx++;
  277.         }
  278.         else if(sscanf(line, "'MODELGROUP:'d", gCurModelGroup) && strlen(line) > 3)print("LINE ERROR");
  279.     }
  280.     printf("\t-Loaded %d vehicles from %s", idx, file);
  281.     fclose(f);
  282.     return 1;
  283. }
  284. PickRandomVehicleFromGroup(group)
  285. {
  286.     new idx;
  287.     while(gModelGroup[group][idx] != 0)idx++;
  288.     return gModelGroup[group][random(idx)];
  289. }
  290. public OnPlayerEnterDynamicArea(playerid, areaid)
  291. {
  292.     if(gPlayerCurrentArea[playerid] == -1)
  293.     {
  294.         for(new i; i < GRID_TOTAL; i++)
  295.         {
  296.             if(IsPlayerInDynamicArea(playerid, gAreaData[i]))
  297.             {
  298.                 OnPlayerChangeArea(playerid, i, -1);
  299.                 return 1;
  300.             }
  301.         }
  302.     }
  303.     return 1;
  304. }
  305. public OnPlayerLeaveDynamicArea(playerid, areaid)
  306. {
  307.     for(new i; i < GRID_TOTAL; i++)
  308.     {
  309.         if(IsPlayerInDynamicArea(playerid, gAreaData[i]))
  310.         {
  311.             OnPlayerChangeArea(playerid, i, gPlayerCurrentArea[playerid]);
  312.             return 1;
  313.         }
  314.     }
  315.  
  316.     CreateAreaVehicles(GRID_TOTAL);
  317.     gPlayerCurrentArea[playerid] = GRID_TOTAL;
  318.     return 1;
  319. }
  320.  
  321. OnPlayerChangeArea(playerid, newareaid, oldareaid)
  322. {
  323.     if(gLoadingVehicles)return 1;
  324.  
  325.     if(GetPlayersInDynamicArea(gAreaData[newareaid]) == 1)
  326.     {
  327.         gPlayerCurrentArea[playerid] = newareaid;
  328.         if(-1 < newareaid < GRID_TOTAL)CreateAreaVehicles(newareaid);
  329.     }
  330.  
  331.     if(-1 < oldareaid < GRID_TOTAL)
  332.     {
  333.         if(GetPlayersInDynamicArea(gAreaData[oldareaid]) == 0)
  334.         {
  335.             gPlayerCurrentArea[playerid] = newareaid;
  336.             DestroyAreaVehicles(oldareaid, newareaid);
  337.         }
  338.     }
  339.     return 1;
  340. }
  341. CreateAreaVehicles(area)
  342. {
  343.     if(gAreaStreamed[area])return 0;
  344.     gAreaStreamed[area] = true;
  345.     for(new i; i < gVehicleCount[area]; i++)
  346.     {
  347.         if(gVehicleData[area][i][v_model] == 0)continue;
  348.        
  349.         if(gVehicleData[area][i][v_spawnArea] != area)
  350.         {
  351.             ShiftVehicleToNextAreaIndex(i, area, gVehicleData[area][i][v_spawnArea]);
  352.             continue;
  353.         }
  354.  
  355.         gVehicleData[area][i][v_id] = CreateVehicle(
  356.             gVehicleData[area][i][v_model],
  357.             gVehicleData[area][i][v_posX],
  358.             gVehicleData[area][i][v_posY],
  359.             gVehicleData[area][i][v_posZ],
  360.             gVehicleData[area][i][v_rot],
  361.             -1, -1, -1);
  362.  
  363.         SetVehicleNumberPlate(gVehicleData[area][i][v_id], RandomNumberPlateString());
  364.  
  365.     }
  366.     return 1;
  367. }
  368. DestroyAreaVehicles(area, newarea)
  369. {
  370.     if(!gAreaStreamed[area])return 0;
  371.     gAreaStreamed[area] = false;
  372.     for(new i; i < gVehicleCount[area]; i++)
  373.     {
  374.         if(IsAnyPlayerInVehicle(gVehicleData[area][i][v_id]))
  375.             ShiftVehicleToNextAreaIndex(i, area, newarea);
  376.  
  377.         if(IsValidVehicle(gVehicleData[area][i][v_id]))
  378.             DestroyVehicle(gVehicleData[area][i][v_id]);
  379.     }
  380.     return 0;
  381. }
  382.  
  383. GetPlayersInDynamicArea(area)
  384. {
  385.     new count;
  386.     for(new i;i<MAX_PLAYERS;i++)if(IsPlayerConnected(i))if(IsPlayerInDynamicArea(i, area))count++;
  387.     return count;
  388. }
  389.  
  390. IsAnyPlayerInVehicle(vehicleid)
  391. {
  392.     for(new i;i<MAX_PLAYERS;i++)if(IsPlayerInVehicle(i, vehicleid))return 1;
  393.     return 0;
  394. }
  395.  
  396. ShiftVehicleToNextAreaIndex(index, oldarea, newarea)
  397. {
  398.     new newindex;
  399.     while(gVehicleData[newarea][newindex][v_model] != 0)newindex++;
  400.  
  401.     gVehicleData[newarea][newindex] = gVehicleData[oldarea][index];
  402.    
  403.     gVehicleData[oldarea][index][v_id]      = -1;
  404.     gVehicleData[oldarea][index][v_model]   = 0;
  405.     gVehicleData[oldarea][index][v_posX]        = 0.0;
  406.     gVehicleData[oldarea][index][v_posY]        = 0.0;
  407.     gVehicleData[oldarea][index][v_posZ]        = 0.0;
  408.     gVehicleData[oldarea][index][v_rot]     = 0.0;
  409.    
  410.     gVehicleCount[oldarea]--;
  411.     gVehicleCount[newarea]++;
  412. }
  413.  
  414.  
  415.  
  416.  
  417. RandomNumberPlateString()
  418. {
  419.     new str[9];
  420.     for(new c;c<8;c++)
  421.     {
  422.         if(c<4)str[c] = 'A' + random(26);
  423.         else if(c>4)str[c] = '0' + random(10);
  424.         str[4] = ' ';
  425.     }
  426.     return str;
  427. }
Add Comment
Please, Sign In to add comment