Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Dynamic ATM System
- By: VincentDunn
- */
- #include <a_samp>
- #define FILTERSCRIPT
- #include <zcmd>
- #include <streamer>
- //------------------------------------------------------------------------------
- #define MAX_ATMS (100)
- #define MAX_DEPOSIT_AMOUNT (1000000000)
- #define ATM_MODEL (2754) // only change this to ATM models
- /*
- You can change the two defines below
- to what you want new players to spawn with
- */
- #define NOOB_SPAWN_CASH (10000)
- #define NOOB_SPAWN_BANK (10000)
- /*
- Used to define the range the player must
- be in to use/edit ATMS
- */
- #define ATM_RANGE (3.0)
- //------------------------------------------------------------------------------
- forward SaveDynamicATMS();
- forward LoadDynamicATMS();
- forward CreateDynamicATM(atmid);
- forward DestroyDynamicATM(atmid);
- forward SaveBankAccount(playerid);
- forward LoadBankAccount(playerid);
- forward CreateBankAccount(playerid);
- forward moneyTimer();
- //------------------------------------------------------------------------------
- enum iATMS {
- atmTaken,
- Text3D:atmLabelID,
- atmObjectID,
- Float:atmX,
- Float:atmY,
- Float:atmZ,
- Float:atmAngle,
- atmInterior,
- atmVirtualWorld
- }
- enum E_PLAYER_DATA {
- pBalance,
- pEditingID,
- pHandMoney
- }
- new
- DB:ATMS,
- AtmInfo[MAX_ATMS][iATMS],
- PlayerBank[MAX_PLAYERS][E_PLAYER_DATA],
- gATMCount=0,
- Timer;
- //------------------------------------------------------------------------------
- public OnFilterScriptInit()
- {
- ATMS = db_open("ATM_Database.db");
- Timer = SetTimer("moneyTimer", 1000, 1);
- db_query(ATMS, "CREATE TABLE IF NOT EXISTS `ATMS` \
- (`ID`, \
- `Taken`, \
- `X`, \
- `Y`, \
- `Z`, \
- `Angle`, \
- `Interior`, \
- `VirtualWorld`)"
- );
- db_query(ATMS, "CREATE TABLE IF NOT EXISTS `BankAccounts` (`Name`,`Balance`,`Cash`)");
- print("\n----------------------------------------");
- print("Dynamic ATM System by VincentDunn loaded\n");
- LoadDynamicATMS();
- print("----------------------------------------\n");
- return 1;
- }
- public OnFilterScriptExit()
- {
- print("\n------------------------------------------");
- print("Dynamic ATM System by VincentDunn unloaded\n");
- SaveDynamicATMS();
- print("------------------------------------------\n");
- db_close(ATMS);
- KillTimer(Timer);
- return 1;
- }
- main(){}
- //------------------------------------------------------------------------------
- CMD:createatm(playerid, params[])
- {
- if(!IsPlayerAdmin(playerid)) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not authorised to use that command");
- }
- new
- id = getNextAvailableATMID();
- GetPlayerPos(playerid, AtmInfo[id][atmX], AtmInfo[id][atmY], AtmInfo[id][atmZ]);
- GetPlayerFacingAngle(playerid, AtmInfo[id][atmAngle]);
- GetXYInFrontOfPlayer(playerid, AtmInfo[id][atmX], AtmInfo[id][atmY], 2.0);
- AtmInfo[id][atmTaken] = 1;
- AtmInfo[id][atmInterior] = GetPlayerInterior(playerid);
- AtmInfo[id][atmVirtualWorld] = GetPlayerVirtualWorld(playerid);
- AtmInfo[id][atmAngle] += 90;
- SendClientMessage(playerid, 0xFFFF00FF, "You have created an ATM.");
- CreateDynamicATM(id);
- return 1;
- }
- CMD:destroyatm(playerid, params[])
- {
- if(!IsPlayerAdmin(playerid)) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not authorised to use that command");
- }
- new
- id = getClosestATMID(playerid);
- if(id != -1) {
- DestroyDynamicATM(id);
- SendClientMessage(playerid, 0xFFFF00FF, "You have destroyed the nearest ATM.");
- return 1;
- }
- else {
- SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not near an ATM");
- }
- return 1;
- }
- CMD:editatm(playerid, params[])
- {
- if(!IsPlayerAdmin(playerid)) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You are not authorised to use that command");
- }
- if(!gATMCount) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: No ATMs have been created.");
- }
- new
- id = getClosestATMID(playerid);
- if(id != -1) {
- DestroyDynamic3DTextLabel(AtmInfo[id][atmLabelID]);
- PlayerBank[playerid][pEditingID] = id;
- SendClientMessage(playerid, 0xFFFFFFFF, "Use {FFFF00}~k~~PED_SPRINT~{FFFFFF} to look around.");
- EditDynamicObject(playerid, AtmInfo[id][atmObjectID]);
- return 1;
- }
- else {
- SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You must be closer to the ATM you want to edit");
- }
- return 1;
- }
- CMD:adeposit(playerid, params[])
- {
- if(getClosestATMID(playerid) != -1) {
- new
- iAmount = strval(params);
- if(isnull(params) || iAmount <= 0 || iAmount > MAX_DEPOSIT_AMOUNT) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /adeposit [amount]");
- }
- if(iAmount > getPlayerCash(playerid)) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You don't have that much money.");
- }
- new
- szCmd[128];
- setPlayerCash(playerid, getPlayerCash(playerid)-iAmount);
- PlayerBank[playerid][pBalance] += iAmount;
- format(szCmd, sizeof(szCmd), "{FFFF00}You have deposited $%d.", iAmount);
- SendClientMessage(playerid, -1, szCmd);
- return 1;
- }
- else {
- SendClientMessage(playerid, -1, "SERVER: You are not near an ATM.");
- }
- return 1;
- }
- CMD:awithdraw(playerid, params[])
- {
- if(getClosestATMID(playerid) != -1) {
- new
- iAmount = strval(params);
- if(isnull(params) || iAmount <= 0 || iAmount > MAX_DEPOSIT_AMOUNT) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /awithdraw [amount]");
- }
- if(iAmount > PlayerBank[playerid][pBalance]) {
- return SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: You don't have that much money in your bank account.");
- }
- new
- szCmd[128];
- setPlayerCash(playerid, getPlayerCash(playerid)+iAmount);
- PlayerBank[playerid][pBalance] -= iAmount;
- format(szCmd, sizeof(szCmd), "{FFFF00}You have taken $%d out of your bank account. You have $%d left.", iAmount, PlayerBank[playerid][pBalance]);
- SendClientMessage(playerid, -1, szCmd);
- return 1;
- }
- else {
- SendClientMessage(playerid, -1, "SERVER: You are not near an ATM.");
- }
- return 1;
- }
- CMD:abalance(playerid, params[])
- {
- if(getClosestATMID(playerid) != -1) {
- new
- szCmd[128];
- format(szCmd, sizeof(szCmd), "{FFFF00}You have $%d in your bank account.", PlayerBank[playerid][pBalance]);
- SendClientMessage(playerid, -1, szCmd);
- return 1;
- }
- else {
- SendClientMessage(playerid, -1, "SERVER: You are not near an ATM.");
- }
- return 1;
- }
- CMD:atmhelp(playerid, params[])
- {
- SendClientMessage(playerid, -1, "ATM COMMANDS: /adeposit /abalance /awithdraw");
- if(IsPlayerAdmin(playerid)) {
- SendClientMessage(playerid, -1, "ATM COMMANDS: /createatm /editatm /destroyatm");
- }
- return 1;
- }
- //------------------------------------------------------------------------------
- public OnPlayerEditDynamicObject(playerid, objectid, response, Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz)
- {
- new
- id = PlayerBank[playerid][pEditingID];
- if(id != -1) {
- switch (response) {
- case EDIT_RESPONSE_CANCEL: {
- PlayerBank[playerid][pEditingID] = -1;
- return 0;
- }
- case EDIT_RESPONSE_FINAL: {
- AtmInfo[id][atmX] = x;
- AtmInfo[id][atmY] = y;
- AtmInfo[id][atmZ] = z;
- AtmInfo[id][atmAngle] = rz;
- DestroyDynamicObject(objectid);
- CreateDynamicATM(id);
- SendClientMessage(playerid, 0xFFFF00FF, "You have updated this ATM's position.");
- PlayerBank[playerid][pEditingID] = -1;
- return 1;
- }
- }
- }
- return 1;
- }
- public OnPlayerConnect(playerid)
- {
- PlayerBank[playerid][pBalance] = 0;
- PlayerBank[playerid][pEditingID] = -1;
- setPlayerCash(playerid, 0);
- LoadBankAccount(playerid);
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- SaveBankAccount(playerid);
- return 1;
- }
- public OnPlayerSpawn(playerid)
- {
- setPlayerCash(playerid, getPlayerCash(playerid));
- return 1;
- }
- //------------------------------------------------------------------------------
- public moneyTimer()
- {
- for(new i=0; i<MAX_PLAYERS; i++) {
- if(IsPlayerConnected(i)) {
- if(GetPlayerMoney(i) > getPlayerCash(i)) {
- // You can put ban code here
- setPlayerCash(i, getPlayerCash(i));
- }
- }
- }
- return 1;
- }
- public SaveDynamicATMS()
- {
- new
- szQuery[256],
- iFixID,
- time = GetTickCount(),
- ATMcount;
- db_query(ATMS, "DELETE FROM `ATMS`");
- for(new i=0; i<MAX_ATMS; i++) {
- if(AtmInfo[i][atmTaken] == 1) {
- ATMcount++;
- format(szQuery, sizeof(szQuery), "INSERT INTO `ATMS` (`ID`, `Taken`) VALUES (%d, 1)", iFixID);
- db_query(ATMS, szQuery);
- format(szQuery, sizeof(szQuery), "UPDATE `ATMS` SET \
- X = %f, \
- Y = %f, \
- Z = %f, \
- Angle = %f, \
- Interior = %d, \
- VirtualWorld = %d \
- WHERE ID = %d",
- AtmInfo[i][atmX],
- AtmInfo[i][atmY],
- AtmInfo[i][atmZ],
- AtmInfo[i][atmAngle],
- AtmInfo[i][atmInterior],
- AtmInfo[i][atmVirtualWorld],
- iFixID);
- //printf("[debug] %s", szQuery);
- db_query(ATMS, szQuery);
- iFixID++;
- }
- }
- printf("%d Dynamic ATMS saved in %dms.", ATMcount, GetTickCount()-time);
- return 1;
- }
- public LoadDynamicATMS()
- {
- new
- DBResult:dbresult = db_query(ATMS, "SELECT * FROM `ATMS`"),
- rows = db_num_rows(dbresult),
- field[20],
- time = GetTickCount();
- //printf("[debug] %s", szQuery);
- if(!rows) {
- return print("No rows were found in the ATMs table.");
- }
- for(new i=0; i<rows; i++) {
- AtmInfo[i][atmTaken] = 1;
- db_get_field_assoc(dbresult, "X", field, sizeof(field)); AtmInfo[i][atmX] = floatstr(field);
- db_get_field_assoc(dbresult, "Y", field, sizeof(field)); AtmInfo[i][atmY] = floatstr(field);
- db_get_field_assoc(dbresult, "Z", field, sizeof(field)); AtmInfo[i][atmZ] = floatstr(field);
- db_get_field_assoc(dbresult, "Angle", field, sizeof(field)); AtmInfo[i][atmAngle] = floatstr(field);
- db_get_field_assoc(dbresult, "Interior", field, sizeof(field)); AtmInfo[i][atmInterior] = strval(field);
- db_get_field_assoc(dbresult, "VirtualWorld", field, sizeof(field)); AtmInfo[i][atmVirtualWorld] = strval(field);
- CreateDynamicATM(i);
- db_next_row(dbresult);
- }
- db_free_result(dbresult);
- printf("%d Dynamic ATMS loaded in %dms.", rows, GetTickCount()-time);
- return 1;
- }
- public CreateDynamicATM(atmid)
- {
- new
- szLabelText[16];
- format(szLabelText, sizeof(szLabelText), "ATM\nID: %d", atmid);
- gATMCount++;
- 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], _, _);
- 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], _, _);
- return 1;
- }
- public DestroyDynamicATM(atmid)
- {
- DestroyDynamic3DTextLabel(AtmInfo[atmid][atmLabelID]);
- DestroyDynamicObject(AtmInfo[atmid][atmObjectID]);
- AtmInfo[atmid][atmTaken] = 0;
- AtmInfo[atmid][atmX] = 0.0;
- AtmInfo[atmid][atmY] = 0.0;
- AtmInfo[atmid][atmZ] = 0.0;
- AtmInfo[atmid][atmAngle] = 0;
- return 1;
- }
- public LoadBankAccount(playerid)
- {
- new
- szQuery[128],
- DBResult:result,
- rows,
- field[30];
- format(szQuery, sizeof(szQuery), "SELECT Balance, Cash FROM BankAccounts WHERE `Name` = '%s' LIMIT 1", getPlayerNameEx(playerid));
- //printf("[debug] %s", szQuery);
- result = db_query(ATMS, szQuery);
- rows = db_num_rows(result);
- if(rows)
- {
- db_get_field_assoc(result, "Balance", field, sizeof(field)); PlayerBank[playerid][pBalance] = strval(field);
- db_get_field_assoc(result, "Cash", field, sizeof(field)); PlayerBank[playerid][pHandMoney] = strval(field);
- }
- else {
- PlayerBank[playerid][pBalance] = NOOB_SPAWN_CASH;
- PlayerBank[playerid][pHandMoney] = NOOB_SPAWN_BANK;
- CreateBankAccount(playerid);
- }
- db_free_result(result);
- return 1;
- }
- public SaveBankAccount(playerid)
- {
- new
- szQuery[128];
- format(szQuery, sizeof(szQuery), "UPDATE `BankAccounts` SET Balance = %d, Cash = %d WHERE `Name` = '%s'",
- PlayerBank[playerid][pBalance],
- PlayerBank[playerid][pHandMoney],
- getPlayerNameEx(playerid)
- );
- //printf("[debug] %s", szQuery);
- db_query(ATMS, szQuery);
- return 1;
- }
- public CreateBankAccount(playerid)
- {
- new
- szQuery[128];
- format(szQuery, sizeof(szQuery), "INSERT INTO `BankAccounts` (`Name`, `Balance`, `Cash`) VALUES ('%s', %d, %d)",
- getPlayerNameEx(playerid),
- PlayerBank[playerid][pBalance],
- PlayerBank[playerid][pHandMoney]
- );
- //printf("[debug] %s", szQuery);
- db_query(ATMS, szQuery);
- return 1;
- }
- //------------------------------------------------------------------------------
- stock getCoordsInFrontOfPlayer(playerid, &Float:x, &Float:y, Float:Distance)
- {
- new
- Float:r;
- if(!IsPlayerInAnyVehicle(playerid)) {
- GetPlayerFacingAngle(playerid,r);
- }
- else {
- GetVehicleZAngle(GetPlayerVehicleID(playerid),r);
- }
- x += (Distance * floatsin(-r, degrees));
- y += (Distance * floatcos(-r, degrees));
- return 1;
- }
- stock getNextAvailableATMID()
- {
- new i=0;
- while(i != MAX_ATMS) {
- if(AtmInfo[i][atmTaken] == 0) {
- return i;
- }
- i++;
- }
- return -1;
- }
- stock getClosestATMID(playerid)
- {
- new i=0;
- while(i != MAX_ATMS) {
- if(IsPlayerInRangeOfPoint(playerid, ATM_RANGE, AtmInfo[i][atmX], AtmInfo[i][atmY], AtmInfo[i][atmZ]) && AtmInfo[i][atmTaken] == 1) {
- return i;
- }
- i++;
- }
- return -1;
- }
- stock getPlayerNameEx(playerid)
- {
- new
- szName[MAX_PLAYER_NAME];
- GetPlayerName(playerid, szName, sizeof(szName));
- return szName;
- }
- stock setPlayerCash(playerid, amount)
- {
- PlayerBank[playerid][pHandMoney] = amount;
- ResetPlayerMoney(playerid);
- GivePlayerMoney(playerid, PlayerBank[playerid][pHandMoney]);
- return 1;
- }
- stock getPlayerCash(playerid) return PlayerBank[playerid][pHandMoney];
Advertisement
Add Comment
Please, Sign In to add comment