Guest User

Boombox save and load system

a guest
Jan 16th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.96 KB | None | 0 0
  1. // Includes
  2. #include <a_samp>
  3. #include <zcmd>
  4. #include <sscanf2>
  5. #include <streamer>
  6. #include <dini>
  7. #include <SII>
  8. #include <dcmd>
  9.  
  10.  
  11.  
  12.  
  13. // Defines
  14. #define COLOR_WHITE 0xFFFFFFFF
  15. #define COLOR_LIGHTBLUE 0x33CCFFFF
  16. #define COLOR_GREY 0xAFAFAFFF
  17. #define MAX_BOOMBOX             (600)
  18. // Variables
  19. new Boombox[MAX_PLAYERS];
  20. new BoomboxObject[MAX_PLAYERS];
  21. new BoomboxStream[MAX_PLAYERS];
  22. new BoomboxPlayer[MAX_PLAYERS];
  23. new BoomboxURL[MAX_PLAYERS][256];
  24. new Float:bpos[MAX_PLAYERS][4];
  25.  
  26. enum dav
  27. {
  28.     boomplayerid,
  29.     boomboxurl[192],
  30.     Float:bposA,
  31.     Float:bposB,
  32.     Float:bposC
  33. };
  34. new BoomBoxInfo[MAX_BOOMBOX][dav];
  35. new BoomBoxIDTaken[MAX_BOOMBOX];
  36. new boomboxes;
  37.  
  38. LoadBoombox()
  39. {
  40.     for(new i = 1; i < 180; i++)
  41.     {
  42.         new file[100];
  43.         new boomboxurlname[192];
  44.         format(file, sizeof(file), "Boxes/%d.ini", i);
  45.         if(fexist(file) && INI_Open(file))
  46.         {
  47.             BoomBoxInfo[i][boomplayerid] = INI_ReadInt("boomplayerid");
  48.             INI_ReadString(boomboxurlname, "boomboxurl");
  49.             strmid(BoomBoxInfo[i][boomboxurl], boomboxurlname, 0, strlen(boomboxurlname), 255);
  50.             BoomBoxInfo[i][bposA] = INI_ReadFloat("bposA");
  51.             BoomBoxInfo[i][bposB] = INI_ReadFloat("bposB");
  52.             BoomBoxInfo[i][bposC] = INI_ReadFloat("bposC");
  53.             PlayAudioStreamForPlayer(BoomBoxInfo[i][boomplayerid], BoomBoxInfo[i][boomboxurl], BoomBoxInfo[i][bposA], BoomBoxInfo[i][bposB], BoomBoxInfo[i][bposC], 30, 1);
  54.             // BoomboxPlayer[playerid] = i;
  55.             BoomboxStream[playerid] = 1;
  56.             SendClientMessage(playerid, COLOR_GREY, "You are listening to music coming out of a nearby boombox.");
  57.             BoomBoxIDTaken[i] = 1;
  58.             boomboxes += 1;
  59.             strdel(boomboxurlname, 0, 192);
  60.             INI_Close();
  61.         }
  62.     }
  63.     return 1;
  64. }
  65.  
  66. SaveBoombox()
  67. {
  68.     for(new i = 1; i < 180; i++)
  69.     {
  70.         if(BoomBoxIDTaken[i] == 0) continue;
  71.         new file[100];
  72.         format(file, sizeof(file), "Boxes/%d.ini", i);
  73.         if(INI_Open(file))
  74.         {
  75.             INI_WriteInt("boomplayerid", BoomBoxInfo[i][boomplayerid]);
  76.             INI_WriteString("boomboxurl", BoomBoxInfo[i][boomboxurl]);
  77.             INI_WriteFloat("bposA", BoomBoxInfo[i][bposA]);
  78.             INI_WriteFloat("bposB", BoomBoxInfo[i][bposB]);
  79.             INI_WriteFloat("bposC", BoomBoxInfo[i][bposC]);
  80.             INI_Save();
  81.             INI_Close();
  82.         }
  83.     }
  84.     return 1;
  85. }
  86.  
  87. public OnFilterScriptInit()
  88. {
  89.     LoadBoombox();
  90.     return 1;
  91. }
  92.  
  93.  
  94. // Clearing variables
  95. public OnPlayerConnect(playerid)
  96. {
  97.     Boombox[playerid] = 0;
  98.     BoomboxPlayer[playerid] = -1;
  99.     BoomboxStream[playerid] = 0;
  100.     bpos[playerid][0] = 0; bpos[playerid][1] = 0; bpos[playerid][2] = 0; bpos[playerid][3] = 0;
  101.     format(BoomboxURL[playerid], 256, "");
  102.     if(IsValidDynamicObject(BoomboxObject[playerid])) DestroyDynamicObject(BoomboxObject[playerid]);
  103.     return 1;
  104. }
  105.  
  106. // Clearing variables & Stopping boombox music on disconnect (Double check)
  107. public OnPlayerDisconnect(playerid)
  108. {
  109.     Boombox[playerid] = 0;
  110.     BoomboxPlayer[playerid] = -1;
  111.     BoomboxStream[playerid] = 0;
  112.     bpos[playerid][0] = 0; bpos[playerid][1] = 0; bpos[playerid][2] = 0; bpos[playerid][3] = 0;
  113.     format(BoomboxURL[playerid], 256, "");
  114.     if(IsValidDynamicObject(BoomboxObject[playerid])) DestroyDynamicObject(BoomboxObject[playerid]);
  115.     for(new i=0; i<MAX_PLAYERS; i++)
  116.     {
  117.         if(IsPlayerConnected(i))
  118.         {
  119.             if(BoomboxPlayer[i] == playerid)
  120.             {
  121.                 BoomboxStream[i] = 0;
  122.                 BoomboxPlayer[i] = -1;
  123.                 StopAudioStreamForPlayer(i);
  124.                 SendClientMessage(i, COLOR_GREY, " The boombox creator has disconnected from the server.");
  125.             }
  126.         }
  127.     }
  128.     return 1;
  129. }
  130.  
  131.  
  132. // Boombox command - Usage: /boombox [URL]
  133. CMD:boombox(playerid, params[])
  134. {
  135.     new string[128];
  136.     if(!Boombox[playerid])
  137.     {
  138.         if(sscanf(params, "s[256]", params)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /boombox [music url]");
  139.         for(new i=0; i<MAX_PLAYERS; i++)
  140.         {
  141.             if(IsPlayerConnected(i))
  142.             {
  143.                     if(Boombox[i])
  144.                     {
  145.                         if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
  146.                         {
  147.                             SendClientMessage(playerid, COLOR_GREY, " There is another boombox nearby, place yours somewhere else.");
  148.                             return 1;
  149.                         }
  150.                     }
  151.             }
  152.         }
  153.         Boombox[playerid] = 1;
  154.         format(string, sizeof(string), " You have placed your boombox at your location.");
  155.         SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
  156.         GetPlayerPos(playerid, bpos[playerid][0], bpos[playerid][1], bpos[playerid][2]); bpos[playerid][2] = bpos[playerid][2] - 1;
  157.         GetPlayerFacingAngle(playerid, bpos[playerid][3]); bpos[playerid][3] = bpos[playerid][3] +180;
  158.         BoomboxObject[playerid] = CreateDynamicObject(2103, bpos[playerid][0], bpos[playerid][1], bpos[playerid][2], 0, 0, bpos[playerid][3]);
  159.         format(BoomboxURL[playerid], 256, "%s", params);
  160.     }
  161.     else
  162.     {
  163.         Boombox[playerid] = 0;
  164.         format(string, sizeof(string), " You have removed your boombox.");
  165.         SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
  166.         DestroyDynamicObject(BoomboxObject[playerid]);
  167.         format(BoomboxURL[playerid], 256, "");
  168.         for(new i=0; i<MAX_PLAYERS; i++)
  169.         {
  170.             if(IsPlayerConnected(i))
  171.             {
  172.                 if(BoomboxPlayer[i] == playerid)
  173.                 {
  174.                     BoomboxStream[i] = 0;
  175.                     BoomboxPlayer[i] = -1;
  176.                     StopAudioStreamForPlayer(i);
  177.                     SendClientMessage(i, COLOR_GREY, " The boombox creator has removed his boombox.");
  178.                 }
  179.             }
  180.         }
  181.     }
  182.     return 1;
  183. }
  184.  
  185. // Boombox editing - Usage: /boomboxnext [url]
  186. CMD:boomboxnext(playerid, params[])
  187. {
  188.     if(!Boombox[playerid]) return SendClientMessage(playerid, COLOR_GREY, "You don't have a boombox placed.");
  189.     if(sscanf(params, "s[256]", params)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /boomboxnext [music url]");
  190.     SendClientMessage(playerid, COLOR_GREY, " You have changed the music your boombox is playing.");
  191.     format(BoomboxURL[playerid], 256, "%s", params);
  192.     for(new i=0; i<MAX_PLAYERS; i++)
  193.     {
  194.         if(IsPlayerConnected(i))
  195.         {
  196.                 if(BoomboxPlayer[i] == playerid)
  197.                 {
  198.                     PlayAudioStreamForPlayer(i, BoomboxURL[playerid], bpos[playerid][0], bpos[playerid][1], bpos[playerid][2], 30, 1);
  199.                     SendClientMessage(i, COLOR_GREY, " The boombox music you're listening to has changed.");
  200.                 }
  201.         }
  202.     }
  203.     return 1;
  204. }
  205.  
  206.  
  207. // Playing/Stopping boombox music for nearby players
  208. public OnPlayerUpdate(playerid)
  209. {
  210.     if(!BoomboxStream[playerid])
  211.     {
  212.         for(new i=0; i<MAX_PLAYERS; i++)
  213.         {
  214.             if(IsPlayerConnected(i))
  215.             {
  216.                 if(Boombox[i])
  217.                 {
  218.                     if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
  219.                     {
  220.                         PlayAudioStreamForPlayer(playerid, BoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 30, 1);
  221.                         BoomboxPlayer[playerid] = i;
  222.                         BoomboxStream[playerid] = 1;
  223.                         SendClientMessage(playerid, COLOR_GREY, " You are listening to music coming out of a nearby boombox.");
  224.                     }
  225.                 }
  226.             }
  227.         }
  228.     }
  229.     else
  230.     {
  231.         new i = BoomboxPlayer[playerid];
  232.         if(!IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
  233.         {
  234.             BoomboxStream[playerid] = 0;
  235.             BoomboxPlayer[playerid] = -1;
  236.             StopAudioStreamForPlayer(playerid);
  237.             SendClientMessage(playerid, COLOR_GREY, " You have went far away from the boombox.");
  238.         }
  239.     }
  240.     return 1;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment