Files
dsrc/sku.0/sys.server/compiled/game/script/terminal/mini_vehicle_terminal.script
T

382 lines
10 KiB
Plaintext

// ALL MINI VEHICLE SCRIPTS
// structure/mini_vehicle_control.script
// creature/mini_vehicle.script
// terminal/mini_vehicle_terminal.script
include library.player_structure;
include library.utils;
//STRING IDs
const string MENU_FILE = "pet/pet_menu";
const string_id VEHICLE_NAME = new string_id(MENU_FILE,"mini_vehicle_custom_name");
const string_id VEHICLE_BIOLINK_NEEDED = new string_id(MENU_FILE,"mini_vehicle_biolink_needed");
const string_id VEHICLE_NOT_INVENTORY = new string_id(MENU_FILE,"mini_vehicle_not_inventory");
const string_id VEHICLE_NOT_OWNER = new string_id(MENU_FILE,"mini_vehicle_not_owner");
const string_id VEHICLE_NOT_IN_HOUSE = new string_id(MENU_FILE,"mini_vehicle_not_in_house");
//SUI MENU STRING IDs
const string_id SUMMON = new string_id("sui", "summon_vehicle");
const string_id DESTROY = new string_id("sui", "destroy_vehicle");
//variable constants
const string OBJVAR_VEHICLE_PATROL_POINTS = "vehicle.patrolPoints";
const string OBJVAR_VEHICLE_PATROL_LOOP = "vehicle.patrol_loop";
const string OBJVAR_VEHICLE_PATROL_ONCE = "vehicle.patrol_once";
const string VEHICLE_ID = "vehicle.id";
const string VEHICLE_NAMED = "vehicle.named";
const string PLAYER = "vehicle.user";
//------------------------------------------------
//triggers
//------------------------------------------------
//If the house is loaded, this will be called
trigger OnInitialize()
{
obj_id terminal = self;
obj_id vehicle = getObjIdObjVar(terminal, VEHICLE_ID);
//If the vehicle OID is valid but the Object no longer exists
if(!isValidId(vehicle))
{
removeObjVar(terminal, VEHICLE_ID);
}
else if(!exists(vehicle))
{
boolean created = recreateVehicle(terminal);
if(!created)
removeObjVar(terminal, VEHICLE_ID);
}
return SCRIPT_CONTINUE;
}
//For whatever reason, if the terminal is destroyed the vehicle is to
trigger OnDestroy()
{
obj_id terminal = self;
obj_id vehicle = getObjIdObjVar(terminal, VEHICLE_ID);
if (!isValidId(vehicle))
return SCRIPT_CONTINUE;
destroyObject(vehicle);
return SCRIPT_CONTINUE;
}
//This is called if the player picks up the terminal object. Making sure the vehicle is destroyed.
trigger OnAboutToBeTransferred(obj_id destContainer, obj_id transferer)
{
obj_id terminal = self;
if(!isValidId(terminal))
return SCRIPT_CONTINUE;
if(hasObjVar(terminal, VEHICLE_ID) && exists(terminal))
{
messageTo(terminal, "destroyVehicle", null, 1, false);
}
//we can't leave the location array on the terminal in case the terminal is moved to another house.
if (hasObjVar(terminal, OBJVAR_VEHICLE_PATROL_POINTS) && exists(terminal))
{
removeObjVar(terminal, OBJVAR_VEHICLE_PATROL_POINTS);
}
if (hasObjVar(terminal, OBJVAR_VEHICLE_PATROL_LOOP) && exists(terminal))
{
removeObjVar(terminal, OBJVAR_VEHICLE_PATROL_LOOP);
}
if (hasObjVar(terminal, OBJVAR_VEHICLE_PATROL_ONCE) && exists(terminal))
{
removeObjVar(terminal, OBJVAR_VEHICLE_PATROL_ONCE);
}
return SCRIPT_CONTINUE;
}
trigger OnObjectMenuRequest(obj_id player, menu_info mi)
{
obj_id terminal = self;
if(!exists(terminal))
return SCRIPT_CONTINUE;
if(utils.isInVendor(terminal) || utils.isInBazaar(terminal))
return SCRIPT_CONTINUE;
//get the top container
obj_id ownerContainer = getTopMostContainer(terminal);
if(!isValidId(ownerContainer) || !exists(ownerContainer))
return SCRIPT_CONTINUE;
//make sure container is a player structure, or player inventory if not abort.
//If the terminal is in a treasure drum or other container default menu options for pickup and examine only
if(!player_structure.isBuilding(ownerContainer) && !isPlayer(ownerContainer))
return SCRIPT_CONTINUE;
if(hasObjVar(terminal, VEHICLE_ID))
{
mi.addRootMenu(menu_info_types.ITEM_USE, DESTROY);
}
else
{
mi.addRootMenu(menu_info_types.ITEM_USE, SUMMON);
}
return SCRIPT_CONTINUE;
}
trigger OnObjectMenuSelect(obj_id player, int item)
{
obj_id terminal = self;
obj_id biolink = getBioLink(terminal);
if(!isValidId(terminal))
return SCRIPT_CONTINUE;
if(!isValidId(player))
return SCRIPT_CONTINUE;
sendDirtyObjectMenuNotification(terminal);
if(item != menu_info_types.ITEM_USE)
return SCRIPT_CONTINUE;
//Make sure the terminal is BioLinked.
if(!isValidId(biolink) || biolink == utils.OBJ_ID_BIO_LINK_PENDING)
{
sendSystemMessage(player, VEHICLE_BIOLINK_NEEDED);
return SCRIPT_CONTINUE;
}
//Make sure not in inventory
if(utils.isNestedWithinAPlayer(terminal))
{
sendSystemMessage(player, VEHICLE_NOT_INVENTORY);
return SCRIPT_CONTINUE;
}
//Make sure this is the owner
if(player != getObjIdObjVar(terminal, "biolink.id"))
{
sendSystemMessage(player, VEHICLE_NOT_OWNER);
return SCRIPT_CONTINUE;
}
if(!utils.isInHouseCellSpace(terminal))
{
sendSystemMessage(player, VEHICLE_NOT_IN_HOUSE);
return SCRIPT_CONTINUE;
}
if(!hasObjVar(terminal, VEHICLE_ID))
{
utils.setScriptVar(terminal, "terminal.summon", true);
boolean vehicleCreated = createVehicle(terminal, player);
if(!vehicleCreated)
removeObjVar(terminal, VEHICLE_ID);
}
else
{
messageTo(terminal, "destroyVehicle", null, 1, false);
}
return SCRIPT_CONTINUE;
}
//------------------------------------------------
//messageHandlers
//------------------------------------------------
messageHandler destroyVehicle()
{
obj_id terminal = self;
if(!isValidId(terminal))
return SCRIPT_CONTINUE;
obj_id player = getObjIdObjVar(terminal, "biolink.id");
if(!isValidId(player) || !exists(player))
return SCRIPT_CONTINUE;
obj_id vehicle = getObjIdObjVar(terminal, VEHICLE_ID);
if(isValidId(vehicle) && exists(vehicle))
{
obj_id house = getObjIdObjVar(vehicle, "house");
if(!isValidId(house) || !exists(house))
return SCRIPT_CONTINUE;
removeObjVar(house, "mini_vehicle.vehicle");
if(hasScript(house, "structure.mini_vehicle_control"))
detachScript(house, "structure.mini_vehicle_control");
destroyObject(vehicle);
}
removeObjVar(terminal, VEHICLE_ID);
sendConsoleMessage(player, "vehicle retired.");
return SCRIPT_CONTINUE;
}
messageHandler OnPack()
{
obj_id vehicle = getObjIdObjVar(self, VEHICLE_ID);
if (!isValidId(vehicle))
return SCRIPT_CONTINUE;
destroyObject(vehicle);
return SCRIPT_CONTINUE;
}
boolean setVehicleVars(obj_id terminal, obj_id vehicle)
{
if(!isValidId(terminal) || !exists(terminal))
return false;
if(!isValidId(vehicle) || !exists(vehicle))
return false;
obj_id user = getObjIdObjVar(terminal, "biolink.id");
setInvulnerable(vehicle, true);
//script automatically attached in the vehicles template
setObjVar(terminal, VEHICLE_ID, vehicle);
setObjVar(vehicle, "terminal", terminal);
setObjVar(vehicle, PLAYER, user);
return true;
}
boolean createVehicle(obj_id terminal, obj_id player)
{
//debugSpeakMsg(terminal, "spawning function");
if(!isValidId(terminal) || !exists(terminal))
return false;
obj_id anotherVehicle = getObjIdObjVar(terminal, VEHICLE_ID);
//don't spawn more than one vehicle
if(isValidId(anotherVehicle) && exists(anotherVehicle))
return false;
//debugSpeakMsg(terminal, "so far so good1");
obj_id house = getTopMostContainer(terminal);
if(!isIdValid(house) && !exists(house))
return false;
if(!player_structure.isBuilding(house))
return false;
//debugSpeakMsg(terminal, "so far so good2");
//we need to listen for the broadcast and we can't do this
//until we know for sure the terminal is biolinked and installed in a house
if(!hasScript(house, "structure.mini_vehicle_control"))
{
attachScript(house, "structure.mini_vehicle_control");
listenToMessage(house, "handleActivateVehicle");
}
//If the owner never set up the patrol, don't spawn vehicle
location startLocation = new location();
location[] patrolLoc = getLocationArrayObjVar(terminal, OBJVAR_VEHICLE_PATROL_POINTS);
if (patrolLoc == null)
{
//debugSpeakMsg(terminal, "patrol locations == null");
location playerLoc = getLocation(player);
if(!isValidId(playerLoc.cell))
{
startLocation = getLocation(terminal);
}
else
{
startLocation = getLocation(player);
}
}
else
{
//validate location array
boolean validList = validLocationList(terminal, patrolLoc);
if(!validList)
return false;
//start vehicle at first location
startLocation = patrolLoc[0];
}
string template = getStringObjVar(terminal, "template");
obj_id vehicle = createObject(template, startLocation);
if (patrolLoc != null)
messageTo(vehicle, "handleActivateVehicle", null, 1, false);
if(!isIdValid(vehicle) && !exists(vehicle))
return false;
setVehicleVars(terminal, vehicle);
if(utils.hasScriptVar(terminal, "terminal.summon"))
{
sendConsoleMessage(player, "vehicle summoned.");
utils.removeScriptVar(terminal, "terminal.summon");
}
return true;
}
boolean recreateVehicle(obj_id terminal)
{
obj_id anotherVehicle = getObjIdObjVar(terminal, VEHICLE_ID);
//don't spawn more than one vehicle
if(isValidId(anotherVehicle) && exists(anotherVehicle))
return false;
obj_id house = getTopMostContainer(terminal);
if(!isIdValid(house) && !exists(house))
return false;
if(!player_structure.isBuilding(house))
return false;
//we need to listen for the broadcast and we can't do this
//until we know for sure the terminal is biolinked and installed in a house
if(!hasScript(house, "structure.mini_vehicle_control"))
{
attachScript(house, "structure.mini_vehicle_control");
listenToMessage(house, "handleActivateVehicle");
}
//If the owner never set up the patrol, don't spawn vehicle
location startLocation = new location();
location[] patrolLoc = getLocationArrayObjVar(terminal, OBJVAR_VEHICLE_PATROL_POINTS);
if(patrolLoc == null)
return false;
//validate location array
boolean validList = validLocationList(terminal, patrolLoc);
if(!validList)
return false;
//start vehicle at first location
startLocation = patrolLoc[0];
string template = getStringObjVar(terminal, "template");
obj_id vehicle = createObject(template, startLocation);
messageTo(vehicle, "handleActivateVehicle", null, 1, false);
if(!isIdValid(vehicle) && !exists(vehicle))
return false;
setVehicleVars(terminal, vehicle);
if(utils.hasScriptVar(terminal, "terminal.summon"))
{
utils.removeScriptVar(terminal, "terminal.summon");
}
return true;
}
boolean validLocationList(obj_id terminal, location[] patrolLocations)
{
if(patrolLocations == null)
return false;
for (int i = 0; i < patrolLocations.length; ++i)
{
if(!isValidId(patrolLocations[i].cell))
{
removeObjVar(terminal, OBJVAR_VEHICLE_PATROL_POINTS);
return false;
}
}
return true;
}