Advertisement
Phanto90

PBlock v1.1

Sep 7th, 2011
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 17.22 KB | None | 0 0
  1. /*
  2. ================================================================================
  3.                                                  
  4.                         =====[Phanto's Script]=====
  5.                         |         PBlock minigame |
  6.                         |            Version: 1.1 |
  7.                         |           Type: Include |
  8.                         ===========================
  9.  
  10. Please, don't remove credits.
  11. ================================================================================
  12. */
  13.  
  14.  
  15. #if defined PBlock_Included
  16.   #endinput
  17. #endif
  18. #define PBlock_Included
  19.  
  20. #include a_samp
  21.  
  22. //=============================DIALOGS
  23. #define DIALOG_PBLOCK  1030
  24. //=============================MACRO
  25. #define PRESSED(%0) \
  26.     (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  27. //=============================CHECKPLAYERPLAYING
  28. new bool:pblock_isplaying[MAX_PLAYERS];
  29. new bool:just_pressed_key[MAX_PLAYERS];
  30. new player_scores[MAX_PLAYERS];
  31. //=============================TABLEBLOCKINFO
  32. new block_table[MAX_PLAYERS][5][10];
  33. //=============================COORDINATESFORMOVINGBLOCK
  34. new moving_block[MAX_PLAYERS][2];
  35. //=============================BLOCKSCOLOR
  36. enum
  37.     {
  38.     NO_BLOCK,
  39.     RED_BLOCK,
  40.     BLUE_BLOCK,
  41.     LIGHTBLUE_BLOCK,
  42.     YELLOW_BLOCK,
  43.     PURPLE_BLOCK,
  44.     GREEN_BLOCK,
  45.     LIGHTGREEN_BLOCK,
  46.     WHITE_BLOCK
  47.     }
  48. //=============================TIMER
  49. new pblock_timer[MAX_PLAYERS];
  50. //=============================TEXTDRAWS
  51. new Text:pblock_background,Text:pblock_background2,Text:pblock_title,
  52.     Text:pblock_score,Text:pblock_table;
  53.  
  54. //================================================================ONGAMEMODEINIT
  55. public OnGameModeInit()
  56. {
  57.     pblock_background = TextDrawCreate(228.000000, 44.000000, "_");
  58.     TextDrawBackgroundColor(pblock_background, 255);
  59.     TextDrawFont(pblock_background, 1);
  60.     TextDrawLetterSize(pblock_background, 0.500000, 39.199996);
  61.     TextDrawColor(pblock_background, -1);
  62.     TextDrawSetOutline(pblock_background, 0);
  63.     TextDrawSetProportional(pblock_background, 1);
  64.     TextDrawSetShadow(pblock_background, 1);
  65.     TextDrawUseBox(pblock_background, 1);
  66.     TextDrawBoxColor(pblock_background, 858993493);
  67.     TextDrawTextSize(pblock_background, 421.000000, 0.000000);
  68.  
  69.     pblock_title = TextDrawCreate(228.000000, 19.000000, "P~b~~h~~h~Block~w~ - v1.1      (~r~x~w~)");
  70.     TextDrawBackgroundColor(pblock_title, 255);
  71.     TextDrawFont(pblock_title, 2);
  72.     TextDrawLetterSize(pblock_title, 0.439999, 2.099999);
  73.     TextDrawColor(pblock_title, -1);
  74.     TextDrawSetOutline(pblock_title, 1);
  75.     TextDrawSetProportional(pblock_title, 1);
  76.     TextDrawUseBox(pblock_title, 1);
  77.     TextDrawBoxColor(pblock_title, 87875741);
  78.     TextDrawTextSize(pblock_title, 421.000000, 0.000000);
  79.  
  80.     pblock_score = TextDrawCreate(419.000000, 379.000000, "SCORE: 1003");
  81.     TextDrawAlignment(pblock_score, 3);
  82.     TextDrawBackgroundColor(pblock_score, 255);
  83.     TextDrawFont(pblock_score, 2);
  84.     TextDrawLetterSize(pblock_score, 0.370000, 1.900001);
  85.     TextDrawColor(pblock_score, -1);
  86.     TextDrawSetOutline(pblock_score, 1);
  87.     TextDrawSetProportional(pblock_score, 1);
  88.  
  89.     pblock_background2 = TextDrawCreate(244.000000, 44.000000, "_");
  90.     TextDrawBackgroundColor(pblock_background2, 255);
  91.     TextDrawFont(pblock_background2, 1);
  92.     TextDrawLetterSize(pblock_background2, 0.500000, 36.999984);
  93.     TextDrawColor(pblock_background2, -1);
  94.     TextDrawSetOutline(pblock_background2, 0);
  95.     TextDrawSetProportional(pblock_background2, 1);
  96.     TextDrawSetShadow(pblock_background2, 1);
  97.     TextDrawUseBox(pblock_background2, 1);
  98.     TextDrawBoxColor(pblock_background2, 673720575);
  99.     TextDrawTextSize(pblock_background2, 404.000000, 0.000000);
  100.  
  101.     pblock_table = TextDrawCreate(251.000000, 44.000000, "~w~O~b~~h~~h~O~y~O~r~O~p~O~n~OOOOO~n~OOOOO~n~OOOOO~n~OOOOO~n~OO OO~n~OOOOO~n~OOOOO~n~OOOOO~n~OOOOO");
  102.     TextDrawBackgroundColor(pblock_table, 255);
  103.     TextDrawFont(pblock_table, 2);
  104.     TextDrawLetterSize(pblock_table, 1.049998, 3.699997);
  105.     TextDrawColor(pblock_table, -1);
  106.     TextDrawSetOutline(pblock_table, 1);
  107.     TextDrawSetProportional(pblock_table, 0);
  108.     if (funcidx("PBlock_OnGameModeInit") != -1)
  109.     {
  110.         return CallLocalFunction("PBlock_OnGameModeInit", "");
  111.     }
  112.     return 1;
  113. }
  114.  
  115. #if defined _ALS_OnGameModeInit
  116.   #undef OnGameModeInit
  117. #else
  118.   #define _ALS_OnGameModeInit
  119. #endif
  120. #define OnGameModeInit PBlock_OnGameModeInit
  121. forward PBlock_OnGameModeInit();
  122. //============================================================ONPLAYERDISCONNECT
  123. public OnPlayerDisconnect(playerid, reason)
  124. {
  125.     PBlock_ExitGame(playerid);
  126.     if (funcidx("PBlock_OnPlayerDisconnect") != -1)
  127.     {
  128.         return CallLocalFunction("PBlock_OnPlayerDisconnect", "dd", playerid, reason);
  129.     }
  130.     return 1;
  131. }
  132.  
  133. #if defined _ALS_OnPlayerDisconnect
  134.   #undef OnPlayerDisconnect
  135. #else
  136.   #define _ALS_OnPlayerDisconnect
  137. #endif
  138. #define OnPlayerDisconnect PBlock_OnPlayerDisconnect
  139. forward PBlock_OnPlayerDisconnect(playerid, reason);
  140. //================================================================CREATENEWBLOCK
  141. stock PBlock_Create_NewBlock(playerid)
  142. {
  143.     if(block_table[playerid][2][9] != NO_BLOCK)
  144.         {
  145.         PBlock_GameOver(playerid);
  146.         return 0;
  147.         }
  148.     moving_block[playerid] = {2, 9};
  149.     block_table[playerid][2][9] = random(WHITE_BLOCK)+1;
  150.     PBlock_Refresh_Textdraw(playerid);
  151.     return 1;
  152. }
  153. //===============================================================PBLOCKSTARTGAME
  154. stock PBlock_StartGame(playerid)
  155. {
  156.     PB_PlaySound(playerid, 1076);
  157.     TogglePlayerControllable(playerid, 0);
  158.     player_scores[playerid] = 0;
  159.     TextDrawSetString(pblock_score, "Score: 0");
  160.     TextDrawShowForPlayer(playerid, pblock_background);
  161.     TextDrawShowForPlayer(playerid, pblock_background2);
  162.     TextDrawShowForPlayer(playerid, pblock_title);
  163.     TextDrawShowForPlayer(playerid, pblock_score);
  164.     TextDrawSetString(pblock_table, ""); //Resetting table.
  165.     TextDrawShowForPlayer(playerid, pblock_table);
  166.     ShowPlayerDialog(playerid, DIALOG_PBLOCK, 0, "{157FFF}P{FFCA16}Block {C1C1C1}- v1.1", "   {C1C1C1}Welcome to {157FFF}PBlock Minigame\n\n{C1C1C1}In what does it consist?\nYou will have to reach the maximum points setting in a column or a row \n 3 or more squares of the same color.\nHave fun!\n-{157FFF}Phanto","Play!", "Exit");
  167.     return 1;
  168. }
  169. //=================================================================PBLOCKEXITGAME
  170. stock PBlock_ExitGame(playerid)
  171. {
  172.     PB_PlaySound(playerid, 1077);
  173.     TextDrawHideForPlayer(playerid, pblock_background);
  174.     TextDrawHideForPlayer(playerid, pblock_background2);
  175.     TextDrawHideForPlayer(playerid, pblock_title);
  176.     TextDrawHideForPlayer(playerid, pblock_score);
  177.     TextDrawSetString(pblock_table, ""); //Resetting table.
  178.     TextDrawHideForPlayer(playerid, pblock_table);
  179.     KillTimer(pblock_timer[playerid]);
  180.     pblock_isplaying[playerid] = false;
  181.     TogglePlayerControllable(playerid, 1);
  182.     return 1;
  183. }
  184. //===============================================================PBLOCKSTARTGAME
  185. stock PBlock_GameStart(playerid)
  186. {
  187.     for(new i; i<5; i++)
  188.     {
  189.         for(new j; j<10; j++)
  190.             {
  191.             block_table[playerid][i][j] = NO_BLOCK; //Resetting block info
  192.             }
  193.     }
  194.     GameTextForPlayer(playerid, "~g~~h~~h~GAME STARTS!", 3000, 1);
  195.     PBlock_Create_NewBlock(playerid);
  196.     pblock_isplaying[playerid] = true;
  197.     pblock_timer[playerid] = SetTimerEx("PBlock_Fall", 1000, true, "d", playerid);
  198.     return 1;
  199. }
  200. //================================================================PBLOCKGAMEOVER
  201. stock PBlock_GameOver(playerid)
  202. {
  203.     pblock_isplaying[playerid] = false;
  204.     GameTextForPlayer(playerid, "~r~GAME OVER", 3000, 1);
  205.     KillTimer(pblock_timer[playerid]);
  206.     ShowPlayerDialog(playerid, DIALOG_PBLOCK, 0, "{157FFF}P{FFCA16}Block {C1C1C1}- v1.1", "   {157FFF}PBlock Minigame\n\n{EA3E13}You Lost{C1C1C1}!","Retry!", "Exit");
  207.     return 1;
  208. }
  209. //====================================================================PBLOCKFALL
  210. forward PBlock_Fall(playerid);
  211. public PBlock_Fall(playerid)
  212. {
  213.     if(!moving_block[playerid][1] || block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]-1] != NO_BLOCK)
  214.         {
  215.         new bool: all_blocks_deleted = false;
  216.         while(!all_blocks_deleted)
  217.             {
  218.             if(!PBlock_Deleting_Block(playerid))
  219.                 all_blocks_deleted = true;
  220.             }
  221.         PBlock_Create_NewBlock(playerid);
  222.         return 1;
  223.         }
  224.     PB_PlaySound(playerid, 1131);
  225.     block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]-1] = block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]];
  226.     block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]] = NO_BLOCK;
  227.     moving_block[playerid][1]--;
  228.     PBlock_Refresh_Textdraw(playerid);
  229.     return 1;
  230. }
  231. //=========================================================PBLOCKREFRESHTEXTDRAW
  232. stock PBlock_Refresh_Textdraw(playerid)
  233. {
  234.     new text_string[580];
  235.     for(new p_y = 9; p_y >= 0; p_y--)
  236.         {
  237.         for(new p_x; p_x < 5; p_x++)
  238.             {
  239.             switch(block_table[playerid][p_x][p_y])
  240.                 {
  241.                 case NO_BLOCK: strins(text_string, " ", strlen(text_string), 580);
  242.                 case RED_BLOCK: strins(text_string, "~r~~h~O", strlen(text_string), 580);
  243.                 case BLUE_BLOCK: strins(text_string, "~b~~h~O", strlen(text_string), 580);
  244.                 case LIGHTBLUE_BLOCK: strins(text_string, "~b~~h~~h~O", strlen(text_string), 580);
  245.                 case YELLOW_BLOCK: strins(text_string, "~y~O", strlen(text_string), 580);
  246.                 case PURPLE_BLOCK: strins(text_string, "~p~O", strlen(text_string), 580);
  247.                 case GREEN_BLOCK: strins(text_string, "~g~O", strlen(text_string), 580);
  248.                 case LIGHTGREEN_BLOCK: strins(text_string, "~g~~h~~h~O", strlen(text_string), 580);
  249.                 case WHITE_BLOCK: strins(text_string, "~w~O", strlen(text_string), 580);
  250.                 }
  251.             if(p_x == 4)
  252.                 {
  253.                 strins(text_string, "~n~", strlen(text_string), 580);
  254.                 }
  255.             }
  256.         }
  257.     TextDrawSetString(pblock_table, text_string);
  258.     TextDrawShowForPlayer(playerid,pblock_table);
  259.     return 1;
  260. }
  261. //============================================================CHECKBLOCKTODELETE
  262. stock PBlock_Deleting_Block(playerid)
  263. {
  264.     enum info_check {last_block_type, amount};
  265.     new pblock_check[info_check];
  266.     new score_string[50];
  267.     for(new p_x; p_x < 5; p_x++) //Checking columns
  268.         {
  269.         for(new p_y; p_y < 10; p_y++)
  270.             {
  271.             if(!p_y)
  272.                 {
  273.                 pblock_check[last_block_type] = block_table[playerid][p_x][p_y];
  274.                 pblock_check[amount] = 1;
  275.                 continue;
  276.                 }
  277.             if(block_table[playerid][p_x][p_y] == pblock_check[last_block_type])
  278.                 {
  279.                 pblock_check[amount]++;
  280.                 continue;
  281.                 }
  282.             else if(block_table[playerid][p_x][p_y] != pblock_check[last_block_type] || p_y == 5)
  283.                 {
  284.                 if(pblock_check[amount] >= 3 && pblock_check[last_block_type]!= NO_BLOCK)
  285.                     {
  286.                     player_scores[playerid]++;
  287.                     format(score_string, sizeof(score_string), "Score: %d", player_scores[playerid]);
  288.                     TextDrawSetString(pblock_score, score_string);
  289.                     TextDrawShowForPlayer(playerid, pblock_score);
  290.                     PB_PlaySound(playerid, 1057);
  291.                     for(new pb_del = p_y - pblock_check[amount]; pb_del < p_y; pb_del++)
  292.                         {
  293.                         block_table[playerid][p_x][pb_del] = NO_BLOCK;
  294.                         }
  295.                     }
  296.                 pblock_check[amount] = 1;
  297.                 pblock_check[last_block_type] = block_table[playerid][p_x][p_y];
  298.                 }
  299.             }
  300.         }
  301.     pblock_check[amount] = 0;
  302.     pblock_check[last_block_type] = NO_BLOCK;
  303.     for(new p_y; p_y < 10; p_y++) //Checking rows
  304.         {
  305.         for(new p_x; p_x < 5; p_x++)
  306.             {
  307.             if(!p_x)
  308.                 {
  309.                 pblock_check[last_block_type] = block_table[playerid][p_x][p_y];
  310.                 pblock_check[amount] = 1;
  311.                 continue;
  312.                 }
  313.             if(block_table[playerid][p_x][p_y] == pblock_check[last_block_type])
  314.                 {
  315.                 pblock_check[amount]++;
  316.                 continue;
  317.                 }
  318.             else if(block_table[playerid][p_x][p_y] != pblock_check[last_block_type] || p_x == 5)
  319.                 {
  320.                 if(pblock_check[amount] >= 3 && pblock_check[last_block_type]!= NO_BLOCK)
  321.                     {
  322.                     player_scores[playerid]++;
  323.                     format(score_string, sizeof(score_string), "Score: %d", player_scores[playerid]);
  324.                     TextDrawSetString(pblock_score, score_string);
  325.                     TextDrawShowForPlayer(playerid, pblock_score);
  326.                     PB_PlaySound(playerid, 1057);
  327.                     for(new pb_del = p_x - pblock_check[amount]; pb_del < p_x; pb_del++)
  328.                         {
  329.                         block_table[playerid][pb_del][p_y] = NO_BLOCK;
  330.                         PBlock_OrderColumn(playerid, pb_del);
  331.                         }
  332.                     return 1;
  333.                     }
  334.                 pblock_check[amount] = 1;
  335.                 pblock_check[last_block_type] = block_table[playerid][p_x][p_y];
  336.                 }
  337.             }
  338.         }
  339.     PBlock_Refresh_Textdraw(playerid);
  340.     return 0;
  341. }
  342. //=============================================================PBLOCKORDERCOLUMN
  343. stock PBlock_OrderColumn(playerid, p_x)
  344. {
  345.     new bool:all_blocks_ordered = false;
  346.     while(!all_blocks_ordered)
  347.     {
  348.     for(new p_y = 1; p_y < 10; p_y++)
  349.         {
  350.         if(block_table[playerid][p_x][p_y - 1] == NO_BLOCK)
  351.             {
  352.             block_table[playerid][p_x][p_y - 1] = block_table[playerid][p_x][p_y];
  353.             block_table[playerid][p_x][p_y] = NO_BLOCK;
  354.             }
  355.         }
  356.     if(PBlock_IsColumnOrdered(playerid, p_x))
  357.         all_blocks_ordered = true;
  358.     }
  359.     return 1;
  360. }
  361. //=========================================================PBLOCKISCOLUMNORDERED
  362. PBlock_IsColumnOrdered(playerid, p_x)
  363. {
  364.     new empty_block_coord = -1;
  365.     for(new p_y; p_y < 10; p_y++)
  366.         {
  367.         if(block_table[playerid][p_x][p_y] == NO_BLOCK)
  368.             empty_block_coord = p_y;
  369.         if(block_table[playerid][p_x][p_y] != NO_BLOCK && empty_block_coord != -1)
  370.             return 0;
  371.         }
  372.     return 1;
  373. }
  374. //==============================================================ONDIALOGRESPONSE
  375. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  376. {
  377.     if(dialogid == DIALOG_PBLOCK)
  378.         {
  379.         if(!response)
  380.             {
  381.             PBlock_ExitGame(playerid);
  382.             return 1;
  383.             }
  384.         PBlock_GameStart(playerid);
  385.         return 1;
  386.         }
  387.     if (funcidx("PBlock_OnDialogResponse") != -1)
  388.     {
  389.         return CallLocalFunction("PBlock_OnDialogResponse", "dddds", playerid, dialogid, response, listitem, inputtext);
  390.     }
  391.     return 1;
  392. }
  393.  
  394. #if defined _ALS_OnDialogResponse
  395.   #undef OnDialogResponse
  396. #else
  397.   #define _ALS_OnDialogResponse
  398. #endif
  399. #define OnDialogResponse PBlock_OnDialogResponse
  400. forward PBlock_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
  401. //================================================================ONPLAYERUPDATE
  402. public OnPlayerUpdate(playerid)
  403. {
  404.     new pb_keys, pb_updown, pb_leftright;
  405.     GetPlayerKeys(playerid, pb_keys, pb_updown, pb_leftright);
  406.     if(pb_leftright == 128 || pb_leftright == -128)
  407.         {
  408.         OnPlayerKeyStateChange(playerid, pb_leftright, 0); //Left & Right keys doesn't recall OnPlayerKeyStateChange, so we need to recall ourself
  409.         }
  410.     if (funcidx("PBlock_OnPlayerUpdate") != -1)
  411.     {
  412.         return CallLocalFunction("PBlock_OnPlayerUpdate", "d", playerid);
  413.     }
  414.     return 1;
  415. }
  416.  
  417. #if defined _ALS_OnPlayerUpdate
  418.   #undef OnPlayerUpdate
  419. #else
  420.   #define _ALS_OnPlayerUpdate
  421. #endif
  422. #define OnPlayerUpdate PBlock_OnPlayerUpdate
  423. forward PBlock_OnPlayerUpdate(playerid);
  424. //========================================================ONPLAYERKEYSTATECHANGE
  425. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  426. {
  427.     if(just_pressed_key[playerid]) return 1; //To prevent "double movement" of the moving block
  428.     if(pblock_isplaying[playerid])
  429.         {
  430.         if(PRESSED(KEY_LEFT))
  431.             {
  432.             if(!moving_block[playerid][0] || block_table[playerid][moving_block[playerid][0]-1][moving_block[playerid][1]] != NO_BLOCK) return 1;
  433.             PB_PlaySound(playerid, 1131);
  434.             block_table[playerid][moving_block[playerid][0]-1][moving_block[playerid][1]] = block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]];
  435.             block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]] = NO_BLOCK;
  436.             moving_block[playerid][0]--;
  437.             PBlock_Refresh_Textdraw(playerid);
  438.             just_pressed_key[playerid] = true;
  439.             SetTimerEx("Remove_JustPressed", 300, false, "d", playerid);
  440.             return 1;
  441.             }
  442.         if(PRESSED(KEY_RIGHT))
  443.             {
  444.             if(moving_block[playerid][0] == 4 || block_table[playerid][moving_block[playerid][0]+1][moving_block[playerid][1]] != NO_BLOCK) return 1;
  445.             PB_PlaySound(playerid, 1131);
  446.             block_table[playerid][moving_block[playerid][0]+1][moving_block[playerid][1]] = block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]];
  447.             block_table[playerid][moving_block[playerid][0]][moving_block[playerid][1]] = NO_BLOCK;
  448.             moving_block[playerid][0]++;
  449.             PBlock_Refresh_Textdraw(playerid);
  450.             just_pressed_key[playerid] = true;
  451.             SetTimerEx("Remove_JustPressed", 300, false, "d", playerid);
  452.             return 1;
  453.             }
  454.         }
  455.     if (funcidx("PBlock_OnPlayerKeyStateChange") != -1)
  456.     {
  457.         return CallLocalFunction("PBlock_OnPlayerKeyStateChange", "ddd", playerid, newkeys, oldkeys);
  458.     }
  459.     return 1;
  460. }
  461.  
  462. #if defined _ALS_OnPlayerKeyStateChange
  463.   #undef OnPlayerKeyStateChange
  464. #else
  465.   #define _ALS_OnPlayerKeyStateChange
  466. #endif
  467. #define OnPlayerKeyStateChange PBlock_OnPlayerKeyStateChange
  468. forward PBlock_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  469. //=============================================================REMOVEJUSTPRESSED
  470. forward Remove_JustPressed(playerid);
  471. public Remove_JustPressed(playerid)
  472. {
  473.     just_pressed_key[playerid] = false;
  474.     return 1;
  475. }
  476. //===================================================================PBPLAYSOUND
  477. stock PB_PlaySound(playerid, soundid)
  478. {
  479.     new Float:pb_xpos, Float:pb_ypos, Float:pb_zpos;
  480.     GetPlayerPos(playerid, pb_xpos, pb_ypos, pb_zpos);
  481.     PlayerPlaySound(playerid, soundid, pb_xpos, pb_ypos, pb_zpos);
  482.     return 1;
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement