Advertisement
Guest User

Zinglish

a guest
Nov 4th, 2009
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.86 KB | None | 0 0
  1. //
  2. // Lotto FS
  3. // By: Zinglish
  4. // Edit to your liking, no need to give credit btw
  5. // Only if wanted
  6. //
  7.  
  8. #include <a_samp>
  9. #define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  10.  
  11. // Colors
  12. #define COLOR_GREY 0x84848455
  13. #define COLOR_WHITE 0xFFFFFFFF
  14. #define COLOR_RED 0xE21F1FFF
  15. #define COLOR_TURQUOISE 0x84FFFFFF
  16. #define COLOR_YELLOW 0xFDFF3DFF
  17. #define COLOR_GREEN 0x62FF32FF
  18.  
  19. // Functions
  20.  
  21. stock RandomLottoCash()
  22. {
  23.     return random(30) * 100000; // Only a maximum of $3,000,000
  24. }
  25.  
  26. // When the player is given cash, it displays how much he is given
  27. new Text:DrawcashAmount[MAX_PLAYERS];
  28. new GivePlayerCashRemoveTimer[MAX_PLAYERS];
  29. stock GivePlayerCash(playerid, cashAmount)
  30. {
  31.     GivePlayerMoney(playerid, cashAmount);
  32.     new cashAmountString[56];
  33.     format(cashAmountString, sizeof(cashAmountString), "+$%i", cashAmount);
  34.     TextDrawSetString(DrawcashAmount[playerid], cashAmountString);
  35.     KillTimer(GivePlayerCashRemoveTimer[playerid]);
  36.     GivePlayerCashRemoveTimer[playerid] = SetTimer("GivePlayerCashRemove", 5000, 0);
  37. }
  38. stock TakePlayerCash(playerid, cashAmount)
  39. {
  40.     GivePlayerMoney(playerid, -cashAmount);
  41.     new cashAmountString[56];
  42.     format(cashAmountString, sizeof(cashAmountString), "-$%i", cashAmount);
  43.     TextDrawSetString(DrawcashAmount[playerid], cashAmountString);
  44.     KillTimer(GivePlayerCashRemoveTimer[playerid]);
  45.     GivePlayerCashRemoveTimer[playerid] = SetTimerEx("GivePlayerCashRemove", 5000, 0, "i", playerid);
  46. }
  47. forward GivePlayerCashRemove(playerid);
  48. public GivePlayerCashRemove(playerid)
  49. {
  50.     TextDrawSetString(DrawcashAmount[playerid], " ");
  51.     KillTimer(GivePlayerCashRemoveTimer[playerid]);
  52. }
  53.  
  54. stock PlayerName(playerid)
  55. {
  56.     new name[255];
  57.     GetPlayerName(playerid, name, 255);
  58.     return name;
  59. }
  60.  
  61. // Lotto variables
  62. new hasPlayedLotto[MAX_PLAYERS]; // Stores whether player has played lotto or not
  63. new pLottoNumber[MAX_PLAYERS]; // Stores the player's lotto number he has chosen
  64. new lottoWinner; // Stores the player who won
  65. new lottoCashAmount; // Stores the lotto pot amount cash
  66. new lottoCashAmountIncrease;
  67. new chosenLottoNumber[MAX_PLAYERS];
  68. new Text:lottoDrawText; // Text draw
  69. new lottoDestroy;
  70. new Menu:lottoBacking;
  71.  
  72. // Lotto function (runLotto();)
  73. runLotto()
  74. {
  75.     new stringLotto[512], stringClientMessage[256]; // Text draw text
  76.     new lottoNumber = random(50); // Random lotto number
  77.  
  78.     // loop for every player's lotto number entry
  79.     for (new i=0; i<MAX_PLAYERS; i++)
  80.     {
  81.         if (hasPlayedLotto[i] == 1)
  82.         {
  83.             if(chosenLottoNumber[i] == lottoNumber)
  84.             {
  85.                 lottoWinner = i;
  86.             }
  87.             hasPlayedLotto[i] = 0;
  88.             pLottoNumber[i] = 0;
  89.         }
  90.         ShowMenuForPlayer(lottoBacking, i);
  91.     }
  92.  
  93.     if(lottoWinner >= 0) // If there is a winner
  94.     {
  95.         format(stringLotto, sizeof(stringLotto), "~w~Lotto Draw~n~~n~Lotto: ~g~$%i      ~w~Winning number: ~b~%i~n~~n~~w~Winner: ~r~%s", lottoCashAmount, lottoNumber, PlayerName(lottoWinner));
  96.         format(stringClientMessage, sizeof(stringClientMessage), "%s has won $%i from the lotto", PlayerName(lottoWinner), lottoCashAmount);
  97.  
  98.         GivePlayerCash(lottoWinner, lottoCashAmount);
  99.         SendClientMessageToAll(COLOR_GREEN, stringClientMessage);
  100.         lottoCashAmount = RandomLottoCash();
  101.         lottoWinner = -1;
  102.     }
  103.     if(lottoWinner == -1) // If lottoWinner = -1 (No winner)
  104.     {
  105.         format(stringLotto, sizeof(stringLotto), "~w~Lotto Draw~n~~n~Lotto: ~g~$%i      ~w~Winning number: ~b~%i~n~~w~No winners", lottoCashAmount, lottoNumber);
  106.         lottoCashAmount = lottoCashAmount + lottoCashAmountIncrease;
  107.     }
  108.  
  109.     TextDrawSetString(lottoDrawText, stringLotto);
  110.     TextDrawShowForAll(lottoDrawText);
  111.    
  112.     // Timer to make lotto dissapear after 20 seconds
  113.     KillTimer(lottoDestroy);
  114.     lottoDestroy = SetTimer("lottoDrawHide", 20000, 0);
  115. }
  116.  
  117. //------------------------------------------------
  118.  
  119. public OnFilterScriptInit()
  120. {
  121.     print("\n******************************\n");
  122.     print("**Zinglish's lotto FS loaded**\n");
  123.     print("******************************\n");
  124.     return 1;
  125. }
  126.  
  127. public OnGameModeInit()
  128. {
  129.     lottoWinner = -1;
  130.     lottoCashAmount = RandomLottoCash();
  131.    
  132.     // Draw text arrangements and draws
  133.     lottoDrawText = TextDrawCreate(350, 390, " ");
  134.     TextDrawAlignment(lottoDrawText, 2);
  135.     TextDrawUseBox(lottoDrawText, 0);
  136.     TextDrawBoxColor(lottoDrawText, 0x84848455);
  137.     TextDrawSetShadow(lottoDrawText, 0);
  138.     TextDrawSetOutline(lottoDrawText, 1);
  139.     TextDrawFont(lottoDrawText, 1);
  140.     TextDrawLetterSize(lottoDrawText, 0.4, 0.8);
  141.     TextDrawHideForAll(lottoDrawText);
  142.    
  143.     lottoBacking = CreateMenu(" ", 1, 200, 380, 280);
  144.    
  145.     // Timers
  146.     SetTimer("lottoDrawCreate", 1440000, 1);
  147. }
  148.  
  149. public OnPlayerConnect(playerid)
  150. {
  151.     hasPlayedLotto[playerid] = 0;
  152.     pLottoNumber[playerid] = 0;
  153.    
  154.     HideMenuForPlayer(lottoBacking, playerid);
  155. }
  156.  
  157. public OnPlayerCommandText(playerid, cmdtext[])
  158. {
  159.     dcmd(lotto,5,cmdtext);
  160.    
  161.     if(strcmp(cmdtext, "/drawlotto") == 0)
  162.     {
  163.         //if(IsPlayerAdmin(playerid))
  164.         //{
  165.             runLotto();
  166.            
  167.             return 1;
  168.         //}
  169.         //else
  170.         //{
  171.             //return 0;
  172.         //}
  173.     }
  174.     return 0;
  175. }
  176.  
  177. /**************************************************************
  178. ******************** COMMANDS *********************************
  179. ***************************************************************/
  180. // Player chooses lotto number
  181. dcmd_lotto(playerid, params[])
  182. {
  183.     new ValidNumber = 1;
  184.     if(sscanf(params, "i", chosenLottoNumber[playerid]))
  185.     {
  186.         SendClientMessage(playerid, COLOR_RED, "Usage: /lotto [number 1-50]");
  187.         SendClientMessage(playerid, COLOR_RED, "The lotto costs $200");
  188.     }
  189.     else if(chosenLottoNumber[playerid] < 1 || chosenLottoNumber[playerid] > 50)
  190.     {
  191.         SendClientMessage(playerid, COLOR_RED, "Number must be between 1 and 50");
  192.     }
  193.     else if(hasPlayedLotto[playerid] == 0)
  194.     {
  195.         if(GetPlayerMoney(playerid) < 200)
  196.         {
  197.             SendClientMessage(playerid, COLOR_RED, "You do not have the $200 to pay for the lotto ticket");
  198.         }
  199.         if(GetPlayerMoney(playerid) > 200)
  200.         {
  201.             for(new i; i < MAX_PLAYERS; i++)
  202.             {
  203.                 if(pLottoNumber[playerid] == pLottoNumber[i] && pLottoNumber[playerid] != pLottoNumber[playerid])
  204.                 {
  205.                     ValidNumber = 0;
  206.                 }
  207.             }
  208.             if(ValidNumber == 1)
  209.             {
  210.                 new string[256];
  211.                 format(string, sizeof(string), "You have played the lotto with number %i on you ticket", chosenLottoNumber[playerid]);
  212.                 SendClientMessage(playerid, COLOR_YELLOW, string);
  213.                 TakePlayerCash(playerid, 200);
  214.                 hasPlayedLotto[playerid] = 1;
  215.                 pLottoNumber[playerid] = chosenLottoNumber[playerid];
  216.             }
  217.             if(ValidNumber == 0)
  218.             {
  219.                 SendClientMessage(playerid, COLOR_YELLOW, "Number has already been chosen");
  220.             }
  221.         }
  222.     }
  223.     else if(hasPlayedLotto[playerid] == 1)
  224.     {
  225.         SendClientMessage(playerid, COLOR_YELLOW, "You have already played the lotto");
  226.     }
  227.  
  228.     return 1;
  229. }
  230.  
  231.  
  232.  
  233. // Draw lotto from function
  234. forward lottoDrawCreate(playerid);
  235. public lottoDrawCreate(playerid)
  236. {
  237.     runLotto();
  238. }
  239. // Remove the lotto draw and reset timers
  240. forward lottoDrawHide(playerid);
  241. public lottoDrawHide(playerid)
  242. {
  243.     TextDrawHideForAll(lottoDrawText);
  244.     for(new i; i < MAX_PLAYERS; i++)
  245.     {
  246.         HideMenuForPlayer(lottoBacking, i);
  247.     }
  248.     KillTimer(lottoDestroy);
  249. }
  250.  
  251.  
  252.  
  253. // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  254. // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  255. // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  256. // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  257. // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  258. // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  259. // -------------------------------------------------------------------------------------------------------------------------------------------------------------
  260.  
  261. stock sscanf(string[], format[], {Float,_}:...)
  262. {
  263.     #if defined isnull
  264.         if (isnull(string))
  265.     #else
  266.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  267.     #endif
  268.         {
  269.             return format[0];
  270.         }
  271.     #pragma tabsize 4
  272.     new
  273.         formatPos = 0,
  274.         stringPos = 0,
  275.         paramPos = 2,
  276.         paramCount = numargs(),
  277.         delim = ' ';
  278.     while (string[stringPos] && string[stringPos] <= ' ')
  279.     {
  280.         stringPos++;
  281.     }
  282.     while (paramPos < paramCount && string[stringPos])
  283.     {
  284.         switch (format[formatPos++])
  285.         {
  286.             case '\0':
  287.             {
  288.                 return 0;
  289.             }
  290.             case 'i', 'd':
  291.             {
  292.                 new
  293.                     neg = 1,
  294.                     num = 0,
  295.                     ch = string[stringPos];
  296.                 if (ch == '-')
  297.                 {
  298.                     neg = -1;
  299.                     ch = string[++stringPos];
  300.                 }
  301.                 do
  302.                 {
  303.                     stringPos++;
  304.                     if ('0' <= ch <= '9')
  305.                     {
  306.                         num = (num * 10) + (ch - '0');
  307.                     }
  308.                     else
  309.                     {
  310.                         return -1;
  311.                     }
  312.                 }
  313.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  314.                 setarg(paramPos, 0, num * neg);
  315.             }
  316.             case 'h', 'x':
  317.             {
  318.                 new
  319.                     ch,
  320.                     num = 0;
  321.                 while ((ch = string[stringPos]) > ' ' && ch != delim)
  322.                 {
  323.                     switch (ch)
  324.                     {
  325.                         case 'x', 'X':
  326.                         {
  327.                             num = 0;
  328.                             continue;
  329.                         }
  330.                         case '0' .. '9':
  331.                         {
  332.                             num = (num << 4) | (ch - '0');
  333.                         }
  334.                         case 'a' .. 'f':
  335.                         {
  336.                             num = (num << 4) | (ch - ('a' - 10));
  337.                         }
  338.                         case 'A' .. 'F':
  339.                         {
  340.                             num = (num << 4) | (ch - ('A' - 10));
  341.                         }
  342.                         default:
  343.                         {
  344.                             return -1;
  345.                         }
  346.                     }
  347.                 }
  348.                 setarg(paramPos, 0, num);
  349.             }
  350.             case 'c':
  351.             {
  352.                 setarg(paramPos, 0, string[stringPos++]);
  353.             }
  354.             case 'f':
  355.             {
  356.                 setarg(paramPos, 0, _:floatstr(string[stringPos]));
  357.             }
  358.             case 'p':
  359.             {
  360.                 delim = format[formatPos++];
  361.                 continue;
  362.             }
  363.             case '\'':
  364.             {
  365.                 new
  366.                     end = formatPos - 1,
  367.                     ch;
  368.                 while ((ch = format[++end]) && ch != '\'') {}
  369.                 if (!ch)
  370.                 {
  371.                     return -1;
  372.                 }
  373.                 format[end] = '\0';
  374.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  375.                 {
  376.                     if (format[end + 1])
  377.                     {
  378.                         return -1;
  379.                     }
  380.                     return 0;
  381.                 }
  382.                 format[end] = '\'';
  383.                 stringPos = ch + (end - formatPos);
  384.                 formatPos = end + 1;
  385.             }
  386.             case 'u':
  387.             {
  388.                 new
  389.                     end = stringPos - 1,
  390.                     id = 0,
  391.                     bool:num = true,
  392.                     ch;
  393.                 while ((ch = string[++end]) && ch != delim)
  394.                 {
  395.                     if (num)
  396.                     {
  397.                         if ('0' <= ch <= '9')
  398.                         {
  399.                             id = (id * 10) + (ch - '0');
  400.                         }
  401.                         else
  402.                         {
  403.                             num = false;
  404.                         }
  405.                     }
  406.                 }
  407.                 if (num && IsPlayerConnected(id))
  408.                 {
  409.                     setarg(paramPos, 0, id);
  410.                 }
  411.                 else
  412.                 {
  413.                     #if !defined foreach
  414.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  415.                         #define __SSCANF_FOREACH__
  416.                     #endif
  417.                     string[end] = '\0';
  418.                     num = false;
  419.                     new
  420.                         name[MAX_PLAYER_NAME];
  421.                     id = end - stringPos;
  422.                     foreach (Player, playerid)
  423.                     {
  424.                         GetPlayerName(playerid, name, sizeof (name));
  425.                         if (!strcmp(name, string[stringPos], true, id))
  426.                         {
  427.                             setarg(paramPos, 0, playerid);
  428.                             num = true;
  429.                             break;
  430.                         }
  431.                     }
  432.                     if (!num)
  433.                     {
  434.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  435.                     }
  436.                     string[end] = ch;
  437.                     #if defined __SSCANF_FOREACH__
  438.                         #undef foreach
  439.                         #undef __SSCANF_FOREACH__
  440.                     #endif
  441.                 }
  442.                 stringPos = end;
  443.             }
  444.             case 's', 'z':
  445.             {
  446.                 new
  447.                     i = 0,
  448.                     ch;
  449.                 if (format[formatPos])
  450.                 {
  451.                     while ((ch = string[stringPos++]) && ch != delim)
  452.                     {
  453.                         setarg(paramPos, i++, ch);
  454.                     }
  455.                     if (!i)
  456.                     {
  457.                         return -1;
  458.                     }
  459.                 }
  460.                 else
  461.                 {
  462.                     while ((ch = string[stringPos++]))
  463.                     {
  464.                         setarg(paramPos, i++, ch);
  465.                     }
  466.                 }
  467.                 stringPos--;
  468.                 setarg(paramPos, i, '\0');
  469.             }
  470.             default:
  471.             {
  472.                 continue;
  473.             }
  474.         }
  475.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  476.         {
  477.             stringPos++;
  478.         }
  479.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  480.         {
  481.             stringPos++;
  482.         }
  483.         paramPos++;
  484.     }
  485.     do
  486.     {
  487.         if ((delim = format[formatPos++]) > ' ')
  488.         {
  489.             if (delim == '\'')
  490.             {
  491.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  492.             }
  493.             else if (delim != 'z')
  494.             {
  495.                 return delim;
  496.             }
  497.         }
  498.     }
  499.     while (delim > ' ');
  500.     return 0;
  501. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement