// Wolf Rail drivable engine or cab car - put this in the ROOT prim of your engine (HEAD TRUE), or of a cab car at the other // end of a double-ended train (HEAD FALSE: a DMU's second car, a high-speed set's rear power car), with the train's name. // Sit in the cab (the root prim's seat) to drive: with WolfViewer or WolfStorm the Train Cab opens at the bottom of the // screen (power, brake, forward / neutral / reverse, Easy driving, AUTO, POINTS, horn). The region reads the driver's // controls itself and keeps the train from running past the end of its line. Stand up and the train stops - or, in // AUTO (the train drives itself), runs on by itself as a timetable train until its owner stops it (Stop below). // FORWARD always means "towards the front of the cab you sit in", whichever end of the train that is. // At the end of a platform: touch the cab and choose "Change ends", then walk to the cab at the other end and sit in it - // you drive on from there (nothing turns round: the train just goes back out the way it came). // Changing ends doesn't turn the engine round - use a turntable for that. An engine with only one cab in its train (a // shunter driven either way from its one cab) has no "Change ends": drive it back with the reverser (wolf-rail 1.3.7). // Everyone may drive it when PUBLIC is TRUE; otherwise only you, the owner. // Other viewers: touch the cab for Go / Coast / Stop / Auto / Change ends (train.control drives it from the script). // Shunting (wolf-rail 1.3, stage 3): touch for "Couple ahead" / "Couple behind" (the train creeps onto the stock and // couples) and "Uncouple" (the last unit behind the cab is left standing as a rake of its own, named " rake"). // Only the owner, or someone the owner has given the "shunt" grant, may couple or uncouple (the region checks). // Turntable (wolf-rail 1.3.7): stopped at the stop where a road meets a turntable's deck, touch for "Turntable" - the engine // is drawn onto the deck, turned round and drawn off facing the other way. The driver stays in the seat and goes round too. // Rez every car the way it should look. CAB is where this object's cab is in its OWN frame: "front" at its +X end (red // arrow), "rear" at its -X end (a car built turned round with its root along the line), "both". A car rezzed turned round // (nose out at the back) is seen as facing backwards by itself; give FACES "rear" or "front" only to say so outright. // Uses the wolf-rail plugin through wolfCall("train.start" / "train.attach" / "train.driver" / "train.control" / // "train.cab" / "train.ends" / "train.couple" / "train.uncouple" / "train.turntable", ...). Wolf Territories, 2026. Copy, change // and share freely. string TRAIN = "My train"; // the same name in every car; up to 64 letters, no | integer HEAD = TRUE; // TRUE: this object starts the train (its engine); FALSE: a cab car joining it float SPEED = 12.0; // (HEAD) the fastest it may go, metres a second (up to 30) integer TURNBACK = FALSE; // (HEAD) TRUE: with nobody driving, it changes ends by itself at a platform's stop and goes back out string CAB = "front"; // where this object's cab is: "front", "rear" or "both" float GAP = 0.5; // (cab car) metres of space to the car in front of it (0 to 10) string FACES = ""; // (cab car) "" = as it stands; "front" or "rear" to say which way its +X points integer UNIT = FALSE; // (cab car) TRUE: coupled for good to the car in front (a multiple unit's car) integer PUBLIC = FALSE; // TRUE: anyone who sits in the cab may drive integer MENU = -7412; // the touch menu's channel key gDriver; key gCoupleQ; // train.couple's answer comes later (wolfCallAsync) key gCoupleBy; // who asked for it key gTurnQ; // train.turntable's answer comes later too (wolfCallAsync) key gTurnBy; // Who drives it from the Train Cab now (train.cab's first value; NULL_KEY: nobody), and is it moving? (the region lets a // viewer without the cab go after a few seconds and tells them to use this touch menu) list Cab() { return wolfCall("train.cab", [TRAIN]); } key Driver(list cab) { if (cab == []) return NULL_KEY; return llList2Key(cab, 0); } TakeCab(key who) { if (!PUBLIC && who != llGetOwner()) { llRegionSayTo(who, 0, "Only the owner drives this train."); llUnSit(who); return; } // from this object's own cab (the engine, or a cab car of the train: wolf-rail 1.3) if (wolfCall("train.driver", [TRAIN, who]) == []) llRegionSayTo(who, 0, "The cab is not ready: " + wolfGetLastError()); else { gDriver = who; llRegionSayTo(who, 0, "You are driving " + TRAIN + ". Take your time."); } } Ready() { llSitTarget(<0.0, 0.0, 1.0>, ZERO_ROTATION); // the driver's seat (change it to fit your cab) gDriver = NULL_KEY; if (!HEAD) { // a cab car joins the train (its engine may be in another region: the answer comes by dataserver) list opts = [TRAIN, GAP, "cab", CAB, "power", TRUE, "unit", UNIT]; if (FACES != "") opts += ["faces", FACES]; if (wolfCallAsync("train.attach", opts) == NULL_KEY) llOwnerSay("Could not join " + TRAIN + ": " + wolfGetLastError()); return; } // the train must be on the line before anyone can drive it: start it, then hold it on the brake // (train.start also takes the cab back from anyone driving, so a driver still seated is given it again below) if (wolfCall("train.start", [TRAIN, SPEED, "cab", CAB, "turnback", TURNBACK]) == []) { llOwnerSay("Could not start " + TRAIN + ": " + wolfGetLastError()); return; } wolfCall("train.control", [TRAIN, 0.0, 1.0, 1]); key who = llAvatarOnSitTarget(); if (who != NULL_KEY) TakeCab(who); } ChangeEnds(key id) { list cab = Cab(); if (cab != [] && llList2Float(cab, 2) > 0.05) { llRegionSayTo(id, 0, "Stop the train first, then change ends."); return; } // nobody in the Train Cab: this menu's own controls are given back first (train.speed 0: it stays standing) if (Driver(cab) == NULL_KEY) wolfCall("train.speed", [TRAIN, 0.0]); // ["ok", the new head's key, the cab now in use] list r = wolfCall("train.ends", [TRAIN]); if (r == []) llRegionSayTo(id, 0, "Cannot change ends: " + wolfGetLastError()); else if (llList2Key(r, 2) == llGetKey()) llRegionSayTo(id, 0, "Changed ends: this cab now leads."); else llRegionSayTo(id, 0, "Changed ends: walk to the cab at the other end and sit in it to drive on."); } // Source: wolf-rail src/RailRules.cs train.couple [train, "forward" | "back"] (async) and train.uncouple [train, units // from the cab end in use 1-32 or a vehicle's key, the new train's name] (README 1.3.2). Couple(key id, string way) { gCoupleBy = id; gCoupleQ = wolfCallAsync("train.couple", [TRAIN, way]); if (gCoupleQ == NULL_KEY) llRegionSayTo(id, 0, "Cannot couple: " + wolfGetLastError()); else llRegionSayTo(id, 0, "Coupling " + way + ": the train creeps up to the stock. Wait for it."); } Uncouple(key id) { string rake = llGetSubString(TRAIN + " rake", 0, 63); if (wolfCall("train.uncouple", [TRAIN, 1, rake]) == []) llRegionSayTo(id, 0, "Cannot uncouple: " + wolfGetLastError()); else llRegionSayTo(id, 0, "Uncoupled: the last unit stands as \"" + rake + "\"."); } // Source: wolf-rail src/Turntable.cs train.turntable [train, turntable name, road 1-32 or "opposite"] (async: "ok" or // "error: why"); train.cab's 17th value (index 16, wolf-rail 1.3.7) is the turntable whose deck-edge stop the engine stands // at, "" for none (an older region sends no 17th value: "" too). With a driver aboard the call counts as their own press. Turntable(key id) { list cab = Cab(); if (cab == []) { llRegionSayTo(id, 0, "Cannot turn: " + wolfGetLastError()); return; } if (llList2Float(cab, 2) > 0.05) { llRegionSayTo(id, 0, "Stop the train first, then turn it."); return; } string tt = llList2String(cab, 16); if (tt == "") { llRegionSayTo(id, 0, "Drive up to the stop where the road meets a turntable first, then choose Turntable."); return; } // nobody in the Train Cab: this menu's own controls are given back first (train.speed 0: it stays standing) if (Driver(cab) == NULL_KEY) wolfCall("train.speed", [TRAIN, 0.0]); gTurnBy = id; gTurnQ = wolfCallAsync("train.turntable", [TRAIN, tt, "opposite"]); if (gTurnQ == NULL_KEY) llRegionSayTo(id, 0, "Cannot turn: " + wolfGetLastError()); else llRegionSayTo(id, 0, "Turning " + TRAIN + " round on the turntable " + tt + ". Stay in your seat: you go round with it."); } default { state_entry() { Ready(); llListen(MENU, "", NULL_KEY, ""); } on_rez(integer p) { llResetScript(); } changed(integer c) { if (c & CHANGED_REGION_START) Ready(); if (!(c & CHANGED_LINK)) return; key who = llAvatarOnSitTarget(); if (who != NULL_KEY && who != gDriver) TakeCab(who); else if (who == NULL_KEY && gDriver != NULL_KEY) gDriver = NULL_KEY; // the region has already let the cab go: the train stops (in Auto: it runs on by itself) } // (cab car) "ok|" or "error: " dataserver(key q, string data) { if (q == gCoupleQ) { gCoupleQ = NULL_KEY; if (llGetSubString(data, 0, 5) == "error:") llRegionSayTo(gCoupleBy, 0, "Cannot couple: " + llGetSubString(data, 7, -1)); else llRegionSayTo(gCoupleBy, 0, "Coupled."); return; } if (q == gTurnQ) { gTurnQ = NULL_KEY; if (llGetSubString(data, 0, 5) == "error:") llRegionSayTo(gTurnBy, 0, "Cannot turn: " + llGetSubString(data, 7, -1)); else llRegionSayTo(gTurnBy, 0, "Turned: " + TRAIN + " now faces the other way. Take the power to drive off."); return; } if (llGetSubString(data, 0, 5) == "error:") llOwnerSay(TRAIN + ": " + llGetSubString(data, 7, -1)); } touch_start(integer n) { key t = llDetectedKey(0); list cab = Cab(); key drv = Driver(cab); // train.cab's 18th value (index 17, wolf-rail 1.3.7): 1 when only one object of the train has a cab - no "Change ends" list ends = ["Change ends"]; if (llList2Integer(cab, 17) == 1) ends = []; if (drv != NULL_KEY) { // someone drives from the Train Cab: only they get the touch menu (the cab has the same buttons in WolfViewer and // WolfStorm: CHANGE ENDS, COUPLE, UNCOUPLE, TURNTABLE) if (t == drv) llDialog(t, "Drive " + TRAIN + " from here", ends + ["Couple ahead", "Couple behind", "Uncouple", "Turntable"], MENU); return; } if (!PUBLIC && t != llGetOwner()) return; llDialog(t, "Drive " + TRAIN + " from here", ["Go", "Coast", "Stop", "Auto"] + ends + ["Couple ahead", "Couple behind", "Uncouple", "Turntable"], MENU); } listen(integer ch, string name, key id, string msg) { key drv = Driver(Cab()); if (msg == "Change ends") { if (drv == id || (drv == NULL_KEY && (PUBLIC || id == llGetOwner()))) ChangeEnds(id); return; } if (msg == "Turntable") { // the driver, or (nobody driving) the owner / anyone when PUBLIC (as Change ends) if (drv == id || (drv == NULL_KEY && (PUBLIC || id == llGetOwner()))) Turntable(id); return; } if (msg == "Couple ahead" || msg == "Couple behind" || msg == "Uncouple") { // the driver, or (nobody driving) the owner / anyone when PUBLIC; the region also checks the shunt grant if (!(drv == id || (drv == NULL_KEY && (PUBLIC || id == llGetOwner())))) return; if (msg == "Couple ahead") Couple(id, "forward"); else if (msg == "Couple behind") Couple(id, "back"); else Uncouple(id); return; } if ((!PUBLIC && id != llGetOwner()) || drv != NULL_KEY) return; // [train, power 0-1, brake 0-1, reverser -1 / 0 / 1]: the line's end is always protected if (msg == "Go") wolfCall("train.control", [TRAIN, 1.0, 0.0, 1]); // full power, up to SPEED else if (msg == "Coast") wolfCall("train.control", [TRAIN, 0.0, 0.0, 1]); // keeps its speed else if (msg == "Stop") wolfCall("train.control", [TRAIN, 0.0, 1.0, 1]); // full brake else if (msg == "Auto") wolfCall("train.control", [TRAIN, "auto"]); // drives itself: stations, signals // [driver, mode, speed, line speed, power, brake, reverser, next stop, its metres, line ahead, protecting, lead, ends, ...] list cab = Cab(); if (cab != []) llRegionSayTo(id, 0, TRAIN + ": " + (string)llRound(llList2Float(cab, 2) * 3.6) + " km/h"); } }