Guest User

Dynamic ATM Filterscript/Money Saving

a guest
Jul 25th, 2012
1,702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 14.16 KB | None | 0 0
  1. /*
  2.  
  3.     Dynamic ATM System
  4.    
  5.     By: VincentDunn
  6.  
  7. */
  8.  
  9. #include        <a_samp>
  10.  
  11. #define         FILTERSCRIPT
  12.  
  13. #include        <zcmd>
  14. #include        <streamer>
  15.  
  16. //------------------------------------------------------------------------------
  17.  
  18. #define         MAX_ATMS                (100)
  19. #define         MAX_DEPOSIT_AMOUNT      (1000000000)
  20.  
  21. #define         ATM_MODEL               (2754) // only change this to ATM models
  22.  
  23. /*
  24.     You can change the two defines below
  25.     to what you want new players to spawn with
  26. */
  27.  
  28. #define         NOOB_SPAWN_CASH         (10000)
  29. #define         NOOB_SPAWN_BANK         (10000)
  30.  
  31. /*
  32.     Used to define the range the player must
  33.     be in to use/edit ATMS
  34. */
  35.  
  36. #define         ATM_RANGE       (3.0)
  37.  
  38. //------------------------------------------------------------------------------
  39.  
  40. forward SaveDynamicATMS();
  41. forward LoadDynamicATMS();
  42. forward CreateDynamicATM(atmid);
  43. forward DestroyDynamicATM(atmid);
  44. forward SaveBankAccount(playerid);
  45. forward LoadBankAccount(playerid);
  46. forward CreateBankAccount(playerid);
  47. forward moneyTimer();
  48.  
  49. //------------------------------------------------------------------------------
  50.  
  51. enum iATMS {
  52.     atmTaken,
  53.     Text3D:atmLabelID,
  54.     atmObjectID,
  55.     Float:atmX,
  56.     Float:atmY,
  57.     Float:atmZ,
  58.     Float:atmAngle,
  59.     atmInterior,
  60.     atmVirtualWorld
  61. }
  62.  
  63. enum E_PLAYER_DATA {
  64.     pBalance,
  65.     pEditingID,
  66.     pHandMoney
  67. }
  68.  
  69. new
  70.     DB:ATMS,
  71.     AtmInfo[MAX_ATMS][iATMS],
  72.     PlayerBank[MAX_PLAYERS][E_PLAYER_DATA],
  73.     gATMCount=0,
  74.     Timer;
  75.  
  76.    
  77.  
  78. //------------------------------------------------------------------------------
  79.  
  80. public OnFilterScriptInit()
  81. {
  82.     ATMS = db_open("ATM_Database.db");
  83.    
  84.     Timer = SetTimer("moneyTimer", 1000, 1);
  85.  
  86.     db_query(ATMS, "CREATE TABLE IF NOT EXISTS `ATMS` \
  87.         (`ID`, \
  88.         `Taken`, \
  89.         `X`, \
  90.         `Y`, \
  91.         `Z`, \
  92.         `Angle`, \
  93.         `Interior`, \
  94.         `VirtualWorld`)"
  95.     );
  96.                            
  97.     db_query(ATMS, "CREATE TABLE IF NOT EXISTS `BankAccounts` (`Name`,`Balance`,`Cash`)");
  98.                    
  99.     print("\n----------------------------------------");
  100.     print("Dynamic ATM System by VincentDunn loaded\n");
  101.     LoadDynamicATMS();
  102.     print("----------------------------------------\n");
  103.    
  104.     return 1;
  105. }
  106.  
  107. public OnFilterScriptExit()
  108. {
  109.     print("\n------------------------------------------");
  110.     print("Dynamic ATM System by VincentDunn unloaded\n");
  111.     SaveDynamicATMS();
  112.     print("------------------------------------------\n");
  113.    
  114.     db_close(ATMS);
  115.     KillTimer(Timer);
  116.     return 1;
  117. }
  118.  
  119. main(){}
  120.  
  121.  
  122.  
  123. //------------------------------------------------------------------------------
  124.  
  125. CMD:createatm(playerid, params[])
  126. {
  127.     if(!IsPlayerAdmin(playerid)) {
  128.         return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not authorised to use that command");
  129.     }
  130.  
  131.     new
  132.         id = getNextAvailableATMID();
  133.  
  134.     GetPlayerPos(playerid, AtmInfo[id][atmX], AtmInfo[id][atmY], AtmInfo[id][atmZ]);
  135.     GetPlayerFacingAngle(playerid, AtmInfo[id][atmAngle]);
  136.     GetXYInFrontOfPlayer(playerid, AtmInfo[id][atmX], AtmInfo[id][atmY], 2.0);
  137.    
  138.     AtmInfo[id][atmTaken] = 1;
  139.     AtmInfo[id][atmInterior] = GetPlayerInterior(playerid);
  140.     AtmInfo[id][atmVirtualWorld] = GetPlayerVirtualWorld(playerid);
  141.     AtmInfo[id][atmAngle] += 90;
  142.    
  143.     SendClientMessage(playerid, 0xFFFF00FF, "You have created an ATM.");
  144.    
  145.     CreateDynamicATM(id);
  146.     return 1;
  147. }
  148.  
  149. CMD:destroyatm(playerid, params[])
  150. {
  151.     if(!IsPlayerAdmin(playerid)) {
  152.         return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not authorised to use that command");
  153.     }
  154.    
  155.     new
  156.         id = getClosestATMID(playerid);
  157.  
  158.     if(id != -1) {
  159.         DestroyDynamicATM(id);
  160.         SendClientMessage(playerid, 0xFFFF00FF, "You have destroyed the nearest ATM.");
  161.         return 1;
  162.     }
  163.    
  164.     else {
  165.         SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not near an ATM");
  166.     }
  167.     return 1;
  168. }
  169.  
  170. CMD:editatm(playerid, params[])
  171. {
  172.     if(!IsPlayerAdmin(playerid)) {
  173.         return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not authorised to use that command");
  174.     }
  175.    
  176.     if(!gATMCount) {
  177.         return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: No ATMs have been created.");
  178.     }
  179.    
  180.     new
  181.         id = getClosestATMID(playerid);
  182.  
  183.     if(id != -1) {
  184.         DestroyDynamic3DTextLabel(AtmInfo[id][atmLabelID]);
  185.         PlayerBank[playerid][pEditingID] = id;
  186.         SendClientMessage(playerid, 0xFFFFFFFF, "Use {FFFF00}~k~~PED_SPRINT~{FFFFFF} to look around.");
  187.        
  188.         EditDynamicObject(playerid, AtmInfo[id][atmObjectID]);
  189.         return 1;
  190.     }
  191.    
  192.     else {
  193.         SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You must be closer to the ATM you want to edit");
  194.     }
  195.     return 1;
  196. }
  197.  
  198. CMD:adeposit(playerid, params[])
  199. {
  200.     if(getClosestATMID(playerid) != -1) {
  201.         new
  202.             iAmount = strval(params);
  203.            
  204.         if(isnull(params) || iAmount <= 0 || iAmount > MAX_DEPOSIT_AMOUNT) {
  205.             return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /adeposit [amount]");
  206.         }
  207.  
  208.         if(iAmount > getPlayerCash(playerid)) {
  209.             return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You don't have that much money.");
  210.         }
  211.        
  212.         new
  213.             szCmd[128];
  214.            
  215.         setPlayerCash(playerid, getPlayerCash(playerid)-iAmount);
  216.         PlayerBank[playerid][pBalance] += iAmount;
  217.        
  218.         format(szCmd, sizeof(szCmd), "{FFFF00}You have deposited $%d.", iAmount);
  219.         SendClientMessage(playerid, -1, szCmd);
  220.         return 1;
  221.     }
  222.  
  223.     else {
  224.         SendClientMessage(playerid, -1, "SERVER: You are not near an ATM.");
  225.     }
  226.     return 1;
  227. }
  228.  
  229. CMD:awithdraw(playerid, params[])
  230. {
  231.     if(getClosestATMID(playerid) != -1) {
  232.         new
  233.             iAmount = strval(params);
  234.  
  235.         if(isnull(params) || iAmount <= 0 || iAmount > MAX_DEPOSIT_AMOUNT) {
  236.             return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /awithdraw [amount]");
  237.         }
  238.  
  239.         if(iAmount > PlayerBank[playerid][pBalance]) {
  240.             return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You don't have that much money in your bank account.");
  241.         }
  242.  
  243.         new
  244.             szCmd[128];
  245.  
  246.         setPlayerCash(playerid, getPlayerCash(playerid)+iAmount);
  247.         PlayerBank[playerid][pBalance] -= iAmount;
  248.  
  249.         format(szCmd, sizeof(szCmd), "{FFFF00}You have taken $%d out of your bank account. You have $%d left.", iAmount, PlayerBank[playerid][pBalance]);
  250.         SendClientMessage(playerid, -1, szCmd);
  251.         return 1;
  252.     }
  253.  
  254.     else {
  255.         SendClientMessage(playerid, -1, "SERVER: You are not near an ATM.");
  256.     }
  257.     return 1;
  258. }
  259.  
  260. CMD:abalance(playerid, params[])
  261. {
  262.     if(getClosestATMID(playerid) != -1) {
  263.         new
  264.             szCmd[128];
  265.  
  266.         format(szCmd, sizeof(szCmd), "{FFFF00}You have $%d in your bank account.", PlayerBank[playerid][pBalance]);
  267.         SendClientMessage(playerid, -1, szCmd);
  268.         return 1;
  269.     }
  270.  
  271.     else {
  272.         SendClientMessage(playerid, -1, "SERVER: You are not near an ATM.");
  273.     }
  274.     return 1;
  275. }
  276.  
  277. CMD:atmhelp(playerid, params[])
  278. {
  279.     SendClientMessage(playerid, -1, "ATM COMMANDS: /adeposit /abalance /awithdraw");
  280.  
  281.     if(IsPlayerAdmin(playerid)) {
  282.         SendClientMessage(playerid, -1, "ATM COMMANDS: /createatm /editatm /destroyatm");
  283.     }
  284.    
  285.     return 1;
  286. }
  287.  
  288. //------------------------------------------------------------------------------
  289.  
  290. public OnPlayerEditDynamicObject(playerid, objectid, response, Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz)
  291. {
  292.     new
  293.         id = PlayerBank[playerid][pEditingID];
  294.  
  295.     if(id != -1) {
  296.         switch (response) {
  297.             case EDIT_RESPONSE_CANCEL: {
  298.                 PlayerBank[playerid][pEditingID] = -1;
  299.                 return 0;
  300.             }
  301.             case EDIT_RESPONSE_FINAL: {
  302.                 AtmInfo[id][atmX] = x;
  303.                 AtmInfo[id][atmY] = y;
  304.                 AtmInfo[id][atmZ] = z;
  305.                 AtmInfo[id][atmAngle] = rz;
  306.  
  307.                 DestroyDynamicObject(objectid);
  308.                 CreateDynamicATM(id);
  309.                 SendClientMessage(playerid, 0xFFFF00FF, "You have updated this ATM's position.");
  310.  
  311.                 PlayerBank[playerid][pEditingID] = -1;
  312.                 return 1;
  313.             }
  314.         }
  315.     }
  316.    
  317.     return 1;
  318. }
  319.  
  320. public OnPlayerConnect(playerid)
  321. {
  322.     PlayerBank[playerid][pBalance] = 0;
  323.     PlayerBank[playerid][pEditingID] = -1;
  324.     setPlayerCash(playerid, 0);
  325.    
  326.     LoadBankAccount(playerid);
  327.     return 1;
  328. }
  329.  
  330. public OnPlayerDisconnect(playerid, reason)
  331. {
  332.     SaveBankAccount(playerid);
  333.     return 1;
  334. }
  335.  
  336. public OnPlayerSpawn(playerid)
  337. {
  338.     setPlayerCash(playerid, getPlayerCash(playerid));
  339.     return 1;
  340. }
  341.  
  342. //------------------------------------------------------------------------------
  343.  
  344. public moneyTimer()
  345. {
  346.     for(new i=0; i<MAX_PLAYERS; i++) {
  347.         if(IsPlayerConnected(i)) {
  348.             if(GetPlayerMoney(i) > getPlayerCash(i)) {
  349.                 // You can put ban code here
  350.                 setPlayerCash(i, getPlayerCash(i));
  351.             }
  352.         }
  353.     }
  354.     return 1;
  355. }
  356.  
  357. public SaveDynamicATMS()
  358. {
  359.     new
  360.         szQuery[256],
  361.         iFixID,
  362.         time = GetTickCount(),
  363.         ATMcount;
  364.  
  365.     db_query(ATMS, "DELETE FROM `ATMS`");
  366.    
  367.     for(new i=0; i<MAX_ATMS; i++) {
  368.         if(AtmInfo[i][atmTaken] == 1) {
  369.             ATMcount++;
  370.             format(szQuery, sizeof(szQuery), "INSERT INTO `ATMS` (`ID`, `Taken`) VALUES (%d, 1)", iFixID);
  371.             db_query(ATMS, szQuery);
  372.            
  373.             format(szQuery, sizeof(szQuery), "UPDATE `ATMS` SET \
  374.             X = %f, \
  375.             Y = %f, \
  376.             Z = %f, \
  377.             Angle = %f, \
  378.             Interior = %d, \
  379.             VirtualWorld = %d \
  380.             WHERE ID = %d",
  381.             AtmInfo[i][atmX],
  382.             AtmInfo[i][atmY],
  383.             AtmInfo[i][atmZ],
  384.             AtmInfo[i][atmAngle],
  385.             AtmInfo[i][atmInterior],
  386.             AtmInfo[i][atmVirtualWorld],
  387.             iFixID);
  388.            
  389.             //printf("[debug] %s", szQuery);
  390.             db_query(ATMS, szQuery);
  391.             iFixID++;
  392.         }
  393.     }
  394.     printf("%d Dynamic ATMS saved in %dms.", ATMcount, GetTickCount()-time);
  395.     return 1;
  396. }
  397.  
  398. public LoadDynamicATMS()
  399. {
  400.     new
  401.         DBResult:dbresult = db_query(ATMS, "SELECT * FROM `ATMS`"),
  402.         rows = db_num_rows(dbresult),
  403.         field[20],
  404.         time = GetTickCount();
  405.  
  406.     //printf("[debug] %s", szQuery);
  407.  
  408.     if(!rows) {
  409.         return print("No rows were found in the ATMs table.");
  410.     }
  411.    
  412.     for(new i=0; i<rows; i++) {
  413.         AtmInfo[i][atmTaken] = 1;
  414.         db_get_field_assoc(dbresult, "X", field, sizeof(field));                AtmInfo[i][atmX] = floatstr(field);
  415.         db_get_field_assoc(dbresult, "Y", field, sizeof(field));                AtmInfo[i][atmY] = floatstr(field);
  416.         db_get_field_assoc(dbresult, "Z", field, sizeof(field));                AtmInfo[i][atmZ] = floatstr(field);
  417.         db_get_field_assoc(dbresult, "Angle", field, sizeof(field));            AtmInfo[i][atmAngle] = floatstr(field);
  418.         db_get_field_assoc(dbresult, "Interior", field, sizeof(field));         AtmInfo[i][atmInterior] = strval(field);
  419.         db_get_field_assoc(dbresult, "VirtualWorld", field, sizeof(field));     AtmInfo[i][atmVirtualWorld] = strval(field);
  420.         CreateDynamicATM(i);
  421.         db_next_row(dbresult);
  422.     }
  423.     db_free_result(dbresult);
  424.    
  425.     printf("%d Dynamic ATMS loaded in %dms.", rows, GetTickCount()-time);
  426.     return 1;
  427. }
  428.  
  429. public CreateDynamicATM(atmid)
  430. {
  431.     new
  432.         szLabelText[16];
  433.        
  434.     format(szLabelText, sizeof(szLabelText), "ATM\nID: %d", atmid);
  435.  
  436.     gATMCount++;
  437.     AtmInfo[atmid][atmLabelID] = CreateDynamic3DTextLabel(szLabelText, 0x1D9F00AA, AtmInfo[atmid][atmX], AtmInfo[atmid][atmY], AtmInfo[atmid][atmZ]+1.5, 5.0, _, _, _, AtmInfo[atmid][atmVirtualWorld], AtmInfo[atmid][atmInterior], _, _);
  438.     AtmInfo[atmid][atmObjectID] = CreateDynamicObject(ATM_MODEL, AtmInfo[atmid][atmX], AtmInfo[atmid][atmY], AtmInfo[atmid][atmZ], 0.0, 0.0, AtmInfo[atmid][atmAngle], AtmInfo[atmid][atmVirtualWorld], AtmInfo[atmid][atmInterior], _, _);
  439.     return 1;
  440. }
  441.  
  442. public DestroyDynamicATM(atmid)
  443. {
  444.     DestroyDynamic3DTextLabel(AtmInfo[atmid][atmLabelID]);
  445.     DestroyDynamicObject(AtmInfo[atmid][atmObjectID]);
  446.    
  447.     AtmInfo[atmid][atmTaken] = 0;
  448.     AtmInfo[atmid][atmX] = 0.0;
  449.     AtmInfo[atmid][atmY] = 0.0;
  450.     AtmInfo[atmid][atmZ] = 0.0;
  451.     AtmInfo[atmid][atmAngle] = 0;
  452.     return 1;
  453. }
  454.  
  455. public LoadBankAccount(playerid)
  456. {
  457.     new
  458.         szQuery[128],
  459.         DBResult:result,
  460.         rows,
  461.         field[30];
  462.  
  463.     format(szQuery, sizeof(szQuery), "SELECT Balance, Cash FROM BankAccounts WHERE `Name` = '%s' LIMIT 1", getPlayerNameEx(playerid));
  464.    
  465.     //printf("[debug] %s", szQuery);
  466.  
  467.     result = db_query(ATMS, szQuery);
  468.     rows = db_num_rows(result);
  469.  
  470.     if(rows)
  471.     {
  472.         db_get_field_assoc(result, "Balance", field, sizeof(field));            PlayerBank[playerid][pBalance] = strval(field);
  473.         db_get_field_assoc(result, "Cash", field, sizeof(field));               PlayerBank[playerid][pHandMoney] = strval(field);
  474.     }
  475.  
  476.     else {
  477.         PlayerBank[playerid][pBalance] = NOOB_SPAWN_CASH;
  478.         PlayerBank[playerid][pHandMoney] = NOOB_SPAWN_BANK;
  479.    
  480.         CreateBankAccount(playerid);
  481.     }
  482.     db_free_result(result);
  483.     return 1;
  484. }
  485.  
  486. public SaveBankAccount(playerid)
  487. {
  488.     new
  489.         szQuery[128];
  490.  
  491.     format(szQuery, sizeof(szQuery), "UPDATE `BankAccounts` SET Balance = %d, Cash = %d WHERE `Name` = '%s'",
  492.     PlayerBank[playerid][pBalance],
  493.     PlayerBank[playerid][pHandMoney],
  494.     getPlayerNameEx(playerid)
  495.  
  496.     );
  497.    
  498.     //printf("[debug] %s", szQuery);
  499.    
  500.     db_query(ATMS, szQuery);
  501.     return 1;
  502. }
  503.  
  504. public CreateBankAccount(playerid)
  505. {
  506.     new
  507.         szQuery[128];
  508.  
  509.     format(szQuery, sizeof(szQuery), "INSERT INTO `BankAccounts` (`Name`, `Balance`, `Cash`) VALUES ('%s', %d, %d)",
  510.    
  511.     getPlayerNameEx(playerid),
  512.     PlayerBank[playerid][pBalance],
  513.     PlayerBank[playerid][pHandMoney]
  514.  
  515.     );
  516.    
  517.     //printf("[debug] %s", szQuery);
  518.    
  519.     db_query(ATMS, szQuery);
  520.     return 1;
  521. }
  522.  
  523. //------------------------------------------------------------------------------
  524.  
  525. stock getCoordsInFrontOfPlayer(playerid, &Float:x, &Float:y, Float:Distance)
  526. {
  527.     new
  528.         Float:r;
  529.     if(!IsPlayerInAnyVehicle(playerid)) {
  530.         GetPlayerFacingAngle(playerid,r);
  531.     }
  532.     else {
  533.         GetVehicleZAngle(GetPlayerVehicleID(playerid),r);
  534.     }
  535.     x += (Distance * floatsin(-r, degrees));
  536.     y += (Distance * floatcos(-r, degrees));
  537.     return 1;
  538. }
  539.  
  540. stock getNextAvailableATMID()
  541. {
  542.     new i=0;
  543.     while(i != MAX_ATMS) {
  544.         if(AtmInfo[i][atmTaken] == 0) {
  545.             return i;
  546.         }
  547.         i++;
  548.     }
  549.     return -1;
  550. }
  551.        
  552. stock getClosestATMID(playerid)
  553. {
  554.     new i=0;
  555.     while(i != MAX_ATMS) {
  556.         if(IsPlayerInRangeOfPoint(playerid, ATM_RANGE, AtmInfo[i][atmX], AtmInfo[i][atmY], AtmInfo[i][atmZ]) && AtmInfo[i][atmTaken] == 1) {
  557.             return i;
  558.         }
  559.         i++;
  560.     }
  561.     return -1;
  562. }
  563.  
  564. stock getPlayerNameEx(playerid)
  565. {
  566.     new
  567.         szName[MAX_PLAYER_NAME];
  568.     GetPlayerName(playerid, szName, sizeof(szName));
  569.     return szName;
  570. }
  571.  
  572. stock setPlayerCash(playerid, amount)
  573. {
  574.     PlayerBank[playerid][pHandMoney] = amount;
  575.     ResetPlayerMoney(playerid);
  576.     GivePlayerMoney(playerid, PlayerBank[playerid][pHandMoney]);
  577.     return 1;
  578. }
  579.  
  580. stock getPlayerCash(playerid) return PlayerBank[playerid][pHandMoney];
Advertisement
Add Comment
Please, Sign In to add comment