From c629661af216d4fdfcd7035e6371b0637c32bf7e Mon Sep 17 00:00:00 2001 From: Maikel de Vries Date: Sat, 31 Mar 2018 10:44:36 +0200 Subject: [PATCH] cable reel: fix connecting, max cable length, ensure straight cables --- .../CableReel.ocd/CableLine.ocd/Script.c | 48 +++++++++- .../CableLine.ocd/StringTblDE.txt | 4 +- .../CableLine.ocd/StringTblUS.txt | 4 +- .../Items.ocd/CableReel.ocd/DefCore.txt | 2 +- .../Items.ocd/CableReel.ocd/Script.c | 96 ++++++++++++++++--- .../Items.ocd/CableReel.ocd/StringTblDE.txt | 10 +- .../Items.ocd/CableReel.ocd/StringTblUS.txt | 10 +- .../CableLorrys.ocs/Objects.c | 4 +- .../Experimental.ocf/CableLorrys.ocs/Script.c | 2 +- 9 files changed, 144 insertions(+), 36 deletions(-) diff --git a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/Script.c b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/Script.c index 13177d2e6..7169e987a 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/Script.c +++ b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/Script.c @@ -42,14 +42,52 @@ public func GetConnectedObject(object obj) } +/*-- Cable Changes --*/ + +public func Destruction() +{ + if (GetActionTarget(0)) GetActionTarget(0)->~OnCableLineRemoval(); + if (GetActionTarget(1)) GetActionTarget(1)->~OnCableLineRemoval(); + return; +} + +public func OnLineChange() +{ + // Notify action targets about line change. + var act1 = GetActionTarget(0); + var act2 = GetActionTarget(1); + if (act1) act1->~OnCableLengthChange(this); + if (act2) act2->~OnCableLengthChange(this); + // Break line if it is too long or if it is bend. + if (GetVertexNum() > 2 || GetCableLength() > this.CableMaxLength) + { + OnLineBreak(); + RemoveObject(); + } + return; +} + +// Returns the length between all the vertices. +public func GetCableLength() +{ + var current_length = 0; + for (var index = 0; index < GetVertexNum() - 1; index++) + current_length += Distance(GetVertex(index, VTX_X), GetVertex(index, VTX_Y), GetVertex(index + 1, VTX_X), GetVertex(index + 1, VTX_Y)); + return current_length; +} + + /*-- Breaking --*/ public func OnLineBreak(bool no_msg) { - Sound("Objects::Connect"); + Sound("Objects::LineSnap"); var act1 = GetActionTarget(0); var act2 = GetActionTarget(1); + if (!no_msg) + BreakMessage(); + SetAction("Idle"); if (act1) { @@ -61,19 +99,18 @@ public func OnLineBreak(bool no_msg) act2->~CableDeactivation(activations); act2->~RemoveCableConnection(this); } - if (!no_msg) - BreakMessage(); } public func BreakMessage() { var line_end = GetActionTarget(0); - if (!line_end || line_end->GetID() != CableLorryReel) + + if (!line_end || line_end->GetID() != CableReel) line_end = GetActionTarget(1); if (line_end && line_end->Contained()) line_end = line_end->Contained(); if (line_end) - line_end->Message("$TxtLinebroke$"); + line_end->Message("$MsgCableBroke$"); return; } @@ -132,3 +169,4 @@ local ActMap = { }; local Name = "$Name$"; +local CableMaxLength = 500; diff --git a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblDE.txt b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblDE.txt index 77057707a..c3d894caa 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblDE.txt +++ b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblDE.txt @@ -1,2 +1,2 @@ -TxtLinebroke=Seil gerissen -Name=Seilbahnseil +MsgCableBroke=Seil gerissen +Name=Seilbahnseil \ No newline at end of file diff --git a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblUS.txt b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblUS.txt index dcb92a5c9..6ce7a86f3 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblUS.txt +++ b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/CableLine.ocd/StringTblUS.txt @@ -1,2 +1,2 @@ -TxtLinebroke=Line broke -Name=Cable line \ No newline at end of file +MsgCableBroke=Cable broke +Name=Cable \ No newline at end of file diff --git a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/DefCore.txt b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/DefCore.txt index 930b910a8..9f3a196fb 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/DefCore.txt +++ b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/DefCore.txt @@ -1,5 +1,5 @@ [DefCore] -id=CableLorryReel +id=CableReel Version=8,0 Category=C4D_Object Width=8 diff --git a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/Script.c b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/Script.c index dcb9af3e6..4b253ce43 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/Script.c +++ b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/Script.c @@ -5,13 +5,19 @@ @author Clonkonaut */ -protected func Hit() + +#include Library_Stackable + + +public func Hit() { Sound("Hits::Materials::Rock::RockHit?"); } public func IsToolProduct() { return true; } +public func MaxStackCount() { return 4; } + /*-- Line connection --*/ @@ -27,41 +33,59 @@ protected func ControlUse(object clonk, int x, int y) // There already is a cable if (line) { + // Find other connected crossing. + var other_obj = nil; + if (line->GetActionTarget(0) == this) + other_obj = line->GetActionTarget(1); + else if(line->GetActionTarget(1) == this) + other_obj = line->GetActionTarget(0); + else + return FatalError("Cable reel: line not connected to reel while trying to connect crossings."); + // Cable is already connected to obj -> remove cable. if (obj == line->GetActionTarget(0) || obj == line->GetActionTarget(1)) { - // Cable is already connected to obj -> remove line. line->RemoveObject(); + OnCableLengthChange(); Sound("Objects::Connect"); - clonk->Message("$TxtLineRemoval$"); + clonk->Message("$TxtCableRemoval$"); return true; } + // Check if this connection does not already exist. + else if (AreDirectlyConnectedCrossings(obj, other_obj)) + { + clonk->Message("$TxtDoubleCable$"); + } + // Create new connection. else { - // Connect existing power line to obj. - if (line->GetActionTarget(0) == this) - line->SetActionTargets(obj, line->GetActionTarget(1)); - else if(line->GetActionTarget(1) == this) - line->SetActionTargets(line->GetActionTarget(0), obj); - else - return; + line->SetActionTargets(obj, other_obj); Sound("Objects::Connect"); obj->AddCableConnection(line); + OnCableLengthChange(); clonk->Message("$TxtConnect$", obj->GetName()); - //RemoveObject(); + DoStackCount(-1); return true; } } - else // A new cable needs to be created. + // A new cable needs to be created. + else { line = CreateObjectAbove(CableLine, 0, 0, NO_OWNER); line->SetActionTargets(this, obj); + OnCableLengthChange(); Sound("Objects::Connect"); clonk->Message("$TxtConnect$", obj->GetName()); return true; } } -// Finds all power lines connected to obj (can be nil in local calls). +// Returns whether c1 and c2 are already directly connected by a cable. +private func AreDirectlyConnectedCrossings(object c1, object c2) +{ + return !!FindObject(Find_Func("IsConnectedTo", c1), Find_Func("IsConnectedTo", c2)); +} + +// Finds all cables connected to obj (can be nil in local calls). private func Find_CableLine(object obj) { if (!obj) @@ -70,6 +94,52 @@ private func Find_CableLine(object obj) } +/*-- Inventory --*/ + +public func OnCableLineRemoval() +{ + OnCableLengthChange(); + return; +} + +public func OnCableLengthChange() +{ + // Update usage bar for a possible carrier (the clonk). + var carrier = Contained(); + if (carrier) + carrier->~OnInventoryChange(); + return; +} + +// Display the line length bar over the pipe icon. +public func GetInventoryIconOverlay() +{ + var line = FindObject(Find_CableLine()); + if (!line) return; + + var percentage = 100 * line->GetCableLength() / line.CableMaxLength; + var red = percentage * 255 / 100; + var green = 255 - red; + // Overlay a usage bar. + var overlay = + { + Bottom = "0.75em", + Margin = ["0.1em", "0.25em"], + BackgroundColor = RGB(0, 0, 0), + margin = + { + Margin = "0.05em", + bar = + { + BackgroundColor = RGB(red, green, 0), + Right = Format("%d%%", percentage), + } + } + }; + return overlay; +} + + /*-- Properties --*/ local Name = "$Name$"; diff --git a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblDE.txt b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblDE.txt index c8bf9e622..e7d536971 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblDE.txt +++ b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblDE.txt @@ -1,6 +1,6 @@ -TxtConnectLine=Leitung verbinden -TxtNoNewLine=Hier kann keine neue Leitung verlegt werden. -TxtLineRemoval=Stromleitung abgenommen. -TxtConnect=Stromleitung verbunden|mit %s Name=Kabelspule -Description=Drücke [Benutzen] vor einem Gebäude und ein weiteres Mal vor einem anderen Gebäude, um diese beiden mit einem Stromkabel zu verbinden. \ No newline at end of file +Description=Drücke [Benutzen] vor einer Seilbahnstation und ein weiteres Mal vor einer anderer Seilbahnstation, um diese beiden mit einem Seil zu verbinden. Die Stationen können nur mit einem geraden Seil verbunden werden. +TxtNoNewLine=Hier kann kein neues Seil aufgehängt werden. +TxtCableRemoval=Seil abnehmen. +TxtDoubleCable=Stationen sind schon verbunden. +TxtConnect=Seil verbunden|mit %s \ No newline at end of file diff --git a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblUS.txt b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblUS.txt index 81b3c1f03..b02967ada 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblUS.txt +++ b/planet/Experimental.ocf/CableLorrys.ocs/CableCars.ocd/Items.ocd/CableReel.ocd/StringTblUS.txt @@ -1,6 +1,6 @@ -TxtConnectLine=Connect line -TxtNoNewLine=Cannot create a new line here. -TxtLineRemoval=Power line disconnected. -TxtConnect=Power line connected|to %s Name=Cable reel -Description=Press [Use] in front of a structure and another time in front of another structure to connect both with a power line. \ No newline at end of file +Description=Press [Use] in front of a cable station and another time in front of another cable station to connect both with a cable. The cable stations may only be connected with a straight cable. +TxtNoNewLine=Cannot create a new cable here. +TxtCableRemoval=Cable disconnected. +TxtDoubleCable=Stations are already connected. +TxtConnect=Cable connected|to %s \ No newline at end of file diff --git a/planet/Experimental.ocf/CableLorrys.ocs/Objects.c b/planet/Experimental.ocf/CableLorrys.ocs/Objects.c index 00eece8d2..dfe0e86b6 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/Objects.c +++ b/planet/Experimental.ocf/CableLorrys.ocs/Objects.c @@ -160,7 +160,7 @@ func InitializeObjects() var Metal001 = ToolsWorkshop001->CreateContents(Metal); Metal001->SetPosition(76, 369); - var CableLorryReel001 = CreateObjectAbove(CableLorryReel, 163, 386); - CableLorryReel001->Unstick(7); + var CableReel001 = CreateObjectAbove(CableReel, 163, 386); + CableReel001->Unstick(7); return true; } diff --git a/planet/Experimental.ocf/CableLorrys.ocs/Script.c b/planet/Experimental.ocf/CableLorrys.ocs/Script.c index 5e9784005..b1350121a 100644 --- a/planet/Experimental.ocf/CableLorrys.ocs/Script.c +++ b/planet/Experimental.ocf/CableLorrys.ocs/Script.c @@ -49,7 +49,7 @@ protected func Initialize() chest->CreateContents(Dynamite, 4); chest->CreateContents(Shovel, 2); chest->CreateContents(Hammer, 2); - chest->CreateContents(CableLorryReel, 2); + chest->CreateContents(CableReel, 2); // Tool workshop on the little mountain. var tools = CreateObjectAbove(ToolsWorkshop, 540, 260);