not work

← разместить еще код

Расшаренный код:

include "gs.gs"
include "interface.gs"
include "locomotive.gs"
include "world.gs"
include "world1.gs"
include "vehicle.gs"
include "MapObject.gs"
include "sessionvariables.gs"

class TransitVehicle isclass Locomotive
{
	Asset Destinations;
	Asset Routes;
	
	StringTable RouteTable;
	StringTable DestTable;
	
	bool bHasRoutes = false;
	bool bHasDestinations = false;
	int iCurrentRoute;
	int iCurrentDest;
    string sCurrentRoute;
	string sCurrentDest;	
	
	Soup RTSoup;
	Soup DTSoup;
	
	int numRoutes;
	int numDests;
		
	bool DTListShown = false;
	bool RTListShown = false;
	bool NightEnabled = false;
	
	string[] carNumbers = new string[0];
	string myNumber = " ";
	string name3, name4, name5;		
	Browser viewer;
	
	
	void ViewMyDetails(void);
	
	void SetLocoNumber()
	{
		StringTable assetStringTable = GetAsset().GetStringTable();
		string sl = assetStringTable.GetString("number_low");
		string sh = assetStringTable.GetString("number_high");
		
		if((sl == "") or (sh == "")) {
			//Interface.Log(me.GetName() + ": No running numbers detected.");
			return;
		}
		
		int low = Str.ToInt(sl);
		int high = Str.ToInt(sh);
		int inc = Str.ToInt(assetStringTable.GetString("number_increment"));
				
		int c, n;
		
		if (inc < 1) inc = 1;
		
		n = low; c = 0;
		while(n <= high)
		{
			carNumbers[c] = (string)(n);
			//Interface.Log("car number " + c + " is " + carNumbers[c]);
			n = n + inc;
			c++;
		}
		
		string strCurrentNumber = me.GetRunningNumber();
		if (strCurrentNumber != "") {
			// Assume that whatever number has been entered is what the
			// user wants, even if it is not prototypically correct.
			return;
		}
	    
		string strFinal;
		strFinal = Str.ToInt(carNumbers[Math.Rand(0, carNumbers.size())]);
		me.SetRunningNumber(strFinal);		
	}
	
	thread void VehicleMain()
	{
		float t;
		
		while(1)
		{
			Sleep(2);
			t = World.GetGameTime();
			//Interface.Log(me.GetName() + " tick, gametime is " + (t * 24));
			if ((t >= 0.201166) and (t <= 0.75)) {
				if (NightEnabled == true) {
					NightEnabled = false;
					//Interface.Log(me.GetName() + " turns off his lights.");
					SetMeshVisible("nightbase", 1, 1.0);
					SetMeshVisible("day", 0, 1.0);
				}
			}
			else if ((t < 0.201166) or (t > 0.75))
			{
				if (NightEnabled == false) {
					//Interface.Log(me.GetName() + " turns on his lights.");
					NightEnabled = true;
					SetMeshVisible("nightbase", 0, 1.0);
					SetMeshVisible("day", 1, 1.0);
				}
			}
		}
	}
	
	public void Init(void)
	{
		// call the parent
		inherited();

		StringTable st = GetAsset().GetStringTable();
		string isLoco = st.GetString("isLocomotive");
		string sHasRoutes = st.GetString("HasRoutes");
		string sHasDestinations = st.GetString("HasDestinations");
		
		if (sHasRoutes == "1") {
			Interface.Log(me.GetName() + " has routes");
			Routes = GetAsset().FindAsset ("routes");		
			RTSoup = Routes.GetConfigSoup().GetNamedSoup("textures");
			numRoutes = RTSoup.CountTags() - 1;
			bHasRoutes = true;
		}
		
		if (sHasDestinations == "1") {
			Destinations = GetAsset().FindAsset ("destinations");
			DTSoup = Destinations.GetConfigSoup().GetNamedSoup("textures");
			numDests = DTSoup.CountTags() - 1;
			bHasDestinations = true;
		}
				
		if (isLoco == "0") {
			//Interface.Log(me.GetName() + " is not a locomotive.");
			SetBrokenThrottle(1);
			SetBrokenReverser(1);
		}
		
		// set loco number if the user's not put one
		SetLocoNumber();
		
		// the message handlers
		if ((bHasDestinations == 1) or (bHasRoutes == 1)) {
			// only add handlers here if the vehicle has route/destination signs.
			AddHandler(me, "ChangeDestinationSign","","ChangeDestinationSign");
			AddHandler(me, "QueryRouteSign", "", "QueryRoute");
		}
			
		AddHandler(me, "MapObject", "View-Details", "ViewDetails");		       
		AddHandler(me, "Browser-Closed", "", "BrowserClose");
		AddHandler(me, "Browser-URL", "", "BrowserClick");
		//AddHandler(me, "World","Night", "WorldNight");
		
		VehicleMain();
	}
		
	void BrowserClick(Message msg) 
	{
		if (viewer == null) return;
		
		bool d = false;
		string lt;
		Train t = me.GetMyTrain();
		string propCmd = msg.minor[16, ];

		if (msg.src == viewer) 
		{
			if (propCmd == "priority-number") {
				t.SetTrainPriorityNumber((t.GetTrainPriorityNumber() % 3) + 1);
			}
			else if (propCmd == "rs_next") {
				iCurrentRoute = iCurrentRoute + 1;
				if (iCurrentRoute > numRoutes) iCurrentRoute = 0;				
				SetFXTextureReplacement("route-sign", Routes, iCurrentRoute);
				d = true;
			}
			else if (propCmd == "rs_prev") {
				iCurrentRoute = iCurrentRoute - 1;
				if (iCurrentRoute < 0) iCurrentRoute = numRoutes;
				SetFXTextureReplacement("route-sign", Routes, iCurrentRoute);
				d = true;
			}
			else if (propCmd == "rs_list") {
				RTListShown = !RTListShown;
				d = true;
			}
			else if (propCmd == "ds_next") {
				iCurrentDest = iCurrentDest + 1;
				if (iCurrentDest > numDests) iCurrentDest = 0;
				SetFXTextureReplacement("dest-sign",Destinations,iCurrentDest);
				d = true;
			}
			else if (propCmd == "ds_prev") {
				iCurrentDest = iCurrentDest - 1;
				if (iCurrentDest < 0) iCurrentDest = numDests;
				SetFXTextureReplacement("dest-sign",Destinations,iCurrentDest);
				d = true;
			}
			else if (propCmd == "ds_list") {
				DTListShown = !DTListShown;
				d = true;
			}
			
			if(d == false) {
				if (RTListShown == true)
				{
					int i;
					for (i = 0; i <= numRoutes; i++)
					{
						lt = "rslist_" + i;
						if(propCmd == lt)
						{
							//Interface.Print("list clicked.");
							iCurrentRoute = i;
							SetFXTextureReplacement("route-sign", Routes, iCurrentRoute);
							d = true;
							break;
						}
					}
				}
			}
			
			if (DTListShown == true)
			{				
				int j;
				for (j= 0; j <= numDests; j++)
				{
					lt = "dslist_" + j;
					if(propCmd == lt)
					{
						iCurrentDest = j;
						SetFXTextureReplacement("dest-sign",Destinations,iCurrentDest);
						break;
					}
				}
			}
		}

		ViewMyDetails();
	}
	
	string ShowRouteList(void)
	{
		int n;
		string cs;
		string s = "<table width = 100%>";
		
		for (n = 0; n <= numRoutes; n++) 
		{
			cs = Str.Tokens(RTSoup.GetNamedTag(n),".")[0];
			if(n == iCurrentRoute) {s = s + "<tr><td><font size + 1><b>";}
			else {s = s + "<tr><td>";}
			
			s = s + "<a href=live://property/rslist_" + n + ">" +  cs + "</td></tr>";
		}
		s = s + "</table>";
		return s;
	}
	
	string ShowDestList(void)
	{
		int n;
		string cs;
		string s = "<table width = 100%>";
		
		for (n = 0; n <= numDests; n++) 
		{
			cs = Str.Tokens(DTSoup.GetNamedTag(n),".")[0];
			if(n == iCurrentDest) {s = s + "<tr><td><font size + 1><b>";}
			else {s = s + "<tr><td>";}
			
			s = s + "<a href=live://property/dslist_" + n + ">" +  cs + "</td></tr>";
		}
		s = s + "</table>";
		return s;
	}
	
	void ViewMyDetails(void)
	{
		Message msg;
		int i;
		string RSCurrent = "none";
		string DSCurrent = "none";
		
		if (!viewer) {
			viewer = Constructors.NewBrowser();
			
			viewer.SetWindowRect(150, 150, 150 + 375, 150 + 300);
		}
		
		Train t = me.GetMyTrain();
		Vehicle[] vehicles = t.GetVehicles();
		DriverCharacter driver = t.GetActiveDriver();
		string prNum = (string)t.GetTrainPriorityNumber();
		string myKuidStr = me.GetAsset().GetKUID().GetHTMLString();
		string prProp = "<a href='live://property/priority-number'>";
		string myName = BrowserInterface.Quote(me.GetLocalisedName());
		string trainName = BrowserInterface.Quote(t.GetTrainDisplayName());
        
		string DSPrev = "<a href=live://property/ds_prev>Prev</a>";    
		string DSNext = "<a href=live://property/ds_next>Next</a>";
		string RSPrev = "<a href=live://property/rs_prev>Prev</a>";    
		string RSNext = "<a href=live://property/rs_next>Next</a>";
		
		if (bHasRoutes) {		
			RSCurrent = Str.Tokens(RTSoup.GetNamedTag(iCurrentRoute),".")[0];
		}
		
		if (bHasDestinations) {
			DSCurrent = Str.Tokens(DTSoup.GetNamedTag(iCurrentDest),".")[0];
		}
		
		string RSList = "<a href=live://property/rs_list>List</a>";
		string DSList = "<a href=live://property/ds_list>List</a>";
		
		// Total mass of all non-locomotives in this train
		float totalMass = 0.0;

		for (i = 0; i < vehicles.size(); i++) {
			if (!vehicles[i].isclass(Locomotive)) totalMass = totalMass + vehicles[i].GetMass();
		}
		string massStr = "Weight in tow: " + ((string)(int)(totalMass / 1000) + " t");
		
		// Begin HTML layout
		string hd = "<html><body><table><tr height=32>";
		
		// Driver icon
		if (driver) {
			string driverImgKuid = driver.GetAsset().GetKUID().GetHTMLString();
			hd = hd + "<td><img kuid='" + driverImgKuid + "' width=32 height=32></td>";
		}
		
		// Train (driver) name and weight
		hd = hd + "<td><b><font size=3 color=#000000>" + trainName + "</font></b></td></tr></table>"
			+ "<p><font color=#000000>" + massStr + "</font></p>";

		// Priority on track
		hd = hd + "<p><font color=#000000>Priority: " + prProp + prNum + "</a></font></p>";

		// Train/Vehicle separator
		hd = hd + "<br><table bgcolor=#000000D0 cellpadding=0 cellspacing=0 border=0 width=100%>"
			+ "<tr height=1><td width=100%></td></tr></table><br>";

		// Vehicle Details
		hd = hd + "<table><tr>"
			+ "<td><img kuid='" + myKuidStr + "' width=64 height=32></td>"
			+ "<td><font color=#AA0000><b>" + myName + "</b></font></td></tr></table>";
	
		if (bHasRoutes) {
			// The route rollsign
			hd = hd + "<table bgcolor=#C08000 width=100%><tr>"
				+ "<td colspan = 4 align = center>ROUTE ROLLSIGN</td>"
				+ "</tr><tr>"
				+ "<td width = 10%>" + RSPrev + "</td><td>" + RSCurrent + "</td><td width = 10%>" 
				+ RSNext + "</td><td width = 10%>" + RSList + "</td>";

			if (RTListShown == true)
			{
				hd = hd + "</tr>" + "<tr><td colspan = 4>" + ShowRouteList() + "</td>";				
			}
			
			hd = hd + "</tr></table>";		

			// separator
			hd = hd + "<br><table bgcolor=#000000D0 cellpadding=0 cellspacing=0 border=0 width=100%>"
				+ "<tr height=1><td width=100%></td></tr></table><br>";
		}

		if (bHasDestinations) {
			// The destination rollsign
			hd = hd + "<table bgcolor=#CC6000 width=100%><tr>"
				+ "<td colspan = 4 align = center>DESTINATION ROLLSIGN</td>"
				+ "</tr><tr>"
				+ "<td width = 10%>" + DSPrev + "</td><td>" + DSCurrent + "</td><td width = 10%>" 
				+ DSNext + "</td><td width = 10%>" + DSList + "</td>";
			
			if (DTListShown == true)
			{
				hd = hd + "</tr>" + "<tr><td colspan = 4>" + ShowDestList() + "</td>";				
			}		
				
			hd = hd + "</tr></table>";
		}
		
		// that's it, close the document and present it.
		hd = hd + "</body></html>";
		viewer.LoadHTMLString(hd);
	}
	
	void ViewDetails(Message msg)
	{
		if (msg.dst == me) ViewMyDetails();
	}

	void BrowserClose(Message msg)
	{
		if (msg.src == viewer)
		viewer = null;
	}
	
	public void SetProperties(Soup soup)
	{
		inherited(soup);
		             name3 = soup.GetNamedTag("script.procedencia1");
             name4 = soup.GetNamedTag("script.via1");
             name5 = soup.GetNamedTag("script.observ1");
			 
			 if (name3 != "")	SetFXNameText("NAME03",name3);	else	SetFXNameText("NAME03"," ");
             if (name4 != "")	SetFXNameText("NAME04",name4);	else	SetFXNameText("NAME04"," ");
             if (name5 != "")	SetFXNameText("NAME05",name5);	else	SetFXNameText("NAME05"," ");
		if (bHasRoutes) {
			iCurrentRoute = soup.GetNamedTagAsInt("iCurrentRoute", 0);
			sCurrentRoute = soup.GetNamedTag("sCurrentRoute");
			if (sCurrentRoute == "") {
				sCurrentRoute = Str.Tokens(RTSoup.GetNamedTag(iCurrentRoute),".")[0];
			}
			SetFXTextureReplacement("route-sign", Routes, iCurrentRoute);
		}
		
		if (bHasDestinations) {
			iCurrentDest = soup.GetNamedTagAsInt("iCurrentDest", 0);
			sCurrentDest = Str.Tokens(DTSoup.GetNamedTag(iCurrentDest),".")[0];
			SetFXTextureReplacement("dest-sign", Destinations, iCurrentDest);
		}
	}
	
	public Soup GetProperties(void)
	{
		Soup soup = inherited();

        soup.SetNamedTag("script.procedencia1",name3);
		soup.SetNamedTag("script.via1",name4);
        soup.SetNamedTag("script.observ1",name5);
		if(bHasRoutes) {
			soup.SetNamedTag("iCurrentRoute", iCurrentRoute);
		}
		
		if(bHasDestinations) {
			soup.SetNamedTag("iCurrentDest", iCurrentDest);
				}
		return soup;			

	}
	
	public string GetDescriptionHTML(void)
	{
		string t_name3, t_name4, t_name5;	
		string strRouteSign = "No routes";
		string strDestSign = "No destinations"; 

        if (name3 == "")	{	t_name3 = "<I>Строка не заполнена</I>";	}	else	{	t_name3 = name3;		}
		if (name4 == "")	{	t_name4 = "<I>Строка не заполнена</I>";	}	else	{	t_name4 = name4;                } 
        if (name5 == "")	{	t_name5 = "<I>Строка не заполнена</I>";	}	else	{	t_name5 = name5;		}
		if (bHasRoutes) {
			strRouteSign = "<a href=live://property/route_sign>" + Str.Tokens(RTSoup.GetNamedTag(iCurrentRoute),".")[0] + "</a>";
		}
			
		if (bHasDestinations) {
			strDestSign = "<a href=live://property/dest_sign>" + Str.Tokens(DTSoup.GetNamedTag(iCurrentDest),".")[0] + "</a>";
		}
		
		string head = "<font color=#000000><table bgcolor=#C08000 border=0 bordercolor=#804000 width=100%>";
		
		string html = inherited();
		html = html + "<font color=#FFFFFF>Номер маршрута: </font><font color=#ffe400>";
		html = html + "<a href='live://property/script.procedencia1'>" + t_name3 + "</a></font><br>";
		html = html + "<font color=#FFFFFF>Верхняя строка: </font><font color=#ffe400>";
		html = html + "<a href='live://property/script.via1'>" + t_name4 + "</a></font><br>";
        html = html + "<font color=#FFFFFF>Нижняя строка: </font><font color=#ffe400>";
		html = html + "<a href='live://property/script.observ1'>" + t_name5 + "</a></font><br>";
		
		if ((bHasRoutes == true) or (bHasDestinations == true)) {			
			html = html + head;
		}
		
		if (bHasRoutes == true)
		{
			html = html + "<tr><td>Route Sign: <font size=+3><b>" + strRouteSign + "</b></font></td></tr>";
		}
		
		if (bHasDestinations == true)
		{
			html = html + "<tr><td>Destination Sign: <font size=+3><b>" + strDestSign + "</b></font></td></tr>";
		}	
		
		if ((bHasRoutes == true) or (bHasDestinations == true)) {			
			html = html + "</table>";
		}
		return html;			

	}
	
	public string GetPropertyName(string pID) 
	{
		string result = inherited(pID);		
		if (pID == "route_sign") result = "Select Route";
		else if (pID == "dest_sign") result = "Select Destination";
		return result;
	}
	
	public string GetPropertyType(string pID) 
	{
		string result = inherited(pID);
		if (pID == "route_sign") result = "list";
		else if (pID == "dest_sign") result = "list";
		return result;
   }
   
   
   public string[] GetPropertyElementList(string pID) 
	{
		string[] result = inherited(pID);
		
		if (pID == "route_sign") {
			result[result.size()] = "None";
			Soup soup = Routes.GetConfigSoup().GetNamedSoup("textures");
			int n;
			for (n = 1; n < soup.CountTags(); n++) 
			{
				result[result.size()] = Str.Tokens(soup.GetNamedTag(n),".")[0];
			}
		}
		else if (pID == "dest_sign") {
			result[result.size()] = "None";
			Soup soup = Destinations.GetConfigSoup().GetNamedSoup("textures");
			int n;
			for (n = 1; n < soup.CountTags(); n++) 
			{
				result[result.size()] = Str.Tokens(soup.GetNamedTag(n),".")[0];
			}
		}		
		return result;
	}
	
	public void SetPropertyValue(string pID, string p_ID, string value, int index)
	{
		if (p_ID=="script.procedencia1")	{	name3=value; }
  		if (p_ID=="script.via1")		{	name4=value; }
        if (p_ID=="script.observ1")		{	name5=value; }
		if (pID == "route_sign")
		{
			iCurrentRoute = index;
			SetFXTextureReplacement("route-sign", Routes, iCurrentRoute);
		}
		else if (pID == "dest_sign")
		{
			iCurrentDest = index;
			SetFXTextureReplacement("dest-sign", Destinations, iCurrentDest);
		}
		else inherited(pID, p_ID, value, index); 
	}

	public string GetPropertyValue(string p_ID)
	{
   string result = inherited(p_ID);
  		if (p_ID=="script.procedencia1")	{	return name3;		}
  		if (p_ID=="script.via1")		{	return name4;		}
        if (p_ID=="script.observ1")		{	return name5;		}
				
		  		
   return result;	
	}	

	void QueryRoute(Message msg)
	{
		if(msg) {			
			PostMessage(msg.src, "VehicleRouteAnswer", Str.Tokens(RTSoup.GetNamedTag(iCurrentRoute),".")[0], 0);
		}
	}
	
	void ChangeDestinationSign(Message msg)
	{
		if(msg) {			
			int n;
			for (n = 1; n < DTSoup.CountTags(); n++) 
			{
				if(Str.Tokens(DTSoup.GetNamedTag(n),".")[0] == msg.minor)
				{					
					iCurrentDest = n;
					SetFXTextureReplacement("dest-sign", Destinations, iCurrentDest);			
					Interface.Print("Destination sign was changed to " + iCurrentDest);
					break;
				}
			}
		}		
	}
	
	void WorldNight(Message msg)
	{
		Interface.Log(me.GetName() + " says it's night.");
	}
};