Houses are glitching
#1

When you buy a house, the gametext still says not owned and sometimes it doesn't even appear.
Sometimes I can't even sell the house I bought and I can enter from half across the city, like if the function checking if player is in range doesnt work.

Can anyone help me, i'm new to scripting :S
pawn Код:
/*
*   @title C-House
*   @description - Build in the script /w MySQL loading.
*   @author Carlton
*
*   - This script is downloadable at the SA-MP forums.
*   - Selling this script or re-releasing it without my permission is not allowed
*
*   <-- Install guide -->
*   - Insert your D.B. info into the defines.
*   - Click compile.
*
*   <-- House adding -->
*   - Use the AddServerHouse function.
*
*   <-- Functions -->
*   - AddServerHouse(id, Float:X, Float:Y, Float:Z, Interior, Float:ExitX, Float:ExitY, Float:ExitZ, Price);
*
    + id - The house ID, don't duplicate house ids.
*   + Float:X - The X coordinate. >
*   + Float:Y - The Y coordinate. >> Entrance
*   + Float:Z - The Z coordinate. >
*   + Interior - The interior id for the house.
*   + Float:ExitX - The X coordinate. >
*   + Float:ExitY - The Y coordinate. >> Exit location
*   + Float:ExitZ - The Z coordinate. >
*   + Price - The price of the house.
*
*   - SaveHouse(houseid)
*   + houseid - The house id you wan't save.

*   <-- Support -- >
*   Enjoy the house system, post any questions in the topic.
*/


#include <a_samp>
#include <a_mysql>

#define DB_HOST "127.0.0.1"
#define DB_USER "root"
#define DB_PASSWORD ""
#define DB_NAME "database"
#define DB_TABLE_NAME "Houses"

#define TOTAL_HOUSES 2000
#define function%0(%1) forward%0(%1); public%0(%1)

#define TRUE 1
#define FALSE 0

new sqlconnection;

enum hdata {
    Float: EntrancePos[3],
    hInterior,
    Float: ExitPos[3],
    hPrice,
    Owner[MAX_PLAYER_NAME],
    HouseStructure[2],
    Text3D:HouseStructure2,
    Locked
}
new HouseData[TOTAL_HOUSES][hdata], globalquery[1024];

function IsPlayerInRangeOfHouse(playerid) {
    for(new i = 0; i < TOTAL_HOUSES; i ++ ) {
        if(IsPlayerInRangeOfPoint(playerid, 5.0, HouseData[i][EntrancePos][0], HouseData[i][EntrancePos][1],  HouseData[i][EntrancePos][2])) return i;
    }
    return 0;
}

function AddServerHouse(id, Float:EntX, Float:EntY, Float:EntZ, Interior, Float:ExitX, Float:ExitY,Float:ExitZ, Price) {
    if(id >= TOTAL_HOUSES) return 0;
    format(globalquery, sizeof(globalquery), "SELECT * FROM "DB_TABLE_NAME" WHERE HouseID = %d", id);
    mysql_query(globalquery,-1,-1,sqlconnection);
    mysql_store_result(sqlconnection);
    if(mysql_num_rows(sqlconnection) > 0) return 0;
    format(globalquery, sizeof(globalquery), "INSERT INTO "DB_TABLE_NAME" (HouseID, X, Y, Z, Interior, ExitX, ExitY, ExitZ, Price, Owner) VALUES(%d,'%f','%f','%f',%d,'%f','%f','%f', %d,'Empty House')",
    id, EntX, EntY, EntZ, Interior, ExitX, ExitY, ExitZ, Price);
    mysql_query(globalquery, -1,-1, sqlconnection);
    printf("House id: %d added to the DB.", id);
    mysql_free_result();
    return 1;
}

function SaveHouse(houseid) {
    format(globalquery, sizeof(globalquery), "UPDATE "DB_TABLE_NAME" SET Owner = '%s', Locked = %d, Price = %d",
    HouseData[houseid][Owner], HouseData[houseid][Locked],  HouseData[houseid][hPrice]);
    mysql_query(globalquery, -1, -1,sqlconnection);
}

function LoadServerHouses() {
    mysql_query("SELECT * FROM "DB_TABLE_NAME"", 1, -1, sqlconnection);
}

public OnFilterScriptInit() {
    sqlconnection = mysql_connect(DB_HOST, DB_USER, DB_NAME, DB_PASSWORD);
    mysql_debug(1);
    AddServerHouse(1, -2641.0000,935.5977,71.9531, 9, 315.856170,1024.496459,1949.797363, 9000);
    AddServerHouse(2, -2660.2253,876.9733,79.7738, 9, 315.856170,1024.496459,1949.797363, 9000);
    LoadServerHouses();
}

public OnQueryFinish( query[], resultid, extraid, connectionHandle ) {
    if(resultid == 1) {
        mysql_store_result(sqlconnection);
        new resultline[128], data[11][128];
        while(mysql_fetch_row_format(resultline,"|"))
        {
            if(mysql_num_rows(sqlconnection) == 0) continue;
            new hid = strval(data[0]);
            split(resultline, data, '|');
            printf("Loading House id %d", hid);
            HouseData[hid][EntrancePos][0] = floatstr(data[1]);
            HouseData[hid][EntrancePos][1] = floatstr(data[2]);
            HouseData[hid][EntrancePos][2] = floatstr(data[3]);
            HouseData[hid][hInterior] = strval(data[4]);
            HouseData[hid][ExitPos][0] = floatstr(data[5]);
            HouseData[hid][ExitPos][1] = floatstr(data[6]);
            HouseData[hid][ExitPos][2] = floatstr(data[7]);
            HouseData[hid][hPrice] = strval(data[8]);
            format(HouseData[hid][Owner], MAX_PLAYER_NAME, data[9]);
            HouseData[hid][Locked] = strval(data[10]);
            HouseData[hid][HouseStructure][0] = CreatePickup(1273,1,HouseData[hid][EntrancePos][0], HouseData[hid][EntrancePos][1], HouseData[hid][EntrancePos][2],-1);
       //     HouseData[hid][HouseStructure2] = Create3DTextLabel("House",0x008080FF,HouseData[hid][EntrancePos][0], HouseData[hid][EntrancePos][1], HouseData[hid][EntrancePos][2]+1,40.0,-1);
        }
    }
}

public OnPlayerCommandText(playerid, cmdtext[]) {
    new cmd[128], idx;
    cmd = strtok(cmdtext, idx);
    if(!strcmp(cmdtext, "/enter", true)) {
        new pos = IsPlayerInRangeOfHouse(playerid);
        new name[MAX_PLAYER_NAME];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        if(HouseData[pos][Locked] == TRUE && strcmp(HouseData[pos][Owner], name, true)) return GameTextForPlayer(playerid, "~g~LOCKED", 3000, 5);
        SetPlayerPos(playerid, HouseData[pos][ExitPos][0], HouseData[pos][ExitPos][1], HouseData[pos][ExitPos][2]);
        SetPlayerInterior(playerid, HouseData[pos][hInterior]);
        SetPlayerVirtualWorld(playerid, pos);
        return 1;
    }
    if(!strcmp(cmdtext, "/sellhouse", true)) {
        new pos = IsPlayerInRangeOfHouse(playerid);
        new name[MAX_PLAYER_NAME];
        GetPlayerName(playerid, name, MAX_PLAYER_NAME);
        if(!strcmp(HouseData[pos][Owner], name, true)) {
            GameTextForPlayer(playerid, "~g~~>~House Sold.~<~", 3000, 5);
            GivePlayerMoney(playerid, HouseData[pos][hPrice]-500);
            strmid(HouseData[pos][Owner], "Empty House", 0, strlen("Empty House"), 255);
            SaveHouse(pos);
        }
        else return GameTextForPlayer(playerid, "~g~You don't own this house", 3000, 5);
    }
    if(!strcmp(cmdtext, "/housecommands", true)) {
        GameTextForPlayer(playerid, "~g~/sellhouse~n~/lockhouse~n~/unlockhouse~n~/buyhouse", 3000, 5);
        return 1;
    }
    if(!strcmp(cmdtext, "/unlockhouse", true)) {
        new pos = IsPlayerInRangeOfHouse(playerid);
        if(HouseData[pos][Locked] == FALSE) return GameTextForPlayer(playerid, "~g~This house is already unlocked", 3000, 5);
        if(!strcmp(HouseData[pos][Owner], "Empty House", true)) return GameTextForPlayer(playerid, "~g~You don't own this house!", 3000, 5);
        HouseData[pos][Locked] = FALSE;
        GameTextForPlayer(playerid, "~g~~>~House Unlocked.~<~", 3000, 5);
        SaveHouse(pos);
    }
    if(strcmp(cmd, "/changehouseprice", true) == 0)
    {
        if(!IsPlayerAdmin(playerid)) return 1;
        new pos = IsPlayerInRangeOfHouse(playerid);
        new tmp[128];
        tmp = strtok(cmdtext, idx);
        if(strlen(tmp) == 0) return GameTextForPlayer(playerid, "~g~/changehouseprice <price>", 3000, 5);
        HouseData[pos][hPrice] = strval(tmp);
        GameTextForPlayer(playerid, "~g~~>~House Price Changed.~<~", 3000, 5);
        SaveHouse(pos);
        return 1;
    }
    if(!strcmp(cmdtext, "/lockhouse", true)) {
        new pos = IsPlayerInRangeOfHouse(playerid);
        if(HouseData[pos][Locked] == TRUE) return GameTextForPlayer(playerid, "~g~This house is already locked", 3000, 5);
        if(!strcmp(HouseData[pos][Owner], "Empty House", true)) return GameTextForPlayer(playerid, "~g~You don't own this house!", 3000, 5);
        HouseData[pos][Locked] = TRUE;
        GameTextForPlayer(playerid, "~g~~>~House Locked.~<~", 3000, 5);
        SaveHouse(pos);
    }
    if(!strcmp(cmdtext, "/buyhouse", true)) {
        new pos = IsPlayerInRangeOfHouse(playerid);
        if(!strcmp(HouseData[pos][Owner], "Empty House", true)) return GameTextForPlayer(playerid, "~g~OWNED", 3000, 5);
        if(GetPlayerMoney(playerid) >= HouseData[pos][hPrice]) {
            GivePlayerMoney(playerid, -HouseData[pos][hPrice]);
            new name[MAX_PLAYER_NAME];
            GetPlayerName(playerid, name, MAX_PLAYER_NAME);
            strmid(HouseData[pos][Owner], name, 0, MAX_PLAYER_NAME, 255);
            SaveHouse(pos);
            GameTextForPlayer(playerid, "~g~Welcome to your new house.~n~/~r~/housecommands", 3000, 5);
        }
        else return GameTextForPlayer(playerid, "~g~YOU CANNOT AFFORD THIS HOUSE.", 3000, 5);
        return 1;
    }
    if(!strcmp(cmdtext, "/exit", true)) {
        for(new i = 0; i < TOTAL_HOUSES; i ++ ) {
            if(IsPlayerInRangeOfPoint(playerid, 10.0, HouseData[i][ExitPos][0], HouseData[i][ExitPos][1], HouseData[i][ExitPos][2])) {
                if(GetPlayerVirtualWorld(playerid) == i) {
                    SetPlayerPos(playerid, HouseData[i][EntrancePos][0], HouseData[i][EntrancePos][1], HouseData[i][EntrancePos][2]);
                    SetPlayerInterior(playerid, 0);
                    SetPlayerVirtualWorld(playerid, 0);
                }
            }
        }
        return 1;
    }
    return 0;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
    new string[128];
    if(!strcmp(HouseData[HouseData[pickupid][HouseStructure][0]][Owner], "Empty House", true)) {
        format(string, sizeof(string), "~b~House for sale~n~~r~Price:~g~ $%d", HouseData[HouseData[pickupid][HouseStructure][0]][hPrice]);
        GameTextForPlayer(playerid, string, 3000, 5);
    }
    else {
        format(string, sizeof(string), "~b~House owned by: %s", HouseData[HouseData[pickupid][HouseStructure][0]][Owner]);
        GameTextForPlayer(playerid, string, 3000, 5);
    }
    return 1;
}

split(const strsrc[], strdest[][], delimiter)
{
    new i, li;
    new aNum;
    new len;
    while(i <= strlen(strsrc))
    {
        if(strsrc[i] == delimiter || i == strlen(strsrc))
        {
            len = strmid(strdest[aNum], strsrc, li, i, 128);
            strdest[aNum][len] = 0;
            li = i+1;
            aNum++;
        }
        i++;
    }
    return 1;
}

strtok(const string[], &index)
{
    new length = strlen(string);
    while ((index < length) && (string[index] <= ' '))
    {
        index++;
    }

    new offset = index;
    new result[20];
    while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
    {
        result[index - offset] = string[index];
        index++;
    }
    result[index - offset] = EOS;
    return result;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)