From 32f21862a5c6412c35260edaa73c0c5c8b036b59 Mon Sep 17 00:00:00 2001 From: unittensor Date: Sat, 18 May 2024 01:37:45 -0400 Subject: [PATCH] RelayAlgorithm --- .../Elevators/Otis1960/RelayAlgorithm.lua | 96 +++++++++++++++++++ src/server/main/Elevators/Otis1960/init.lua | 68 +------------ 2 files changed, 97 insertions(+), 67 deletions(-) create mode 100644 src/server/main/Elevators/Otis1960/RelayAlgorithm.lua diff --git a/src/server/main/Elevators/Otis1960/RelayAlgorithm.lua b/src/server/main/Elevators/Otis1960/RelayAlgorithm.lua new file mode 100644 index 0000000..ee278c8 --- /dev/null +++ b/src/server/main/Elevators/Otis1960/RelayAlgorithm.lua @@ -0,0 +1,96 @@ +--!optimize 2 +--!native +--!strict + +local RelayAlgorithm = {} +RelayAlgorithm.__index = RelayAlgorithm + +function RelayAlgorithm.constructor(BoxAlignPosition, ElevatorAttributes) + return setmetatable({ + BoxAlignPosition = BoxAlignPosition, + Attributes = ElevatorAttributes, + FloorQueue = {} + }, RelayAlgorithm) +end + +--The Otis relay based call logic +--https://youtu.be/BCN9mQOT3RQ + +--Sort the queue based on direction +--[[ + Up: { + [1] = 5, + [2] = 6, + [3] = 7, + [4] = 8, + [5] = 9, + [6] = 10 + } + Down: { + [1] = 5, + [2] = 3, + [3] = 2, + [4] = 1 + } +]] +function RelayAlgorithm:SortQueue(ElevatorGoingUp) + table.sort(self.FloorQueue, function(a: number, b: number): boolean + if ElevatorGoingUp then + return ab + end + end) + print(table.unpack(self.FloorQueue)) +end + +function RelayAlgorithm:CheckQueue() + if self.FloorQueue[1] == self.Attributes.CurrentFloor.Value then + table.remove(self.FloorQueue, 1) + end + if #self.FloorQueue ~= 0 then + local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, self.FloorQueue[1]) + self.Attributes.GoingUp.Value = ElevatorGoingUp + + self:SortQueue(self, ElevatorGoingUp) + --self:__GoToFloor(Leveling[self.FloorQueue[1]], ElevatorGoingUp) + + return true + end + + return false +end + +--[==[ + +local function CheckQueue(self: ClassConstructor) + if self.FloorQueue[1] == Attributes.CurrentFloor.Value then + table.remove(self.FloorQueue, 1) + end + if #self.FloorQueue ~= 0 then + local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, self.FloorQueue[1]) + Attributes.GoingUp.Value = ElevatorGoingUp + + SortQueue(self, ElevatorGoingUp) + self:__GoToFloor(Leveling[self.FloorQueue[1]], ElevatorGoingUp) + end +end + +local function InsertFloorQueue(self: ClassConstructor, RequestedLevel: number, ElevatorGoingUp: boolean) + table.insert(self.FloorQueue, ElevatorGoingUp == Attributes.GoingUp.Value and 1 or #self.FloorQueue+1, RequestedLevel) + SortQueue(self, ElevatorGoingUp) +end + +local function ToFloorQueue(self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number, GoalLevelVEC: number) + local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position + + InsertFloorQueue(self, RequestedLevel, ElevatorGoingUp) + + if ElevatorGoingUp == Attributes.GoingUp.Value and not Doors.Attributes.DoorsOpen.Value then + self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, self.FloorQueue[1], ElevatorBoxCurrentPos.Z) + end +end + +]==]-- + +return RelayAlgorithm \ No newline at end of file diff --git a/src/server/main/Elevators/Otis1960/init.lua b/src/server/main/Elevators/Otis1960/init.lua index 855523b..6a010eb 100644 --- a/src/server/main/Elevators/Otis1960/init.lua +++ b/src/server/main/Elevators/Otis1960/init.lua @@ -308,69 +308,6 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin return ClassConstructor end ---The Otis relay based call logic ---https://youtu.be/BCN9mQOT3RQ - ---Sort the queue based on direction ---[[ - Up: { - [1] = 5, - [2] = 6, - [3] = 7, - [4] = 8, - [5] = 9, - [6] = 10 - } - Down: { - [1] = 5, - [2] = 3, - [3] = 2, - [4] = 1 - } -]] -local function SortQueue(self: ClassConstructor, ElevatorGoingUp: boolean) - table.sort(self.FloorQueue, function(a: number, b: number): boolean - if ElevatorGoingUp then - return ab - end - end) - print(table.unpack(self.FloorQueue)) -end - -local function CheckQueue(self: ClassConstructor) - if self.FloorQueue[1] == Attributes.CurrentFloor.Value then - table.remove(self.FloorQueue, 1) - end - if #self.FloorQueue ~= 0 then - local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, self.FloorQueue[1]) - Attributes.GoingUp.Value = ElevatorGoingUp - - SortQueue(self, ElevatorGoingUp) - self:__GoToFloor(Leveling[self.FloorQueue[1]], ElevatorGoingUp) - end -end - -local function InsertFloorQueue(self: ClassConstructor, FirstIndex: boolean, RequestedLevel: number, ElevatorGoingUp: boolean) - table.insert(self.FloorQueue, FirstIndex and 1 or #self.FloorQueue+1, RequestedLevel) - SortQueue(self, ElevatorGoingUp) -end - -local function ToFloorQueue(self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number, GoalLevelVEC: number) - local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position - - if ElevatorGoingUp then - InsertFloorQueue(self, true, RequestedLevel, ElevatorGoingUp) - - if not Doors.Attributes.DoorsOpen.Value then - self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z) - end - else - InsertFloorQueue(self, false, RequestedLevel, ElevatorGoingUp) - end -end - local function FloorLeveled(self: ClassConstructor, RequestedLevel: number) (self.__MovingConnection :: RBXScriptConnection):Disconnect() Attributes.Moving.Value = false @@ -495,12 +432,9 @@ function Elevator:RequestLevelAsync(RequestedLevel) Attributes.GoingUp.Value = ElevatorGoingUp if Doors.Attributes.DoorsOpen and #self.FloorQueue == 0 then - InsertFloorQueue(self, true, RequestedLevel, ElevatorGoingUp) - self:__GoToFloor(GoalLevelVEC, ElevatorGoingUp) - else - InsertFloorQueue(self, false, RequestedLevel, ElevatorGoingUp) end + InsertFloorQueue(self, RequestedLevel, ElevatorGoingUp) end else warn(`[{Elevator.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`)