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

@@ -22,8 +22,8 @@ type Impl_Constructor = {
__index: Impl_Constructor,
constructor: Constructor_Fun,
--Class functions
__DetectSensorHit: (self: ClassConstructor, DoorTween1: Tween, DoorTween2: Tween) -> RBXScriptConnection,
ToggleElevatorDoors: (self: ClassConstructor, opening: boolean, floor: number) -> ()
__DetectSensorHit: (self: ClassConstructor, DoorTween1: Tween, DoorTween2: Tween) -> RBXScriptConnection,
ToggleElevatorDoorsAsync: (self: ClassConstructor, opening: boolean, floor: number) -> ()
} & Impl_Static_Props
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:GetPropertyChangedSignal("Value"):Connect(function()
print("DoorsOpen=",Doors.Attributes.DoorsOpen.Value)
end)
local Attributes = Doors.Attributes
function Doors.constructor(FloorDoorsTags, ElevatorBox, ElevatorDoor1, ElevatorDoor2, ElevatorDoorSensor)
@@ -103,7 +107,6 @@ end
local init_floors_opened: FloorDoors = {}
local init_floors_closed: FloorDoors = {}
local function DoorsAnimationFloor(FloorDoors: {Instance?}, Floor: number, opening: boolean?, activated_via_censor: boolean?): (Tween?, Tween?)
local Door2Tween_Floor: Tween?
local Door1Tween_Floor: Tween?
@@ -249,7 +252,7 @@ end
function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
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)}
Step = RS.Heartbeat:Connect(function(_dt)
@@ -268,7 +271,7 @@ function Doors:__DetectSensorHit(DoorTween1, DoorTween2)
return Step
end
function Doors:ToggleElevatorDoors(opening, floor)
function Doors:ToggleElevatorDoorsAsync(opening, floor)
--short circuiting central
if opening then
if not Attributes.DoorsOpen.Value then

View File

@@ -2,7 +2,39 @@
--!native
--!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
function RelayAlgorithm.constructor(BoxAlignPosition, ElevatorAttributes, DoorAttributes)
@@ -50,12 +82,7 @@ function RelayAlgorithm:Check(ElevatorGoingUp)
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(ElevatorGoingUp)
--self:__GoToFloor(Leveling[self.__FloorQueue[1]], ElevatorGoingUp)
self:Sort(ElevatorGoingUp)
return true
end
@@ -64,26 +91,7 @@ end
function RelayAlgorithm:Insert(ElevatorGoingUp, RequestedLevel)
table.insert(self.__FloorQueue, ElevatorGoingUp == self.ElevatorAttributes.GoingUp.Value and 1 or #self.__FloorQueue+1, RequestedLevel)
self:SortQueue(ElevatorGoingUp)
self:Sort(ElevatorGoingUp)
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

View File

@@ -32,9 +32,10 @@ local ButtonTags = require(TagsDir:WaitForChild("Buttons"))
local Enums = require(Storage:WaitForChild("Enums"))
local SoundEnums = require(EnumsDir:WaitForChild("Sounds"))
local Leveling = require(script:WaitForChild("Leveling"))
local Doors = require(script:WaitForChild("Doors"))
local MovingObjects = require(script:WaitForChild("MovingObjects"))
local Leveling = require(script:WaitForChild("Leveling"))
local Doors = require(script:WaitForChild("Doors"))
local MovingObjects = require(script:WaitForChild("MovingObjects"))
local RelayAlgorithm = require(script:WaitForChild("RelayAlgorithm"))
local HallDisplays = require(Elevators:WaitForChild("HallDisplays"))
local ElevatorMover = require(Elevators:WaitForChild("Mover"))
@@ -52,9 +53,13 @@ type Impl_Constructor = {
__index: Impl_Constructor,
constructor: Constructor_Fun,
--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) -> (),
__MovingHeartbeat: (self: ClassConstructor, GoingUp: boolean) -> (),
RequestLevelAsync: (self: ClassConstructor, RequestedLevel: number) -> boolean
} & Impl_Static_Props
type Impl_Static_Props = {
@@ -90,30 +95,30 @@ type Impl_Static_Props = {
type Constructor_Fun = (TagsConstructor: TagsConstructor, ButtonsTags: Tags.ElevatorButtons, LanternsTags: Tags.Lanterns, LandingDoors: Tags.LandingTags) -> ClassConstructor
type Constructor_Return_Props = {
Tags: Tags,
MOConstructor: MovingObjects.MovingObjectsConstructor,
LanternsConstructor: Lanterns.LanternsConstructor,
HallDisplaysConstructor: HallDisplays.HallDisplaysConstructor,
ElevatorBox_1960: UnionOperation,
ElevatorDoor1: BasePart,
ElevatorDoor2: BasePart,
ElevatorDoorSensor: Folder,
BoxAttachment: Attachment,
BoxAlignPosition: AlignPosition,
BoxAlignOrientation: AlignOrientation,
ElevatorDoorsConstructor: Doors.DoorConstructor,
Ropes: {Instance},
TractionRopesConstructor: TractionRopes.TractionRopesConstructor,
Pulley: UnionOperation,
Pulley2: UnionOperation,
Governor: UnionOperation,
GovernorFlyballs: Part,
PieplatePulley: UnionOperation,
MachineRoom: MovingObjects.MachineRoom,
HallDisplays: {Instance},
ButtonsConstructor: Buttons.ButtonsConstructor,
FloorQueue: {number},
__MovingConnection: RBXScriptConnection?,
Tags: Tags,
MOConstructor: MovingObjects.MovingObjectsConstructor,
LanternsConstructor: Lanterns.LanternsConstructor,
HallDisplaysConstructor: HallDisplays.HallDisplaysConstructor,
ElevatorBox_1960: UnionOperation,
ElevatorDoor1: BasePart,
ElevatorDoor2: BasePart,
ElevatorDoorSensor: Folder,
BoxAttachment: Attachment,
BoxAlignPosition: AlignPosition,
BoxAlignOrientation: AlignOrientation,
ElevatorDoorsConstructor: Doors.DoorConstructor,
Ropes: {Instance},
TractionRopesConstructor: TractionRopes.TractionRopesConstructor,
Pulley: UnionOperation,
Pulley2: UnionOperation,
Governor: UnionOperation,
GovernorFlyballs: Part,
PieplatePulley: UnionOperation,
MachineRoom: MovingObjects.MachineRoom,
HallDisplays: {Instance},
ButtonsConstructor: Buttons.ButtonsConstructor,
RelayAlgorithmConstructor: RelayAlgorithm.RelayAlgorithmConstructor,
__MovingConnection: RBXScriptConnection?,
}
local Elevator = {} :: Impl_Constructor
@@ -165,7 +170,7 @@ end
local function _ActivatedFloorButton(self: ClassConstructor, ButtonFloor: number, ButtonTree: Tags.ButtonPropertiesSafe)
ButtonTree.Prompt.Enabled = false
local Some = self:RequestLevelAsync(ButtonFloor)
local Some = self:RequestLevel(ButtonFloor)
if Some then
local FloorTracker: RBXScriptConnection
@@ -292,23 +297,23 @@ function Elevator.constructor(TagsConstructor, ButtonsTags, LanternsTags, Landin
self.BoxAlignPosition,
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)
IterateButtons(ClassConstructor, ButtonsTagsConstructor)
self.FloorQueue = {}
--Open the elevator doors on server start
task.spawn(function()
self.LanternsConstructor:Toggle(true, Attributes.CurrentFloor.Value)
self.HallDisplaysConstructor:SetHallDisplays(Attributes.CurrentFloor.Value)
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, Attributes.CurrentFloor.Value)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, Attributes.CurrentFloor.Value)
end)
print(`🔝 {Elevator.Name} initialized and ready`)
return ClassConstructor
end
local function FloorLeveled(self: ClassConstructor, RequestedLevel: number)
function Elevator:Leveled(RequestedLevel)
(self.__MovingConnection :: RBXScriptConnection):Disconnect()
Attributes.Moving.Value = false
Attributes.CurrentFloor.Value = RequestedLevel
@@ -317,15 +322,22 @@ local function FloorLeveled(self: ClassConstructor, RequestedLevel: number)
self.LanternsConstructor:Reset(Attributes.CurrentFloor.Value)
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
local function FloorLeveling(self: ClassConstructor, RequestedLevel: number)
function Elevator:Leveling(RequestedLevel)
self.BoxAlignPosition.MaxVelocity = 1
self.LanternsConstructor:Toggle(true, RequestedLevel)
end
local function FloorPassingUp(self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number)
function Elevator:FloorPassingUp(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY>=Leveling[Attributes.CurrentFloor.Value+1] then
Attributes.CurrentFloor.Value+=1
@@ -334,7 +346,7 @@ local function FloorPassingUp(self: ClassConstructor, ElevatorPositionY: number,
end
end
local function FloorPassingDown(self: ClassConstructor, ElevatorPositionY: number, RequestedLevel: number)
function Elevator:FloorPassingDown(ElevatorPositionY, RequestedLevel)
if ElevatorPositionY<=Leveling[Attributes.CurrentFloor.Value-1] then
Attributes.CurrentFloor.Value-=1
@@ -343,7 +355,7 @@ local function FloorPassingDown(self: ClassConstructor, ElevatorPositionY: numbe
end
end
function Elevator:__MovingHeartbeat(GoingUp: boolean)
function Elevator:__MovingHeartbeat(GoingUp)
local Delta = 0
local DoorsOpeningEvent = false
@@ -351,9 +363,12 @@ function Elevator:__MovingHeartbeat(GoingUp: boolean)
self.__MovingConnection:Disconnect()
end
--Only used for the X and Z axis
local ElevatorPositionInit: Vector3 = self.ElevatorBox_1960.Position
self.__MovingConnection = RS.Heartbeat:Connect(function(_dt)
Delta+=1
local FloorGoal: number = self.FloorQueue[1]
local FloorGoal: number = self.RelayAlgorithmConstructor.__FloorQueue[1]
Attributes.Moving.Value = true
Attributes.GoalFloor.Value = FloorGoal
@@ -368,42 +383,44 @@ function Elevator:__MovingHeartbeat(GoingUp: boolean)
--Kill the connection
if GoingUp then
FloorPassingUp(self, ElevatorPositionY, FloorGoal)
self:FloorPassingUp(ElevatorPositionY, FloorGoal)
if ElevatorPositionY>=BoxAlignY-Elevator.FloorLevelingDistance then
FloorLeveling(self, FloorGoal)
self:Leveling(FloorGoal)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningEvent = true
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, FloorGoal)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal)
end
end
if ElevatorPositionY>=BoxAlignY-Elevator.LeveledDistance then
FloorLeveled(self, FloorGoal)
self:Leveled(FloorGoal)
end
else
FloorPassingDown(self, ElevatorPositionY, FloorGoal)
self:FloorPassingDown(ElevatorPositionY, FloorGoal)
if ElevatorPositionY<=BoxAlignY+Elevator.FloorLevelingDistance then
FloorLeveling(self, FloorGoal)
self:Leveling(FloorGoal)
if not DoorsOpeningEvent and ElevatorPositionY>=BoxAlignY-Elevator.DoorOpeningDistance then
DoorsOpeningEvent = true
self.ElevatorDoorsConstructor:ToggleElevatorDoors(true, FloorGoal)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(true, FloorGoal)
end
end
if ElevatorPositionY<=BoxAlignY+Elevator.LeveledDistance then
FloorLeveled(self, FloorGoal)
self:Leveled(FloorGoal)
end
end
self.BoxAlignPosition.Position = Vector3.new(ElevatorPositionInit.X, Leveling[FloorGoal], ElevatorPositionInit.Z)
end)
end
function Elevator:__GoToFloor(GoalLevelVEC, GoingUp)
if Doors.Attributes.DoorsOpen.Value then
self.ElevatorDoorsConstructor:ToggleElevatorDoors(false, Attributes.CurrentFloor.Value)
self.ElevatorDoorsConstructor:ToggleElevatorDoorsAsync(false, Attributes.CurrentFloor.Value)
end
if GoingUp then
@@ -413,28 +430,31 @@ function Elevator:__GoToFloor(GoalLevelVEC, GoingUp)
end
self.MOConstructor:UpdateCFrame()
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
self:__MovingHeartbeat(GoingUp)
local ElevatorBoxCurrentPos = self.ElevatorBox_1960.Position
self.BoxAlignPosition.Position = Vector3.new(ElevatorBoxCurrentPos.X, GoalLevelVEC, ElevatorBoxCurrentPos.Z)
end
function Elevator:RequestLevelAsync(RequestedLevel)
function Elevator:RequestLevel(RequestedLevel)
local GoalLevelVEC: number? = Leveling[RequestedLevel]
if GoalLevelVEC and GoalLevelVEC ~= Attributes.CurrentFloor.Value then
local ElevatorGoingUp = ElevatorGoingUpDirection(Attributes.CurrentFloor.Value, RequestedLevel)
if Attributes.Moving.Value then
ToFloorQueue(self, ElevatorGoingUp, RequestedLevel, GoalLevelVEC)
self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
else
Attributes.GoingUp.Value = ElevatorGoingUp
if Doors.Attributes.DoorsOpen then
Attributes.GoingUp.Value = ElevatorGoingUp
if Doors.Attributes.DoorsOpen and #self.FloorQueue == 0 then
self:__GoToFloor(GoalLevelVEC, ElevatorGoingUp)
if Doors.Attributes.DoorsOpen and #self.RelayAlgorithmConstructor.__FloorQueue == 0 then
self:__GoToFloor(GoalLevelVEC, ElevatorGoingUp)
end
self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
else
self.RelayAlgorithmConstructor:Insert(ElevatorGoingUp, RequestedLevel)
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)}`)