Skip to content

Instantly share code, notes, and snippets.

@Southclaws
Created April 7, 2014 15:51
Show Gist options
  • Save Southclaws/10022912 to your computer and use it in GitHub Desktop.
Save Southclaws/10022912 to your computer and use it in GitHub Desktop.
Basic modular house script
/*
modhs - Modular House Script
modhs.core
Creates, destroys and offers interfacing functions for houses.
By Southclaw
*/
#define MAX_HOUSE (50)
enum e_house_data
{
house_Exists, // True if the house exists
house_Owner[MAX_PLAYER_NAME],
house_Price,
Float: house_EnterX,
Float: house_EnterY,
Float: house_EnterZ,
Float: house_ExitX,
Float: house_ExitY,
Float: house_ExitZ,
house_Interior
}
// 'static' makes the variables only accessible in this script.
static
house_Data[MAX_HOUSE][e_house_data],
house_Total;
stock CreateHouse(owner[MAX_PLAYER_NAME], price, Float:enter_x, Float:enter_y, Float:enter_z, Float:exit_x, Float:exit_y, Float:exit_z, interior)
{
// Declare a variable to store the ID of this house
new id;
// Search for the next available house slot.
// while the "house_Exists" field is true, keep increasing 'id'
// When "house_Exists" is not true, the loop will stop and 'id' will contain
// the value of the next available house slot that doesn't exist yet.
while(house_Data[id][house_Exists])
id++;
// We can now use 'id' as the cell address
// The house now exists, next time this function is called, this house ID
// will be skipped.
house_Data[id][house_Exists] = 1;
house_Data[id][house_Owner] = owner;
house_Data[id][house_Price] = price;
house_Data[id][house_EnterX] = enter_x;
house_Data[id][house_EnterY] = enter_y;
house_Data[id][house_EnterZ] = enter_z;
house_Data[id][house_ExitX] = exit_x;
house_Data[id][house_ExitY] = exit_y;
house_Data[id][house_ExitZ] = exit_z;
house_Data[id][house_Interior] = interior;
// Increase the total amount of houses.
house_Total++;
// Return the ID of the house.
// This means the return value of this function can be used as a HouseID for
// use in other functions to manipulate house data.
return id;
}
stock DestroyHouse(houseid)
{
// First, make sure this is a valid house id
// If the 'houseid' value is below 0 or above MAX_HOUSE, a memory error will
// occur.
if(!(0 <= houseid < MAX_HOUSE))
return 0;
// Now check if the house exists, no point destroying a house that doesn't.
if(!house_Data[houseid][house_Exists])
return 0;
// If all that is valid, now the house data can be erased.
house_Data[houseid][house_Exists] = 0;
// Sets the first cell of the house owner string to 'EOS' (end of string)
house_Data[houseid][house_Owner][0] = EOS;
house_Data[houseid][house_Price] = 0;
house_Data[houseid][house_EnterX] = 0.0;
house_Data[houseid][house_EnterY] = 0.0;
house_Data[houseid][house_EnterZ] = 0.0;
house_Data[houseid][house_ExitX] = 0.0;
house_Data[houseid][house_ExitY] = 0.0;
house_Data[houseid][house_ExitZ] = 0.0;
house_Data[houseid][house_Interior] = 0;
// Decrement the amount of houses
house_Total--;
// Function was successful, return 1.
return 1;
}
/*
These are called "Interface" functions, they get and set data for houses.
*/
// Checks if a houseid is a valid value, not if it exists.
// Every function should have this check to ensure there are no memory errors.
stock IsValidHouse(houseid)
{
if(!(0 <= houseid < MAX_HOUSE))
return 0;
return 1;
}
// house_Exists
stock HouseExists(houseid)
{
if(IsValidHouse(houseid))
return 0;
return house_Data[houseid][house_Exists];
}
// house_Owner
stock GetHouseOwner(houseid, owner[MAX_PLAYER_NAME])
{
if(IsValidHouse(houseid))
return 0;
owner = house_Data[houseid][house_Owner];
return 1;
}
stock SetHouseOwner(houseid, owner[MAX_PLAYER_NAME])
{
if(IsValidHouse(houseid))
return 0;
house_Data[houseid][house_Owner] = owner;
return 1;
}
// house_Price
stock GetHousePrice(houseid)
{
if(IsValidHouse(houseid))
return 0;
return house_Data[houseid][house_Price];
}
stock SetHousePrice(houseid, price)
{
if(IsValidHouse(houseid))
return 0;
house_Data[houseid][house_Price] = price;
return 1;
}
// house_EnterX
// house_EnterY
// house_EnterZ
stock GetHouseEnterPos(houseid, &Float:x, &Float:y, &Float:z)
{
if(IsValidHouse(houseid))
return 0;
x = house_Data[houseid][house_EnterX];
y = house_Data[houseid][house_EnterY];
z = house_Data[houseid][house_EnterZ];
return 1;
}
stock SetHouseEnterPos(houseid, Float:x, Float:y, Float:z)
{
if(IsValidHouse(houseid))
return 0;
house_Data[houseid][house_EnterX] = x;
house_Data[houseid][house_EnterY] = y;
house_Data[houseid][house_EnterZ] = z;
return 1;
}
// house_ExitX
// house_ExitY
// house_ExitZ
stock GetHouseExitPos(houseid, &Float:x, &Float:y, &Float:z)
{
if(IsValidHouse(houseid))
return 0;
x = house_Data[houseid][house_ExitX];
y = house_Data[houseid][house_ExitY];
z = house_Data[houseid][house_ExitZ];
return 1;
}
stock SetHouseExitPos(houseid, Float:x, Float:y, Float:z)
{
if(IsValidHouse(houseid))
return 0;
house_Data[houseid][house_ExitX] = x;
house_Data[houseid][house_ExitY] = y;
house_Data[houseid][house_ExitZ] = z;
return 1;
}
// house_Interior
stock GetHouseInterior(houseid)
{
if(IsValidHouse(houseid))
return 0;
return house_Data[houseid][house_Interior];
}
stock SetHouseInterior(houseid, interior)
{
if(IsValidHouse(houseid))
return 0;
house_Data[houseid][house_Interior] = interior;
return 1;
}
/*
modhs - Modular House Script
modhs
Includes everything in the correct order.
By Southclaw
*/
#include "modhs/modhs.core.pwn"
#include "modhs/modhs.storage.pwn"
#include "modhs/modhs.interact.pwn"
/*
modhs - Modular House Script
modhs.interact
Allows interaction with houses.
By Southclaw
*/
/*
OnPlayerPickUpPickup or whatever you use to allow players to enter houses.
Set player position to GetHouseEnterPos.
*/
/*
modhs - Modular House Script
modhs.storage
Stores house data while the server is off.
By Southclaw
*/
#define HOUSE_PATH "/Houses/%d.ini"
stock HousePath(houseid)
{
new string[128];
format(string, sizeof (string),HOUSE_PATH, houseid);
return 1:
}
LoadHouses()
{
// Not sure how y_ini works properly
// I assumed you had some y_ini load code already written for this part.
}
SaveHouses()
{
new
owner[MAX_PLAYER_NAME],
enter_x,
enter_y,
enter_z,
exit_x,
exit_y,
exit_z;
for(new i; i < MAX_HOUSES; i++)
{
if(!HouseExists(i))
continue;
GetHouseOwner(i, owner);
GetHouseEnterPos(i, enter_x, enter_y, enter_z);
GetHouseExitPos(i, exit_x, exit_y, exit_z);
//new INI:ini = INI_Open(HousePath(i));
INI_SetTag(ini, "Main");
INI_WriteString(ini, "Owner", owner);
INI_WriteInt(ini, "Price", GetHousePrice(i));
INI_WriteFloat(ini, "EnterX", enter_x);
INI_WriteFloat(ini, "EnterY", enter_y);
INI_WriteFloat(ini, "EnterZ", enter_z);
INI_WriteFloat(ini, "ExitX", exit_x);
INI_WriteFloat(ini, "ExitY", exit_y);
INI_WriteFloat(ini, "ExitZ", exit_z);
INI_WriteInt(ini, "Interior", GetHouseInterior(i));
INI_Close(ini);
}
}
forward LoadHouse(i, name[], value[]);
public LoadHouse(i, name[], value[])
{
new
owner[MAX_PLAYER_NAME],
price,
Float:enter_x,
Float:enter_y,
Float:enter_z,
Float:exit_x,
Float:exit_y,
Float:exit_z,
interior;
INI_String("Owner", owner, MAX_PLAYER_NAME);
INI_Int("Price", price);
INI_Float("EnterX", enter_x);
INI_Float("EnterY", enter_y);
INI_Float("EnterZ", enter_z);
INI_Float("ExitX", exit_x);
INI_Float("ExitY", exit_y);
INI_Float("ExitZ", exit_z);
INI_Int("Interior", interior);
CreateHouse(owner, price, enter_x, enter_y, enter_z, exit_x, exit_y, exit_z, interior);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment