Advertisement
Southclaw

Southclaw's Gun Game

Dec 6th, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.59 KB | None | 0 0
  1. #include <YSI\y_hooks>
  2.  
  3. #define GUN_INDEX_FILE          "Gungame/index.ini"
  4. #define GUN_DATA_FILE           "Gungame/%s.dat"
  5. #define GUN_MAX_ARENA           (8)
  6. #define GUN_MAX_ARENA_NAME      (24)
  7. #define GUN_MAX_ARENA_FILENAME  (8+GUN_MAX_ARENA_NAME+4)
  8. #define GUN_MAX_SPAWNS          (16)
  9.  
  10. #define GUN_MAX_WEAPON_LEVEL    14
  11.  
  12.  
  13. enum E_GUN_ARENA_DATA
  14. {
  15. Float:      gun_posX,
  16. Float:      gun_posY,
  17. Float:      gun_posZ,
  18. Float:      gun_rotZ
  19. }
  20. enum e_gun_leaderboard_data
  21. {
  22.             gun_playerId,
  23.             gun_playerScore
  24. }
  25.  
  26.  
  27. new
  28.             gun_ArenaData[GUN_MAX_ARENA][GUN_MAX_SPAWNS][E_GUN_ARENA_DATA],
  29.             gun_ArenaName[GUN_MAX_ARENA][GUN_MAX_ARENA_NAME],
  30.             gun_TotalSpawns[GUN_MAX_ARENA],
  31.             gun_TotalArenas,
  32.             gun_CurrentArena = -1,
  33.             gun_Active,
  34.             gun_Leaderboard[MAX_PLAYERS][e_gun_leaderboard_data];
  35.  
  36. new
  37.             gun_PlayerLbPosition[MAX_PLAYERS],
  38.             gun_WeaponLevel[MAX_PLAYERS],
  39.             gun_WeaponTable[GUN_MAX_WEAPON_LEVEL] =
  40.             {
  41.                 22,
  42.                 23,
  43.                 24,
  44.                 25,
  45.                 26,
  46.                 27,
  47.                 28,
  48.                 29,
  49.                 33,
  50.                 30,
  51.                 31,
  52.                 34,
  53.                 37,
  54.                 4
  55.             };
  56.  
  57. new
  58. PlayerText: gun_PlayerPosTextBackground,
  59. PlayerText: gun_PlayerPosTextForground,
  60. PlayerText: gun_PlayerPosTextPlace,
  61. PlayerText: gun_PlayerPosTextLevel,
  62. Text:       gun_LbColumn1[MAX_PLAYERS],
  63. Text:       gun_LbColumn2[MAX_PLAYERS],
  64. Text:       gun_LbColumn3[MAX_PLAYERS];
  65.  
  66.  
  67. CMD:gungame(playerid, params[])
  68. {
  69.     if(gCurrentChallenge != CHALLENGE_NONE)
  70.     {
  71.         Msg(playerid, YELLOW, " >  You can't start a minigame while a challenge is active.");
  72.         return 1;
  73.     }
  74.     if(gCurrentMinigame[playerid] == MINIGAME_GUNGAME)
  75.     {
  76.         gun_Leave(playerid);
  77.     }
  78.     else if(gCurrentMinigame[playerid] == MINIGAME_NONE)
  79.     {
  80.         if(gun_Active == DGW_STATE_INACTIVE)
  81.             gun_FormatArenaList(playerid);
  82.  
  83.         else
  84.             gun_Join(playerid);
  85.     }
  86.     else
  87.     {
  88.         Msg(playerid, YELLOW, " >  Please exit your current minigame before joining another");
  89.     }
  90.     return 1;
  91. }
  92.  
  93. gun_Start(arena)
  94. {
  95.     gun_CurrentArena = arena;
  96.     gun_Active = true;
  97. }
  98.  
  99. gun_Join(playerid, msg = true)
  100. {
  101.     gCurrentMinigame[playerid] = MINIGAME_GUNGAME;
  102.     SetPlayerVirtualWorld(playerid, MINIGAME_WORLD);
  103.  
  104.     gun_UpdateLeaderboard();
  105.     gun_Spawn(playerid);
  106.  
  107.     if(msg)
  108.     {
  109.         PlayerLoop(i)if(IsPlayerInFreeRoam(i) || gCurrentMinigame[playerid] == MINIGAME_GUNGAME)
  110.             MsgF(i, YELLOW, " >  %P"#C_YELLOW" has joined the "#C_BLUE"Gun-Game "#C_YELLOW"minigame on the map: "#C_BLUE"%s"#C_YELLOW"! Type "#C_ORANGE"/gungame "#C_YELLOW"to join.",
  111.                 playerid, gun_ArenaName[gun_CurrentArena]);
  112.     }
  113. }
  114.  
  115. gun_Spawn(playerid)
  116. {
  117.     new spawn = random(gun_TotalSpawns[gun_CurrentArena]);
  118.  
  119.     SetPlayerPos(playerid,
  120.         gun_ArenaData[gun_CurrentArena][spawn][gun_posX],
  121.         gun_ArenaData[gun_CurrentArena][spawn][gun_posY],
  122.         gun_ArenaData[gun_CurrentArena][spawn][gun_posZ]+1.0);
  123.  
  124.     FreezePlayer(playerid, 1000);
  125.  
  126.     SetPlayerFacingAngle(playerid, gun_ArenaData[gun_CurrentArena][spawn][gun_rotZ]);
  127.     SetPlayerVirtualWorld(playerid, MINIGAME_WORLD);
  128.    
  129.     SetPlayerHP(playerid, 100.0);
  130.  
  131.     ResetPlayerWeapons(playerid);
  132.     GivePlayerWeapon(playerid,
  133.         gun_WeaponTable[gun_WeaponLevel[playerid]],
  134.         WepData[gun_WeaponTable[gun_WeaponLevel[playerid]]][MagSize] * 4);
  135.    
  136.     SetCameraBehindPlayer(playerid);
  137.     gun_ShowHud(playerid);
  138. }
  139.  
  140. script_GunGame_OnPlayerDeath(playerid, killerid, reason)
  141. {
  142.     if(gCurrentMinigame[playerid] == MINIGAME_GUNGAME)
  143.     {
  144.         if(killerid != INVALID_PLAYER_ID)
  145.         {
  146.             if(gCurrentMinigame[killerid] == MINIGAME_GUNGAME)
  147.             {
  148.                 if(reason == 0)
  149.                 {
  150.                     if(gun_WeaponLevel[playerid] > 0)
  151.                         gun_WeaponLevel[playerid]--;
  152.  
  153.                     gun_UpdateLeaderboard();
  154.                 }
  155.                 if(gun_WeaponLevel[killerid] < GUN_MAX_WEAPON_LEVEL)
  156.                 {
  157.                     gun_WeaponLevel[killerid]++;
  158.                     gun_UpdateWeaponLevelUI(killerid);
  159.                     gun_UpdateLeaderboard();
  160.  
  161.                     if(gun_WeaponLevel[killerid] == GUN_MAX_WEAPON_LEVEL)
  162.                     {
  163.                         gun_EndRound();
  164.                     }
  165.                     else
  166.                     {
  167.                         ResetPlayerWeapons(killerid);
  168.                         GivePlayerWeapon(killerid,
  169.                             gun_WeaponTable[gun_WeaponLevel[killerid]],
  170.                             WepData[gun_WeaponTable[gun_WeaponLevel[killerid]]][MagSize] * 4);
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.         else
  176.         {
  177.             if(gun_WeaponLevel[playerid] > 0)
  178.                 gun_WeaponLevel[playerid]--;
  179.  
  180.             gun_UpdateLeaderboard();
  181.         }
  182.         gun_HideHud(playerid);
  183.     }
  184. }
  185. hook OnPlayerSpawn(playerid)
  186. {
  187.     if(gCurrentMinigame[playerid] == MINIGAME_GUNGAME)
  188.         defer gun_Respawn(playerid);
  189.  
  190.     return 1;
  191. }
  192. timer gun_Respawn[500](playerid)
  193. {
  194.     gun_Spawn(playerid);
  195. }
  196.  
  197. gun_EndRound()
  198. {
  199.     gun_UpdateLeaderboard();
  200.     PlayerLoop(i)
  201.     {
  202.         if(gCurrentMinigame[i] == MINIGAME_GUNGAME)
  203.         {
  204.             gun_ShowLeaderboard(i);
  205.             gun_HideHud(i);
  206.             ResetPlayerWeapons(i);
  207.         }
  208.     }
  209.     defer gun_NewRound();
  210. }
  211.  
  212. timer gun_NewRound[10000]()
  213. {
  214.     PlayerLoop(i)
  215.     {
  216.         if(gCurrentMinigame[i] == MINIGAME_GUNGAME)
  217.         {
  218.             gun_WeaponLevel[i] = 0;
  219.             gun_UpdateWeaponLevelUI(i);
  220.             gun_Spawn(i);
  221.             gun_HideLeaderboard(i);
  222.         }
  223.     }
  224.     gun_UpdateLeaderboard();
  225. }
  226.  
  227. gun_UpdateLeaderboard()
  228. {
  229.     for(new i; i < MAX_PLAYERS; i++)
  230.     {
  231.         if(gCurrentMinigame[i] == MINIGAME_GUNGAME)
  232.         {
  233.             gun_Leaderboard[i][gun_playerId] = i;
  234.             gun_Leaderboard[i][gun_playerScore] = gun_WeaponLevel[i];
  235.         }
  236.         else
  237.         {
  238.             gun_Leaderboard[i][gun_playerId] = -1;
  239.             gun_Leaderboard[i][gun_playerScore] = -1;
  240.         }
  241.     }
  242.  
  243.     SortDeepArray(gun_Leaderboard, gun_playerScore, .order = SORT_DESC);
  244.  
  245.     for(new i; i < MAX_PLAYERS; i++)
  246.     {
  247.         if(0 <= gun_Leaderboard[i][gun_playerId] < MAX_PLAYERS)
  248.         {
  249.             gun_PlayerLbPosition[gun_Leaderboard[i][gun_playerId]] = i;
  250.             gun_UpdateLbPositionUI(gun_Leaderboard[i][gun_playerId]);
  251.         }
  252.     }
  253. }
  254.  
  255. gun_UpdateWeaponLevelUI(playerid)
  256. {
  257.     new str[13];
  258.  
  259.     format(str, 13, "Level: %d/%d", gun_WeaponLevel[playerid] + 1, GUN_MAX_WEAPON_LEVEL);
  260.     PlayerTextDrawSetString(playerid, gun_PlayerPosTextLevel, str);
  261. }
  262.  
  263. gun_UpdateLbPositionUI(playerid)
  264. {
  265.     new str[4];
  266.  
  267.     valstr(str, gun_PlayerLbPosition[playerid] + 1);
  268.     strcat(str, returnOrdinal(gun_PlayerLbPosition[playerid] + 1));
  269.  
  270.     PlayerTextDrawSetString(playerid, gun_PlayerPosTextPlace, str);
  271. }
  272.  
  273. gun_ShowHud(playerid)
  274. {
  275.     PlayerTextDrawShow(playerid, gun_PlayerPosTextBackground);
  276.     PlayerTextDrawShow(playerid, gun_PlayerPosTextForground);
  277.     PlayerTextDrawShow(playerid, gun_PlayerPosTextPlace);
  278.     PlayerTextDrawShow(playerid, gun_PlayerPosTextLevel);
  279. }
  280. gun_HideHud(playerid)
  281. {
  282.     PlayerTextDrawHide(playerid, gun_PlayerPosTextBackground);
  283.     PlayerTextDrawHide(playerid, gun_PlayerPosTextForground);
  284.     PlayerTextDrawHide(playerid, gun_PlayerPosTextPlace);
  285.     PlayerTextDrawHide(playerid, gun_PlayerPosTextLevel);
  286. }
  287.  
  288. gun_ShowLeaderboard(playerid)
  289. {
  290.     new str[24];
  291.  
  292.     SortDeepArray(gun_Leaderboard, gun_playerScore, .order = SORT_DESC);
  293.  
  294.     for(new i; i < MAX_PLAYERS; i++)
  295.     {
  296.         if(0 <= gun_Leaderboard[i][gun_playerId] < MAX_PLAYERS)
  297.         {
  298.             valstr(str, i+1);
  299.             TextDrawSetString(gun_LbColumn1[i], str);
  300.  
  301.             GetPlayerName(gun_Leaderboard[i][gun_playerId], str, 24);
  302.             TextDrawSetString(gun_LbColumn2[i], str);
  303.  
  304.             valstr(str, gun_WeaponLevel[gun_Leaderboard[i][gun_playerId]]);
  305.             TextDrawSetString(gun_LbColumn3[i], str);
  306.  
  307.             TextDrawShowForPlayer(playerid, gun_LbColumn1[i]);
  308.             TextDrawShowForPlayer(playerid, gun_LbColumn2[i]);
  309.             TextDrawShowForPlayer(playerid, gun_LbColumn3[i]);
  310.         }
  311.         else break;
  312.     }
  313. }
  314. gun_HideLeaderboard(playerid)
  315. {
  316.     for(new i; i < MAX_PLAYERS; i++)
  317.     {
  318.         TextDrawHideForPlayer(playerid, gun_LbColumn1[i]);
  319.         TextDrawHideForPlayer(playerid, gun_LbColumn2[i]);
  320.         TextDrawHideForPlayer(playerid, gun_LbColumn3[i]);
  321.     }
  322. }
  323.  
  324. gun_Leave(playerid, msg = true)
  325. {
  326.     gCurrentMinigame[playerid] = MINIGAME_NONE;
  327.     SetPlayerVirtualWorld(playerid, FREEROAM_WORLD);
  328.  
  329.     ResetPlayerWeapons(playerid);
  330.     gun_HideHud(playerid);
  331.  
  332.     if(gun_PlayersInGame() == 0)
  333.     {
  334.         gun_End();
  335.     }
  336.     if(msg)
  337.     {
  338.         PlayerLoop(i)if(IsPlayerInFreeRoam(i) || gCurrentMinigame[i] == MINIGAME_GUNGAME)
  339.             MsgF(i, YELLOW, " >  %P"#C_YELLOW" has left the "#C_BLUE"/gungame "#C_YELLOW"minigame.", playerid);
  340.     }
  341. }
  342.  
  343. gun_End()
  344. {
  345.     PlayerLoop(i)
  346.     {
  347.         if(gCurrentMinigame[i] == MINIGAME_GUNGAME)
  348.         {
  349.             ResetPlayerWeapons(i);
  350.         }
  351.     }
  352.     gun_Active = false;
  353. }
  354.  
  355. gun_PlayersInGame()
  356. {
  357.     new count;
  358.     PlayerLoop(i)
  359.     {
  360.         if(gCurrentMinigame[i] == MINIGAME_GUNGAME)count++;
  361.     }
  362.     return count;
  363. }
  364.  
  365. gun_FormatArenaList(playerid)
  366. {
  367.     new
  368.         list[GUN_MAX_ARENA * (GUN_MAX_ARENA_NAME + 1)];
  369.  
  370.     for(new i; i < gun_TotalArenas; i++)
  371.     {
  372.         strcat(list, gun_ArenaName[i]);
  373.         strcat(list, "\n");
  374.     }
  375.    
  376.     ShowPlayerDialog(playerid, d_GungameList, DIALOG_STYLE_LIST, "Gun Game", list, "Start", "Cancel");
  377. }
  378.  
  379. hook OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  380. {
  381.     if(dialogid == d_GungameList && response)
  382.     {
  383.         gun_Start(listitem);
  384.         gun_Join(playerid);
  385.     }
  386.     return 1;
  387. }
  388.  
  389. gun_LoadArenas()
  390. {
  391.     new
  392.         File:idxFile,
  393.         File:datFile,
  394.         line[128],
  395.         filename[GUN_MAX_ARENA_FILENAME],
  396.         arenaidx,
  397.         spawnidx;
  398.  
  399.     if(!fexist(GUN_INDEX_FILE))
  400.         return print("ERROR: Gungame index file not found");
  401.  
  402.     idxFile = fopen(GUN_INDEX_FILE, io_read);
  403.    
  404.     gun_TotalArenas = 0;
  405.  
  406.     while(fread(idxFile, line))
  407.     {
  408.         if(line[strlen(line)-2] == '\r')
  409.             line[strlen(line)-2] = EOS;
  410.  
  411.         gun_ArenaName[arenaidx][0] = EOS;
  412.         strcat(gun_ArenaName[arenaidx], line);
  413.  
  414.         format(filename, sizeof(filename), GUN_DATA_FILE, line);
  415.        
  416.         if(!fexist(filename))
  417.             return printf("ERROR: Gungame arena '%s' file not found", filename);
  418.        
  419.         datFile = fopen(filename, io_read);
  420.  
  421.         while(fread(datFile, line))
  422.         {
  423.             if(!sscanf(line, "e<p<,>ffff>", gun_ArenaData[arenaidx][spawnidx]))
  424.                 spawnidx++;
  425.         }
  426.         gun_TotalSpawns[arenaidx] = spawnidx;
  427.         arenaidx++;
  428.     }
  429.  
  430.     fclose(idxFile);
  431.    
  432.     gun_TotalArenas = arenaidx;
  433.  
  434.     return 1;
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement