I've written this script to put on doors of rooms people rent. It's designed to collect rent every 7 days from the time they've first opened the door with their key:
// Rent collection script that goes in the OnOpen for player's door
#include "datetime"
void main()
{
object oPC = GetLastOpenedBy();
object oKey = GetFirstItemInInventory(oPC);
int bAlreadyOpened = GetPersistantDynamicInt(oKey, "Rent Collection On");
string sNextPayment;
string sKeyTag;
string sDoorTagOwner = GetStringLeft(GetTag(OBJECT_SELF), 4);
struct DateTime sNow = getCurrentDateTime();
struct DateTime sRentCollectionTime =
addDateTime(GetPersistantDateTime(oKey, "Rent Start Time"), newDateTime(0,0,7,0));
// Check if the PC has the door's key
while (GetIsObjectValid(oKey))
{
// Check if sDoorTagOwner is the same as the first 4 letters of sKeyTag
// Key tag names are usually of the format <Character Name>Key, and door
// tag names for renters are usually of the format <Character Name>Door.
sKeyTag = GetStringLeft(GetTag(oKey),4);
if (TestStringAgainstPattern(sKeyTag, sDoorTagOwner))
{
// if this object is in fact the door key, check to see if this is
// the first time the door is being opened by the player. If it is,
// start the rent clock ticking. Collect rent every 7 days.
if (!bAlreadyOpened)
{
// Set key to Rent Collection Status ON
SetPersistantInt(oKey, "Rent Collection On", TRUE, 60, SCRIPTNAME_GENERAL);
// Set Rent Start time to Current Time
SetPersistantDateTime(oKey, "Rent Start Time", sNow);
}
// if the door has already been opened before, check the time on the key
// to see if 7 days have passed since the last Rent Start Time
// if more than 7 days have passed, collect the rent and reset the rent
// start time
else if (bAlreadyOpened
&& compareDateTime(sNow, sRentCollectionTime) >= 7
&& GetGold(oPC) >= 1000)
{
TakeGoldFromCreature(1000, oPC, TRUE);
SendMessageToPC(oPC, "1000 gold has been taken for your rent.");
SetPersistantDateTime(oKey, "Rent Start Time", sNow);
}
// if the player doesn't have enough gold after 7 days, destroy their key
else if (bAlreadyOpened
&& compareDateTime(sNow, sRentCollectionTime) >= 7
&& GetGold(oPC) <= 1000)
{
DestroyObject(oKey);
SendMessageToPC(oPC, "You do not have enough money to meet your rent!");
SendMessageToPC(oPC, "Your key has been destroyed. You will have to see a real estate agent.");
}
}
oKey = GetNextItemInInventory();
}
}
Door Rent Collection Script, tell me if you see obvious bugs
Moderator: Event DM
-
- World Advisor, Co-founder
- Posts: 15149
- Joined: Fri Sep 14, 2001 9:48 pm
- Timezone: GMT-5
- Contact:
Door Rent Collection Script, tell me if you see obvious bugs
"Truth has no form."
--Idries Shah
--Idries Shah
Looks fine. Only thing is rent will be collected multiple times if the player happens to have more than one key.
If I understand that correctly, you're planning to hardcode (=set in the toolset) the tags of the keys and doors, right ? Have you considered assigning numbers from 0000 - 9999 and use a database table that associates the number with the character (e.g. 0123 = 'Joe') ? May be easier to maintain in the long run.
If I understand that correctly, you're planning to hardcode (=set in the toolset) the tags of the keys and doors, right ? Have you considered assigning numbers from 0000 - 9999 and use a database table that associates the number with the character (e.g. 0123 = 'Joe') ? May be easier to maintain in the long run.