CCG/Example: DriverCommand
From TrainzOnline
Contents |
Driver-Command
Directory Structure
A typical asset of this kind has the following File\Directory Structure:
Required Files
config.txt - The config file for the asset.
thumb.jpg - The thumbnail image for this asset. A 240x180 jpeg.
driveto.texture.txt - The texture.txt file. See the section on Texture.txt files on Page 96 for more information.
driveto.tga - The texture file for the drivercommand icon.
drivetocommand.gs - The trainz script file referenced in the config.txt.
drivetoschedulecommand.gs - A trainz script file which is a dependency of "drivercommand.gs".
Config.txt File Listing
kuid <kuid:56113:1268> trainz-build 2.5 category-class "YD" category-region "00" category-era "1800s;1810s;1820s" username "testDriverCommand" kind "drivercommand" supports-null-driver-character 1 script "DriveToCommand.gs" class "DriveToCommand" description "Test command. This does the same thing as `Drive To'." string-table { description "Allows a driver character to take a train either to a destination industry or a specific track in a destination industry." driver_command_drive_to "Drive To " } kuid-table { command-sounds <kuid:-3:10219> } thumbnails { 0 { image "thumb.jpg" width 240 height 180 } 1 { image "driveto.tga" width 64 height 64 } }
DriverToCommand.gs File Listing
// // DriverToCommand.gs // // Copyright (C) 2003 Auran Developments Pty Ltd // All Rights Reserved. // include "DriverCommand.gs" include "World.gs" include "Browser.gs" include "KUID.gs" include "Industry.gs" include "DriveToScheduleCommand.gs" // // Driver command that allows a driver character to take a train either to a destination industry // or a specific track in a destination industry. // class DriveToCommand isclass DriverCommand { // // Initialize parent object and add handlers to process messages for this driver command object. // public void Init(Asset asset) { inherited(asset); AddHandler(me, "DriveToIndustry", null, "DriveTo"); AddHandler(me, "DriveToIndustryTrack", null,"DriveToTrack"); } // // Adds industry destination menu items for all industries to the given menu along with submenus of // destination tracks for all industries added. // public void AddCommandMenuItem(DriverCharacter driver,Menu menu) { Train train; if (driver) { train = driver.GetTrain(); if (!train) return; } StringTable strTable = GetAsset().GetStringTable(); Menu industriesMenu = Constructors.NewMenu(); GameObject[] industryList = World.GetIndustryList(); int i, industryCount = industryList.size(); if (!industryCount) // we dont bother with a `Drive To' command if there are no industries return; for (i = 0; i < industryCount; i++) { Industry industry = cast<Industry>(industryList[i]); string localisedName = industry.GetLocalisedName(); string[] locationNames = new string[0]; string[] locationTracks = new string[0]; industry.AppendDriverDestinations(locationNames,locationTracks); if (localisedName.size()) if (locationNames.size()) { Menu submenu = Constructors.NewMenu(); int j; for (j = 0; j < locationNames.size(); j++) if (locationNames[j] and locationNames[j].size() and locationTracks[j] and locationTracks[j].size()) submenu.AddItem(locationNames[j], me, "DriveToIndustryTrack", industry.GetId() + " " + locationTracks[j]); else if (train) train.Exception("Error in `track names' of industry `" + localisedName + "'"); industriesMenu.AddSubmenu(localisedName + " >",submenu); } else industriesMenu.AddItem(localisedName, me,"DriveToIndustry", industry.GetName()); } industriesMenu.SubdivideItems(); menu.AddSubmenu(strTable.GetString("driver_ command_drive_to") + " >", industriesMenu); } // // Called by either DriveTo() or DriveToTrack() to play one of random driver acknowledgments. // void PlayConfirmation(void) { KUID kuid = GetAsset().LookupKUIDTable("command-sounds"); Library libCommandSounds = World.GetLibrary(kuid); if (libCommandSounds) { libCommandSounds.LibraryCall("PlayConfirmation",null, null); } } DriverScheduleCommand CreateScheduleCommand(DriverCharacter driver, Soup soup) { DriveToScheduleCommand cmd = new DriveToScheduleCommand(); cmd.Init(driver, me); cmd.SetParent(me); cmd.SetProperties(soup); return cast<DriverScheduleCommand>cmd; } // // Handler method to drive a train to an industry (no specific destination track though). // void DriveTo(Message msg) { DriverCommands commands = GetDriverCommands(msg); DriverCharacter driver = cast<DriverCharacter>(msg.src); string industryName = msg.minor; // schedule our command Soup soup = Constructors.NewSoup(); soup.SetNamedTag("industryName", industryName); DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); commands.AddDriverScheduleCommand(cmd); if (driver) PlayConfirmation(); } // // Handler method to drive a train to a specific track in an industry. // void DriveToTrack(Message msg) { DriverCommands commands = GetDriverCommands(msg); DriverCharacter driver = cast<DriverCharacter>(msg.src); string msgData = msg.minor; int industryId = Str.UnpackInt(msgData); string trackName = Str.UnpackString(msgData); GameObject industry = Router.GetGameObject(industryId); if (!industry) return; // schedule our command Soup soup = Constructors.NewSoup(); soup.SetNamedTag("industryName", industry.GetName()); soup.SetNamedTag("trackName", trackName); DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); commands.AddDriverScheduleCommand(cmd); if (driver) PlayConfirmation(); } // void PlayConfirmation(void) { KUID kuid = GetAsset().LookupKUIDTable("command-sounds"); Library libCommandSounds = World.GetLibrary(kuid); if (libCommandSounds) { libCommandSounds.LibraryCall("PlayConfirmation",null, null); } } DriverScheduleCommand CreateScheduleCommand(DriverCharacter driver, Soup soup) { DriveToScheduleCommand cmd = newDriveToScheduleCommand(); cmd.Init(driver, me); cmd.SetParent(me); cmd.SetProperties(soup); return cast<DriverScheduleCommand>cmd; } // // Handler method to drive a train to an industry (no specific destination track though). // void DriveTo(Message msg) { DriverCommands commands = GetDriverCommands(msg); DriverCharacter driver = cast<DriverCharacter>(msg.src); string industryName = msg.minor; // schedule our command Soup soup = Constructors.NewSoup(); soup.SetNamedTag("industryName", industryName); DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); commands.AddDriverScheduleCommand(cmd); // void PlayConfirmation(void) { KUID kuid = GetAsset().LookupKUIDTable("command-sounds"); Library libCommandSounds = World.GetLibrary(kuid); if (libCommandSounds) { libCommandSounds.LibraryCall("PlayConfirmation",null, null); } } DriverScheduleCommand CreateScheduleCommand(DriverCharacter driver, Soup soup) { DriveToScheduleCommand cmd = new DriveToScheduleCommand(); cmd.Init(driver, me); cmd.SetParent(me); cmd.SetProperties(soup); return cast<DriverScheduleCommand>cmd; } // // Handler method to drive a train to an industry (no specific destination track though). // void DriveTo(Message msg) { DriverCommands commands = GetDriverCommands(msg); DriverCharacter driver = cast<DriverCharacter>(msg.src); string industryName = msg.minor; // schedule our command Soup soup = Constructors.NewSoup(); soup.SetNamedTag("industryName", industryName); DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); commands.AddDriverScheduleCommand(cmd); if (driver) PlayConfirmation(); } // // Handler method to drive a train to a specific track in an industry. // void DriveToTrack(Message msg) { DriverCommands commands = GetDriverCommands(msg); DriverCharacter driver = cast<DriverCharacter>(msg.src); string msgData = msg.minor; int industryId = Str.UnpackInt(msgData); string trackName = Str.UnpackString(msgData); GameObject industry = Router.GetGameObject(industryId); if (!industry) return; // schedule our command Soup soup = Constructors.NewSoup(); soup.SetNamedTag("industryName", industry.GetName()); soup.SetNamedTag("trackName", trackName); DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); commands.AddDriverScheduleCommand(cmd); if (driver) PlayConfirmation(); } } // // Handler method to drive a train to a specific track in an industry. // void DriveToTrack(Message msg) { DriverCommands commands = GetDriverCommands(msg); DriverCharacter driver = cast<DriverCharacter>(msg.src); string msgData = msg.minor; int industryId = Str.UnpackInt(msgData); string trackName = Str.UnpackString(msgData); GameObject industry = Router.GetGameObject(industryId); if (!industry) return; // schedule our command Soup soup = Constructors.NewSoup(); soup.SetNamedTag("industryName", industry.GetName()); soup.SetNamedTag("trackName", trackName); DriveToScheduleCommand cmd = cast<DriveToScheduleCommand>CreateScheduleCommand(driver, soup); commands.AddDriverScheduleCommand(cmd); if (driver) PlayConfirmation(); } };
And the dependent script
DriveToScheduleCommand.gs File Fisting
// // DriveToScheduleCommand.gs // // Copyright (C) 2003 Auran Developments Pty Ltd // All Rights Reserved. // include "DriverCommand.gs" include "World.gs" include "Browser.gs" include "KUID.gs" include "Industry.gs" include "DriveToCommand.gs" include "Schedule.gs" // // Driver schedule command used by DriveToCommand to get a driver character to take a train to // a specific track on an industry. // class DriveToScheduleCommand isclass DriverScheduleCommand { public string industryName; // Name of the industry to drive to. public string trackName; // Name of the track in the industry to drive to. DriveToCommand parent; public void SetParent(DriveToCommand newParent) { parent = newParent; } // // Starts executing this schedule command on the given driver character. // public bool BeginExecute(DriverCharacter driver) { Train train = driver.GetTrain(); if (!train) // cant drive if we dont have a train return false; Industry industry = cast<Industry>(Router.GetGameObject(industryName)); if (!industry) // cant drive to an industry which doesn't exist return false; return driver.NavigateToIndustry(industry, trackName); } // we should really implement EndExecute() to allow the game to determine the success of this command // // Provides an icon for this command so it can be seen on the driver's schedule. Uses the industry // icon to indicate the destination. // public object GetIcon(void) { Industry industry = cast<Industry>(Router.GetGameObject(industryName)); return cast<object>industry; } public string GetTooltip(void) { StringTable strTable = GetAsset().GetStringTable(); string userTrackName = trackName; Industry industry = cast<Industry>(Router.GetGameObject(industryName)); if (industry) { string[] destNames = new string[0]; string[] destTracks = new string[0]; industry.AppendDriverDestinations(destNames,destTracks); int i; for (i = 0; i < destNames.size(); i++) { if (destTracks[i] == trackName) { userTrackName = destNames[i]; break; } } } return strTable.GetString("driver_command_drive_to") + industryName + " (" + userTrackName + ")"; } public Soup GetProperties(void) { Soup soup = Constructors.NewSoup(); // Save support // Save the properties to the soup, then return the soup soup.SetNamedTag("industryName", industryName); soup.SetNamedTag("trackName", trackName); return soup; } public void SetProperties(Soup soup) { // Load support // Setup the properties from the passed in soup industryName = soup.GetNamedTag("industryName"); trackName = soup.GetNamedTag("trackName"); } };
Download this asset
This asset is available for download from the TRS2006website at:
http://files.auran.com/TRS2006/Downloads/Example_Download.zip