RelayAlgorithm

This commit is contained in:
2024-05-18 01:37:45 -04:00
parent 51ce3d1c98
commit 32f21862a5
2 changed files with 97 additions and 67 deletions

View File

@@ -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 a<b
else
return a>b
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

View File

@@ -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 a<b
else
return a>b
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)}`)