Advertisement
Kyance

SAMP - Playerbased capture zones

Feb 7th, 2015
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.48 KB | None | 0 0
  1. /*
  2. >>> READ EVERY 'IMPORTANT' TAG! (CTRL+F 'IMPORTANT' and then press F3).
  3. >>> READ EVERYTHING FROM LINE 1 - 50,
  4.                                     THEY CAN SAVE YOUR LIFE (not really, but they'll inform you about some "lazy bug fixes" & how to avoid breaking my fixes)!!!!!!!!
  5.  
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7.  
  8. STUFF YOU MIGHT NEED TO KNOW,
  9.  
  10. 'Interval' - The time it takes to capture the zone, default: 25.
  11. 'Controller' - The player who has captured/owns the, default: INVALID_PLAYER_ID.
  12.  
  13. 'Zone' - Zone stuff for 'CreateDynamicSquare'
  14. 'zMinX', 'zMinY', 'zMaxX', 'zMaxY' - The Zone positions/borders or w.e.
  15.  
  16. 'TextX', 'TextY', 'TextZ' - The 3D Text Label position.
  17. 'zText' - 3D Text Label for zone
  18.  
  19. 'Name' - The Zone Name.
  20.  
  21. 'Capturing' - Is the zone being captured or not.
  22.  
  23. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  24.  
  25. Q&A (with myself),
  26.  
  27.  
  28. Q: "How to add the zones?!?!!?!?!?!"
  29. A: go to 'zInfo' (line 116), and add the stuff.
  30.  
  31. Q: "What stuff?"
  32. A: The stuff which I named above (Interval, Controller, Pos ...).
  33.  
  34. Q: "How"
  35. A: Just look at the example zones I gave you.
  36.  
  37. Q: "How to create zones??"
  38. A: I personally use "Xtremes Vehicle Plotter", but im sure there are many other editors out there.
  39.  
  40. Q: "filterscript unload??"
  41. A: Check the console, if it prints(shows, writes..) errors and 2 notes, you did something wrong with adding the zones (line 123 - 125).
  42.  
  43. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  44.  
  45. POSSIBLE ERROR/WARNING YOU MIGHT GET & HOW TO FIX IT,
  46.  
  47.  
  48. "error 052: multi-dimensional arrays must be fully initialized"
  49.  
  50. Your 'MAX_ZONES' needs to be LOWERED! (-1 the number, EXAMPLE: You replace the 6 with a 5)
  51. * If you added a zone, you must raise it, if you removed a zone, you must lower it.
  52.  
  53. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  54.  
  55. "error 018: initialization data exceeds declared size",
  56. "error 001: expected token: "}", but found "{"",
  57. "error 054: unmatched closing brace ("}")",
  58. "error 010: invalid function or declaration"
  59.  
  60. Your 'MAX_ZONES' needs to be RAISED! (+1 the number, EXAMPLE: You replace the 5 with a 6)
  61. * If you added a zone, you must raise it, if you removed a zone, you must lower it.
  62.  
  63.  
  64.  
  65. **
  66. I haven't 'got' any other warns/errors that caused me to get confused,
  67. but most of the errors are being caused in the "zInfo" part -- So, right after you add a zone there, compile it.
  68. Might help you know where you went wrong.
  69.  
  70. */
  71.  
  72.  
  73. #define FILTERSCRIPT
  74.  
  75. #include <a_samp>
  76. #include <foreach>
  77. #include <streamer>
  78. #include <zcmd>
  79.  
  80.  
  81. #define MAX_ZONES 6
  82.  
  83. #define ZONE_MESSAGE_CAPTURING 0
  84. #define ZONE_MESSAGE_SUCCESS 1
  85. #define ZONE_MESSAGE_FAIL 2
  86.  
  87. enum playerInfo
  88. {
  89.     bool:ZoneOwned[ MAX_ZONES ],
  90.  
  91.     CapturingZone,
  92.  
  93.     Name[ 24 ]
  94. };
  95. new
  96.     pInfo[ MAX_PLAYERS ][ playerInfo ],
  97.     Timer_ZoneControl
  98. ;
  99.  
  100.  
  101. enum zoneInfo
  102. {
  103.     Interval, Controller,
  104.     Zone,
  105.    
  106.     Float:zMinX, Float:zMinY,
  107.     Float:zMaxX, Float:zMaxY,
  108.    
  109.     Float:TextX, Float:TextY, Float:TextZ,
  110.     Text3D:zText,
  111.  
  112.     Name[ 18 ],
  113.    
  114.     bool:Capturing
  115. };
  116. new zInfo[ MAX_ZONES ][ zoneInfo ] =
  117. {
  118.     { 25, INVALID_PLAYER_ID, -1, -1564.835, 2534.099, -1366.311, 2674.233, -1495.9861, 2601.3513, 55.6872, Text3D:zText, "El Quebrados", false },
  119.     { 25, INVALID_PLAYER_ID, -1, -794.0955, 1996.917, -607.2495, 2090.34, -726.5607, 2061.4075, 60.3828, Text3D:zText, "Sherman Dam", false },
  120.     { 25, INVALID_PLAYER_ID, -1, 1107.436, 2714.903, 1396.395, 2854.154, 1279.2919, 2779.8875, 10.8203, Text3D:zText, "Golf Course", false },
  121.     { 25, INVALID_PLAYER_ID, -1, 1255.821, 2072.804, 1384.68, 2212.055, 1356.5494, 2144.2888, 11.0156, Text3D:zText, "Bandits Stadium", false },
  122.     { 25, INVALID_PLAYER_ID, -1, 1333.917, 676.4326, 1536.969, 800.2107, 1494.1503, 707.7706, 10.6719, Text3D:zText, "Blackfield Chapel", false },
  123.     //Add your next zones here, above 'To_Fix_Bugs'
  124.     { 25, INVALID_PLAYER_ID, -1, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, Text3D:zText, "To_Fix_Bugs", true } //IMPORTANT: Do NOT remove or change this!!! (last zone won't work, so yes..)
  125.     //IMPORTANT: Do NOT add your zones here!!! (Add them above 'To_Fix_Bugs'!!)
  126. };
  127.    
  128. #if defined FILTERSCRIPT
  129.  
  130. public OnFilterScriptInit()
  131. {
  132.     print("\n--------------------------------------");
  133.     print("'Player-Based Capture Zones' loading..\nVersion: 1.0");
  134.     print("--------------------------------------\n");
  135.    
  136.     for(new i = 0; i < MAX_ZONES; i++)
  137.     {
  138.         EditZone( i, true );
  139.     }
  140.     foreach( Player, i )
  141.     {
  142.         GetPlayerName(i, pInfo[ i ][ Name ], 24);
  143.         pInfo[ i ][ CapturingZone ] = -1;
  144.        
  145.         TogglePlayerAllDynamicAreas(i, true);
  146.     }
  147.    
  148.     Timer_ZoneControl = SetTimer("UpdateZones", 1000, true);
  149.     return 1;
  150. }
  151.  
  152. public OnFilterScriptExit()
  153. {
  154.     print("\n--------------------------------------");
  155.     print("'Player-Based Capture Zones' unloading..");
  156.     print("--------------------------------------\n");
  157.    
  158.     for(new i = 0; i < MAX_ZONES; i++)
  159.     {
  160.         EditZone( i, false );
  161.     }
  162.    
  163.     KillTimer( Timer_ZoneControl );
  164.     return 1;
  165. }
  166.  
  167. #endif
  168.  
  169. //- - - - CALLBACKs ( MAIN ONES )
  170.  
  171. public OnPlayerConnect(playerid)
  172. {
  173.     GetPlayerName(playerid, pInfo[ playerid ][ Name ], 24);
  174.     for(new i; i < MAX_ZONES; i++)
  175.     {
  176.         if(zInfo[ i ][ Controller ] != playerid) continue;
  177.  
  178.         pInfo[ playerid ][ ZoneOwned ][ i ] = false;
  179.         zInfo[ i ][ Controller ] = INVALID_PLAYER_ID;
  180.     }
  181.     TogglePlayerAllDynamicAreas(playerid, true);
  182.     pInfo[ playerid ][ CapturingZone ] = -1;
  183.     return 1;
  184. }
  185.  
  186. public OnPlayerDisconnect(playerid, reason)
  187. {
  188.     new
  189.         string[ 42 ]
  190.     ;
  191.     for(new i; i < MAX_ZONES; i++)
  192.     {
  193.         if(zInfo[ i ][ Controller ] != playerid) continue;
  194.  
  195.         format(string, sizeof( string ), "'%s'\nNOT CAPTURED", zInfo[ i ][ Name ]);
  196.         Update3DTextLabelText(zInfo[ i ][ zText ], -1, string);
  197.        
  198.         pInfo[ playerid ][ ZoneOwned ][ i ] = false;
  199.         zInfo[ i ][ Controller ] = INVALID_PLAYER_ID;
  200.     }
  201.     TogglePlayerAllDynamicAreas(playerid, false);
  202.    
  203.     zInfo[ pInfo[ playerid ][ CapturingZone ] ][ Capturing ] = false;
  204.     pInfo[ playerid ][ CapturingZone ] = -1;
  205.     return 1;
  206. }
  207.  
  208. public OnPlayerSpawn(playerid)
  209. {
  210.     if(pInfo[ playerid ][ CapturingZone ] != -1)
  211.     {
  212.         EndCapture(playerid, pInfo[ playerid ][ CapturingZone ], false, true);
  213.     }
  214.     return 1;
  215. }
  216.  
  217. public OnPlayerDeath(playerid, killerid, reason)
  218. {
  219.     if(pInfo[ playerid ][ CapturingZone ] != -1)
  220.     {
  221.         EndCapture(playerid, pInfo[ playerid ][ CapturingZone ], false, true);
  222.     }
  223.     return 1;
  224. }
  225.  
  226. //- - - - CALLBACKs ( STREAMER ONES )
  227.  
  228. public OnPlayerEnterDynamicArea(playerid, areaid)
  229. {
  230.     areaid = areaid-1; //IMPORTANT: Do NOT remove this... as long as it doesn't ruin stuff, this is my "lazy bug fix" of a weird id bug.
  231.     //It loads fine on console, but i dunno.. messes up the names and stuff (could be a bug w/ the new streamer? :O)
  232.    
  233.     if(!IsPlayerInAnyVehicle(playerid))
  234.     {
  235.         if(zInfo[ areaid ][ Capturing ] == false)
  236.         {
  237.             if(pInfo[ playerid ][ ZoneOwned ][ areaid ] == false)
  238.             {
  239.                 StartCapture( playerid, areaid, true );
  240.             }
  241.             else SendClientMessage(playerid, -1, "(ZONE) You already own this zone!");
  242.         }
  243.         else SendClientMessage(playerid, -1, "(ZONE) This zone is already being captured!");
  244.     }
  245.     return 1;
  246. }
  247. public OnPlayerLeaveDynamicArea(playerid, areaid)
  248. {
  249.     areaid = areaid-1; //IMPORTANT: Do NOT remove this... as long as it doesn't ruin stuff, this is my "lazy bug fix" of a weird id bug.
  250.     //It loads fine on console, but i dunno.. messes up the names and stuff (could be a bug w/ the new streamer? :O)
  251.    
  252.     if(!IsPlayerInAnyVehicle(playerid))
  253.     {
  254.         if( pInfo[ playerid ][ CapturingZone ] == areaid )
  255.         {
  256.             EndCapture( playerid, areaid, false, true );
  257.         }
  258.     }
  259.     return 1;
  260. }
  261.  
  262. public OnPlayerStateChange(playerid, newstate, oldstate)
  263. {
  264.     if(IsPlayerInAnyDynamicArea(playerid, 0))
  265.     {
  266.         if(newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER)
  267.         {
  268.             EndCapture( playerid, pInfo[ playerid ][ CapturingZone ], false, true );
  269.             SendClientMessage(playerid, -1, "(ZONE) You must be on-foot!");
  270.         }
  271.     }
  272.     return 1;
  273. }
  274.  
  275. //- - - - FUNCTIONs
  276.  
  277. ZoneMessage( playerid, zoneid, messagetype, bool:sendtoall = false )
  278. {
  279.     new
  280.         string[ 86 ]
  281.     ;
  282.     switch( messagetype )
  283.     {
  284.         case ZONE_MESSAGE_CAPTURING:
  285.         {
  286.             switch( sendtoall )
  287.             {
  288.                 case false:
  289.                 {
  290.                     format(string, sizeof( string ), "(ZONE) You're capturing '%s'.", zInfo[ zoneid ][ Name ]);
  291.                     SendClientMessage(playerid, -1, string);
  292.                 }
  293.                 case true:
  294.                 {
  295.                     format(string, sizeof( string ), "(ZONE) %s is capturing '%s'.", pInfo[ playerid ][ Name ], zInfo[ zoneid ][ Name ]);
  296.                     SendClientMessageToAll(-1, string);
  297.                 }
  298.             }
  299.         }
  300.         case ZONE_MESSAGE_SUCCESS:
  301.         {
  302.             switch( sendtoall )
  303.             {
  304.                 case false:
  305.                 {
  306.                     format(string, sizeof( string ), "(ZONE) You've captured '%s'!", zInfo[ zoneid ][ Name ]);
  307.                     SendClientMessage(playerid, -1, string);
  308.                 }
  309.                 case true:
  310.                 {
  311.                     format(string, sizeof( string ), "(ZONE) %s has captured '%s'!", pInfo[ playerid ][ Name ], zInfo[ zoneid ][ Name ]);
  312.                     SendClientMessageToAll(-1, string);
  313.                 }
  314.             }
  315.         }
  316.         case ZONE_MESSAGE_FAIL:
  317.         {
  318.             switch( sendtoall )
  319.             {
  320.                 case false:
  321.                 {
  322.                     format(string, sizeof( string ), "(ZONE) You've failed to capture '%s'.", zInfo[ zoneid ][ Name ]);
  323.                     SendClientMessage(playerid, -1, string);
  324.                 }
  325.                 case true:
  326.                 {
  327.                     format(string, sizeof( string ), "(ZONE) %s has failed to capture '%s'.", pInfo[ playerid ][ Name ], zInfo[ zoneid ][ Name ]);
  328.                     SendClientMessageToAll(-1, string);
  329.                 }
  330.             }
  331.         }
  332.     }
  333. }
  334.  
  335. StartCapture( playerid, zoneid, bool:message )
  336. {
  337.     new
  338.         string[ 42 ]
  339.     ;
  340.  
  341.     pInfo[ playerid ][ CapturingZone ] = zoneid;
  342.  
  343.     zInfo[ zoneid ][ Capturing ] = true;
  344.     zInfo[ zoneid ][ Interval ] = 25;
  345.    
  346.     format(string, sizeof( string ), "'%s'\nBEING CAPTURED", zInfo[ zoneid ][ Name ]);
  347.     Update3DTextLabelText(zInfo[ zoneid ][ zText ], -1, string);
  348.  
  349.     if(message) ZoneMessage( playerid, zoneid, ZONE_MESSAGE_CAPTURING, true );
  350. }
  351. EndCapture( playerid, zoneid, bool:success, bool:message )
  352. {
  353.     new
  354.         string[ 52 ]
  355.     ;
  356.  
  357.     pInfo[ playerid ][ CapturingZone ] = -1;
  358.  
  359.     zInfo[ zoneid ][ Interval ] = 25;
  360.     zInfo[ zoneid ][ Capturing ] = false;
  361.  
  362.     switch( success )
  363.     {
  364.         case true:
  365.         {
  366.             pInfo[ playerid ][ ZoneOwned ][ zoneid ] = true;
  367.             zInfo[ zoneid ][ Controller ] = playerid;
  368.            
  369.             format(string, sizeof( string ), "'%s'\nCAPTURED BY: %s", zInfo[ zoneid ][ Name ], pInfo[ playerid ][ Name ]);
  370.             Update3DTextLabelText(zInfo[ zoneid ][ zText ], -1, string);
  371.                
  372.             if(message) ZoneMessage( playerid, zoneid, ZONE_MESSAGE_SUCCESS, true );
  373.         }
  374.         case false:
  375.         {
  376.             pInfo[ playerid ][ ZoneOwned ][ zoneid ] = false;
  377.            
  378.             if(zInfo[ zoneid ][ Controller ] != -1)
  379.             {
  380.                 format(string, sizeof( string ), "'%s'\nCAPTURED BY: %s", zInfo[ zoneid ][ Name ], pInfo[ zInfo[ zoneid ][ Controller ] ][ Name ]);
  381.                 Update3DTextLabelText(zInfo[ zoneid ][ zText ], -1, string);
  382.             }
  383.             else
  384.             {
  385.                 format(string, sizeof( string ), "'%s'\nNOT CAPTURED", zInfo[ zoneid ][ Name ]);
  386.                 Update3DTextLabelText(zInfo[ zoneid ][ zText ], -1, string);
  387.             }
  388.            
  389.             if(message) ZoneMessage( playerid, zoneid, ZONE_MESSAGE_FAIL, true );
  390.         }
  391.     }
  392. }
  393.  
  394.  
  395. EditZone( zoneid, bool:create )
  396. {
  397.     switch( create )
  398.     {
  399.         case true:
  400.         {
  401.             new
  402.                 string[ 42 ]
  403.             ;
  404.             if( zoneid != MAX_ZONES-1 )
  405.             {
  406.                 if(strcmp("To_Fix_Bugs", zInfo[ zoneid ][ Name ], true))
  407.                 {
  408.                     zInfo[ zoneid ][ Zone ] = CreateDynamicRectangle( zInfo[ zoneid ][ zMinX ], zInfo[ zoneid ][ zMinY ], zInfo[ zoneid ][ zMaxX ], zInfo[ zoneid ][ zMaxY ], 0, -1, -1);
  409.  
  410.                     format(string, sizeof( string ), "'%s'\nNOT CAPTURED", zInfo[ zoneid ][ Name ]);
  411.                     zInfo[ zoneid ][ zText ] = Create3DTextLabel(string, -1, zInfo[ zoneid ][ TextX ], zInfo[ zoneid ][ TextY ], zInfo[ zoneid ][ TextZ ], 85, 0, 1);
  412.  
  413.                     printf("--------------------\nzone %s(%d) created", zInfo[ zoneid ][ Name ], zoneid);
  414.                 }
  415.                 else
  416.                 {
  417.                     printf("ERROR: 'bug-zone %s' loaded with id %d\nNOTE: 'bug-zone' has an invalid id!\nNOTE: are you sure 'bug-zone' is last in the list?", zInfo[ zoneid ][ Name ], zoneid);
  418.                     SendRconCommand("unloadfs capturezones");
  419.                 }
  420.             }
  421.             else
  422.             {
  423.                 zInfo[ zoneid ][ Zone ] = CreateDynamicRectangle( zInfo[ zoneid ][ zMinX ], zInfo[ zoneid ][ zMinY ], zInfo[ zoneid ][ zMaxX ], zInfo[ zoneid ][ zMaxY ], 69, -1, -1);
  424.                 printf("--------------------\nbug-zone %s(%d) created at world 69\n--------------------\n", zInfo[ zoneid ][ Name ], zoneid);
  425.             }
  426.         }
  427.         case false:
  428.         {
  429.             DestroyDynamicArea( zoneid );
  430.             Delete3DTextLabel( zInfo[ zoneid ][ zText ]);
  431.            
  432.             printf("--------------------\nzone %s(%d) deleted", zInfo[ zoneid ][ Name ], zoneid);
  433.         }
  434.     }
  435. }
  436.  
  437.  
  438. forward UpdateZones();
  439. public UpdateZones()
  440. {
  441.     new
  442.         pzoneid,
  443.         str[ 50 ]
  444.     ;
  445.     foreach( Player, i )
  446.     {
  447.         if(pInfo[ i ][ CapturingZone ] == -1) continue;
  448.         else
  449.         {
  450.             pzoneid = pInfo[ i ][ CapturingZone ];
  451.            
  452.             zInfo[ pzoneid ][ Interval ]--;
  453.             if(zInfo[ pzoneid ][ Interval ] > 0) format(str, sizeof( str ), "~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~r~%d", zInfo[ pzoneid ][ Interval ]), GameTextForPlayer(i, str, 1250, 3);
  454.             else EndCapture( i, pzoneid, true, true ), GameTextForPlayer(i, "~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~g~CAPTURED", 2500, 3);
  455.         }
  456.     }
  457.     return 1;
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement