Floor algorithm needs big rework

This commit is contained in:
2024-05-20 01:42:01 -04:00
parent 1cc658c330
commit b67c70aad1
4 changed files with 121 additions and 90 deletions

File diff suppressed because one or more lines are too long

View File

@@ -23,7 +23,7 @@ type Impl_Constructor = {
constructor: Constructor_Fun, constructor: Constructor_Fun,
--Class functions --Class functions
__DetectSensorHit: (self: ClassConstructor, DoorTween1: Tween, DoorTween2: Tween) -> RBXScriptConnection, __DetectSensorHit: (self: ClassConstructor, DoorTween1: Tween, DoorTween2: Tween) -> RBXScriptConnection,
ToggleElevatorDoors: (self: ClassConstructor, opening: boolean, floor: number) -> () ToggleElevatorDoorsAsync: (self: ClassConstructor, opening: boolean, floor: number) -> ()
} & Impl_Static_Props } & Impl_Static_Props
type Constructor_Fun = (FloorDoorsTags: Tags.LandingTags, ElevatorBox: BasePart, ElevatorDoor1: BasePart, ElevatorDoor2: BasePart, ElevatorDoorSensor: Folder) -> ClassConstructor type Constructor_Fun = (FloorDoorsTags: Tags.LandingTags, ElevatorBox: BasePart, ElevatorDoor1: BasePart, ElevatorDoor2: BasePart, ElevatorDoorSensor: Folder) -> ClassConstructor
@@ -72,6 +72,10 @@ Doors.Attributes = {
Doors.Attributes.DoorsOpen.Value = false Doors.Attributes.DoorsOpen.Value = false
Doors.Attributes.DoorsOpen:GetPropertyChangedSignal("Value"):Connect(function()
print("DoorsOpen=",Doors.Attributes.DoorsOpen.Value)
end)
local Attributes = Doors.Attributes local Attributes = Doors.Attributes
function Doors.constructor(FloorDoorsTags, ElevatorBox, ElevatorDoor1, ElevatorDoor2, ElevatorDoorSensor) function Doors.constructor(FloorDoorsTags, ElevatorBox, ElevatorDoor1, ElevatorDoor2, ElevatorDoorSensor)
@@ -103,7 +107,6 @@ end
local init_floors_opened: FloorDoors = {} local init_floors_opened: FloorDoors = {}
local init_floors_closed: FloorDoors = {} local init_floors_closed: FloorDoors = {}
local function DoorsAnimationFloor(FloorDoors: {Instance?}, Floor: number, opening: boolean?, activated_via_censor: boolean?): (Tween?, Tween?) local function DoorsAnimationFloor(FloorDoors: {Instance?}, Floor: number, opening: boolean?, activated_via_censor: boolean?): (Tween?, Tween?)
local Door2Tween_Floor: Tween? local Door2Tween_Floor: Tween?
local Door1Tween_Floor: Tween? local Door1Tween_Floor: Tween?
@@ -249,7 +252,7 @@ end
function Doors:__DetectSensorHit(DoorTween1, DoorTween2) function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
local Step = nil local Step = nil
if Doors.Sensors and not Attributes.DoorsOpen.Value then if Doors.Sensors and Attributes.DoorsOpen.Value then
raycastParams.FilterDescendantsInstances = {self.ElevatorBox, table.unpack(RayIgnoring)} raycastParams.FilterDescendantsInstances = {self.ElevatorBox, table.unpack(RayIgnoring)}
Step = RS.Heartbeat:Connect(function(_dt) Step = RS.Heartbeat:Connect(function(_dt)
@@ -268,7 +271,7 @@ function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
return Step return Step
end end
function Doors:ToggleElevatorDoors(opening, floor) function Doors:ToggleElevatorDoorsAsync(opening, floor)
--short circuiting central --short circuiting central
if opening then if opening then
if not Attributes.DoorsOpen.Value then if not Attributes.DoorsOpen.Value then

View File

@@ -2,7 +2,39 @@
--!native --!native
--!strict --!strict
local RelayAlgorithm = {} type ClassConstructor = typeof(setmetatable({} :: Constructor_Return_Props, {} :: Impl_Constructor))
type Impl_Constructor = {
__index: Impl_Constructor,
constructor: Constructor_Fun,
--Class functions
Sort: (self: ClassConstructor, ElevatorGoingUp: boolean) -> (),
Check: (self: ClassConstructor, ElevatorGoingUp: boolean) -> (),
Insert: (self: ClassConstructor, ElevatorGoingUp: boolean, RequestedLevel: number) -> (),
}
type Constructor_Fun = (BoxAlignPosition: AlignPosition, ElevatorAttributes: ElevatorAttributes, DoorAttributes: DoorAttributes) -> ClassConstructor
type Constructor_Return_Props = {
BoxAlignPosition: AlignPosition,
ElevatorAttributes: ElevatorAttributes,
DoorAttributes: DoorAttributes,
__FloorQueue: Floors
}
type Floors = {number}
type ElevatorAttributes = {
CurrentFloor: IntValue,
GoingUp: BoolValue,
GoalFloor: IntValue
}
type DoorAttributes = {
DoorsOpen: BoolValue
}
export type RelayAlgorithmConstructor = ClassConstructor
local RelayAlgorithm = {} :: Impl_Constructor
RelayAlgorithm.__index = RelayAlgorithm RelayAlgorithm.__index = RelayAlgorithm
function RelayAlgorithm.constructor(BoxAlignPosition, ElevatorAttributes, DoorAttributes) function RelayAlgorithm.constructor(BoxAlignPosition, ElevatorAttributes, DoorAttributes)
@@ -50,12 +82,7 @@ function RelayAlgorithm:Check(ElevatorGoingUp)
table.remove(self.__FloorQueue, 1) table.remove(self.__FloorQueue, 1)
end end
if #self.__FloorQueue ~= 0 then if #self.__FloorQueue ~= 0 then
--local ElevatorGoingUp = ElevatorGoingUpDirection(self.Attributes.CurrentFloor.Value, self.__FloorQueue[1]) self:Sort(ElevatorGoingUp)
--self.Attributes.GoingUp.Value = ElevatorGoingUp
self:SortQueue(ElevatorGoingUp)
--self:__GoToFloor(Leveling[self.__FloorQueue[1]], ElevatorGoingUp)
return true return true
end end
@@ -64,26 +91,7 @@ end
function RelayAlgorithm:Insert(ElevatorGoingUp, RequestedLevel) function RelayAlgorithm:Insert(ElevatorGoingUp, RequestedLevel)
table.insert(self.__FloorQueue, ElevatorGoingUp == self.ElevatorAttributes.GoingUp.Value and 1 or #self.__FloorQueue+1, RequestedLevel) table.insert(self.__FloorQueue, ElevatorGoingUp == self.ElevatorAttributes.GoingUp.Value and 1 or #self.__FloorQueue+1, RequestedLevel)
self:SortQueue(ElevatorGoingUp) self:Sort(ElevatorGoingUp)
end end
function RelayAlgorithm:Proceed(ElevatorGoingUp, RequestedLevel)
self:Insert(RequestedLevel, ElevatorGoingUp)
return self.ElevatorAttributes.GoingUp.Value and not self.DoorAttributes.DoorsOpen.Value
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 return RelayAlgorithm

View File

@@ -35,6 +35,7 @@ local SoundEnums = require(EnumsDir:WaitForChild("Sounds"))
local Leveling = require(script:WaitForChild("Leveling")) local Leveling = require(script:WaitForChild("Leveling"))
local Doors = require(script:WaitForChild("Doors")) local Doors = require(script:WaitForChild("Doors"))
local MovingObjects = require(script:WaitForChild("MovingObjects")) local MovingObjects = require(script:WaitForChild("MovingObjects"))
local RelayAlgorithm = require(script:WaitForChild("RelayAlgorithm"))
local HallDisplays = require(Elevators:WaitForChild("HallDisplays")) local HallDisplays = require(Elevators:WaitForChild("HallDisplays"))
local ElevatorMover = require(Elevators:WaitForChild("Mover")) local ElevatorMover = require(Elevators:WaitForChild("Mover"))
@@ -52,9 +53,13 @@ type Impl_Constructor = {
__index: Impl_Constructor, __index: Impl_Constructor,
constructor: Constructor_Fun, constructor: Constructor_Fun,
--Class functions --Class functions
Leveled: (self: ClassConstructor, RequestedLevel: number) -> (),
Leveling: (self: ClassConstructor, RequestedLevel: number) -> (),
FloorPassingUp: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (),
FloorPassingDown: (self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) -> (),
RequestLevel: (self: ClassConstructor, RequestedLevel: number) -> boolean,
__GoToFloor: (self: ClassConstructor, Level: number, GoingUp: boolean) -> (), __GoToFloor: (self: ClassConstructor, Level: number, GoingUp: boolean) -> (),
__MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean) -> (), __MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean) -> (),
RequestLevelAsync: (self: ClassConstructor, RequestedLevel: number) -> boolean
} & Impl_Static_Props } & Impl_Static_Props
type Impl_Static_Props = { type Impl_Static_Props = {
@@ -112,7 +117,7 @@ type Constructor_Return_Props = {
MachineRoom: MovingObjects.MachineRoom, MachineRoom: MovingObjects.MachineRoom,
HallDisplays: {Instance}, HallDisplays: {Instance},
ButtonsConstructor: Buttons.ButtonsConstructor, ButtonsConstructor: Buttons.ButtonsConstructor,
FloorQueue: {number}, RelayAlgorithmConstructor: RelayAlgorithm.RelayAlgorithmConstructor,
__MovingConnection: RBXScriptConnection?, __MovingConnection: RBXScriptConnection?,
} }
@@ -165,7 +170,7 @@ end
local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe) local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe)
ButtonTree.Prompt.Enabled = false ButtonTree.Prompt.Enabled = false
local Some = self:RequestLevelAsync(ButtonFloor) local Some = self:RequestLevel(ButtonFloor)
if Some then if Some then
local FloorTracker: RBXScriptConnection local FloorTracker: RBXScriptConnection
@@ -292,23 +297,23 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.BoxAlignPosition, self.BoxAlignPosition,
self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Elevator.Responsiveness, Elevator.MaxVelocity) self.BoxAlignOrientation = ElevatorMover(self.ElevatorBox_1960, self.ElevatorBox_1960.Position, Elevator.Responsiveness, Elevator.MaxVelocity)
self.RelayAlgorithmConstructor = RelayAlgorithm.constructor(self.BoxAlignPosition, Attributes, Doors.Attributes)
local ClassConstructor = setmetatable(self, Elevator) local ClassConstructor = setmetatable(self, Elevator)
IterateButtons(ClassConstructor, ButtonsTagsConstructor) IterateButtons(ClassConstructor, ButtonsTagsConstructor)
self.FloorQueue = {}
--Open the elevator doors on server start --Open the elevator doors on server start
task.spawn(function() task.spawn(function()
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value) self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value) self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
end) end)
print(`🔝 {Elevator.Name} initialized and ready`) print(`🔝 {Elevator.Name} initialized and ready`)
return ClassConstructor return ClassConstructor
end end
local function FloorLeveled(self: ClassConstructor, RequestedLevel: number) function Elevator:Leveled(RequestedLevel)
(self.__MovingConnection :: RBXScriptConnection):Disconnect() (self.__MovingConnection :: RBXScriptConnection):Disconnect()
Attributes.Moving.Value = false Attributes.Moving.Value = false
Attributes.CurrentFloor.Value = RequestedLevel Attributes.CurrentFloor.Value = RequestedLevel
@@ -317,15 +322,22 @@ local function FloorLeveled(self: ClassConstructor, RequestedLevel: number)
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value) self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
task.wait(Elevator.QueueWaitTime) task.wait(Elevator.QueueWaitTime)
CheckQueue(self)
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
if self.RelayAlgorithmConstructor:Check(ElevatorGoingUp) then
--More floors in the queue
Attributes.GoingUp.Value = ElevatorGoingUp
self:__GoToFloor(Leveling[self.RelayAlgorithmConstructor.__FloorQueue[1]], ElevatorGoingUp)
end
end end
local function FloorLeveling(self: ClassConstructor, RequestedLevel: number) function Elevator:Leveling(RequestedLevel)
self.BoxAlignPosition.MaxVelocity = 1 self.BoxAlignPosition.MaxVelocity = 1
self.LanternsConstructor:Toggle(true, RequestedLevel) self.LanternsConstructor:Toggle(true, RequestedLevel)
end end
local function FloorPassingUp(self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY>=Leveling[Attributes.CurrentFloor.Value+1] then if ElevatorPositionY>=Leveling[Attributes.CurrentFloor.Value+1] then
Attributes.CurrentFloor.Value+=1 Attributes.CurrentFloor.Value+=1
@@ -334,7 +346,7 @@ local function FloorPassingUp(self: ClassConstructor, ElevatorPositionY: number,
end end
end end
local function FloorPassingDown(self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number) function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY<=Leveling[Attributes.CurrentFloor.Value-1] then if ElevatorPositionY<=Leveling[Attributes.CurrentFloor.Value-1] then
Attributes.CurrentFloor.Value-=1 Attributes.CurrentFloor.Value-=1
@@ -343,7 +355,7 @@ local function FloorPassingDown(self: ClassConstructor, ElevatorPositionY: numbe
end end
end end
function Elevator:__MovingHeartbeat(GoingUp: boolean) function Elevator:__MovingHeartbeat(GoingUp)
local Delta = 0 local Delta = 0
local DoorsOpeningEvent = false local DoorsOpeningEvent = false
@@ -351,9 +363,12 @@ function Elevator:__MovingHeartbeat(GoingUp: boolean)
self.__MovingConnection:Disconnect() self.__MovingConnection:Disconnect()
end end
--Only used for the X and Z axis
local ElevatorPositionInit: Vector3 = self.ElevatorBox_1960.Position
self.__MovingConnection = RS.Heartbeat:Connect(function(_dt) self.__MovingConnection = RS.Heartbeat:Connect(function(_dt)
Delta+=1 Delta+=1
local FloorGoal: number = self.FloorQueue[1] local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1]
Attributes.Moving.Value = true Attributes.Moving.Value = true
Attributes.GoalFloor.Value = FloorGoal Attributes.GoalFloor.Value = FloorGoal
@@ -368,42 +383,44 @@ function Elevator:__MovingHeartbeat(GoingUp: boolean)
--Kill the connection --Kill the connection
if GoingUp then if GoingUp then
FloorPassingUp(self, ElevatorPositionY, FloorGoal) self:FloorPassingUp(ElevatorPositionY, FloorGoal)
if ElevatorPositionY>=BoxAlignY-Elevator.FloorLevelingDistance then if ElevatorPositionY>=BoxAlignY-Elevator.FloorLevelingDistance then
FloorLeveling(self, FloorGoal) self:Leveling(FloorGoal)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningEvent = true DoorsOpeningEvent = true
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, FloorGoal) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal)
end end
end end
if ElevatorPositionY>=BoxAlignY-Elevator.LeveledDistance then if ElevatorPositionY>=BoxAlignY-Elevator.LeveledDistance then
FloorLeveled(self, FloorGoal) self:Leveled(FloorGoal)
end end
else else
FloorPassingDown(self, ElevatorPositionY, FloorGoal) self:FloorPassingDown(ElevatorPositionY, FloorGoal)
if ElevatorPositionY<=BoxAlignY+Elevator.FloorLevelingDistance then if ElevatorPositionY<=BoxAlignY+Elevator.FloorLevelingDistance then
FloorLeveling(self, FloorGoal) self:Leveling(FloorGoal)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningEvent = true DoorsOpeningEvent = true
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, FloorGoal) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal)
end end
end end
if ElevatorPositionY<=BoxAlignY+Elevator.LeveledDistance then if ElevatorPositionY<=BoxAlignY+Elevator.LeveledDistance then
FloorLeveled(self, FloorGoal) self:Leveled(FloorGoal)
end end
end end
self.BoxAlignPosition.Position = Vector3.new(ElevatorPositionInit.X, Leveling[FloorGoal], ElevatorPositionInit.Z)
end) end)
end end
function Elevator:__GoToFloor(GoalLevelVEC, GoingUp) function Elevator:__GoToFloor(GoalLevelVEC, GoingUp)
if Doors.Attributes.DoorsOpen.Value then if Doors.Attributes.DoorsOpen.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoors(false, Attributes.CurrentFloor.Value) self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value)
end end
if GoingUp then if GoingUp then
@@ -413,28 +430,31 @@ function Elevator:__GoToFloor(GoalLevelVEC, GoingUp)
end end
self.MOConstructor:UpdateCFrame() self.MOConstructor:UpdateCFrame()
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
self:__MovingHeartbeat(GoingUp) self:__MovingHeartbeat(GoingUp)
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z) self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z)
end end
function Elevator:RequestLevelAsync(RequestedLevel) function Elevator:RequestLevel(RequestedLevel)
local GoalLevelVEC: number? = Leveling[RequestedLevel] local GoalLevelVEC: number? = Leveling[RequestedLevel]
if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel) local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
if Attributes.Moving.Value then if Attributes.Moving.Value then
ToFloorQueue(self, ElevatorGoingUp, RequestedLevel, GoalLevelVEC) self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
else else
if Doors.Attributes.DoorsOpen then
Attributes.GoingUp.Value = ElevatorGoingUp Attributes.GoingUp.Value = ElevatorGoingUp
if Doors.Attributes.DoorsOpen and #self.FloorQueue == 0 then if Doors.Attributes.DoorsOpen and #self.RelayAlgorithmConstructor.__FloorQueue == 0 then
self:__GoToFloor(GoalLevelVEC, ElevatorGoingUp) self:__GoToFloor(GoalLevelVEC, ElevatorGoingUp)
end end
InsertFloorQueue(self, RequestedLevel, ElevatorGoingUp) self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
else
self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
end
end end
else else
warn(`[{Elevator.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`) warn(`[{Elevator.Name}]: landing out of range or equals the same range as the goal, requested landing: {tostring(RequestedLevel)}`)