diff --git a/component/train.go b/component/train.go index 43173ef..b3a91c2 100644 --- a/component/train.go +++ b/component/train.go @@ -1,6 +1,9 @@ package component -import "joylink.club/ecs" +import ( + "fmt" + "joylink.club/ecs" +) // TrainPositionInfo 列车当前位置信息 type TrainPositionInfo struct { @@ -19,6 +22,10 @@ type TrainPositionInfo struct { TailLinkOffset int64 } +func (t *TrainPositionInfo) ToString() string { + return fmt.Sprintf("Up=%t len=%d headLink=%s headOff=%d tailLink=%s tailOff=%d", t.Up, t.Len, t.HeadLink, t.HeadLinkOffset, t.TailLink, t.TailLinkOffset) +} + var ( TrainPositionInfoType = ecs.NewComponentType[TrainPositionInfo]() ) diff --git a/fi/train.go b/fi/train.go index 93acd69..1382cdc 100644 --- a/fi/train.go +++ b/fi/train.go @@ -44,9 +44,9 @@ func UpdateTrainPositionFromDynamics(w ecs.World, tpi TrainPositionInfo) error { train.Up = tpi.Up train.Len = int64(tpi.Len) train.HeadLink = tpi.HeadLink - train.HeadLinkOffset = int64(tpi.TailLinkOffset) + train.HeadLinkOffset = int64(tpi.HeadLinkOffset) train.TailLink = tpi.TailLink - train.TailLinkOffset = int64(tpi.HeadLinkOffset) + train.TailLinkOffset = int64(tpi.TailLinkOffset) return ecs.NewOkEmptyResult() } else { return ecs.NewErrResult(fmt.Errorf("列车[%s]实体不存在", tpi.TrainId)) diff --git a/jl-ecs-go b/jl-ecs-go index 4171b05..5aa1f62 160000 --- a/jl-ecs-go +++ b/jl-ecs-go @@ -1 +1 @@ -Subproject commit 4171b05a7c23c3e40a755ccd7d08b4cfd6aa26a0 +Subproject commit 5aa1f62ee8ba0f720e4444a05b53cdbaffe4780a diff --git a/repository/link.go b/repository/link.go index 4dca630..a99e5e4 100644 --- a/repository/link.go +++ b/repository/link.go @@ -32,6 +32,20 @@ func NewLink(id string) *Link { } } +// GetAllPhysicalSection 获取与该link关联的所有物理区段(包括岔区非岔区) +func (l *Link) GetAllPhysicalSection() []*PhysicalSection { + var rt []*PhysicalSection + if l.physicalSections != nil { + rt = append(rt, l.physicalSections...) + } + if l.aRelation != nil { + rt = append(rt, l.aRelation.turnout.section) + } + if l.bRelation != nil { + rt = append(rt, l.bRelation.turnout.section) + } + return rt +} func (l *Link) Length() int64 { return l.length } diff --git a/sys/device_sys/section_detection.to.go b/sys/device_sys/section_detection.to.go index 6705539..9ba9729 100644 --- a/sys/device_sys/section_detection.to.go +++ b/sys/device_sys/section_detection.to.go @@ -10,6 +10,7 @@ import ( "joylink.club/rtsssimulation/repository" "joylink.club/rtsssimulation/repository/model/proto" "log/slog" + "strings" ) // SectionDetectSystem 区段检测系统 @@ -28,7 +29,9 @@ func (s *SectionDetectSystem) Update(w ecs.World) { //所有列车 s.trainQuery.Each(w, func(entry *donburi.Entry) { tp := component.TrainPositionInfoType.Get(entry) + //fmt.Println("============>>>>>>列车位置信息:", tp.ToString()) trainSectionIds := s.doSearchTrainOccupiedSections(w, tp) + //fmt.Println("============>>>>>>列车所在物理区段:", trainSectionIds) for _, sectionId := range trainSectionIds { //车所在区段 tc, find := sectionTrainMap[sectionId] if !find { @@ -61,18 +64,11 @@ func newTrainCount() *trainCount { func (c *trainCount) add() { c.count++ } - func (s *SectionDetectSystem) doSearchTrainOccupiedSections(w ecs.World, tp *component.TrainPositionInfo) []string { wd := entity.GetWorldData(w) curLink := wd.Repo.FindLink(tp.HeadLink) stp := &stpContext{w: w, trainLen: tp.Len, curLink: curLink, curOffset: tp.HeadLinkOffset, searchDirection: !tp.Up, acLen: 0} - c := 0 for stp.needAccumulate() { - c++ - if c > 50 { - slog.Warn("======>>>>搜索列车所占物理区段,迭代次数过多!!!!") - break - } stp.accumulateLen() if stp.needAccumulate() { stp.nextLink() @@ -96,6 +92,14 @@ type stpContext struct { acRanges []*stpLinkRange //轨道range列表 } +func (s *stpContext) printStpLinkRanges() { + var sb strings.Builder + for _, r := range s.acRanges { + sb.WriteString(fmt.Sprintf("[linkId=%s start=%d end=%d linkLen=%d]->", r.link.Id(), r.start, r.end, r.link.Length())) + } + fmt.Println("===========>>>>列车所在link range 链:", sb.String()) +} + // 获取列车所占的物理区段 func (s *stpContext) trainSections() []string { secs := make(map[string]string) @@ -105,6 +109,9 @@ func (s *stpContext) trainSections() []string { secs[id] = id } } + // + //s.printStpLinkRanges() + // var sectionIds []string for _, id := range secs { sectionIds = append(sectionIds, id) @@ -212,17 +219,20 @@ type stpLinkRange struct { // 获取link range 上的物理区段的id func (s *stpLinkRange) sectionIds() []string { var ids []string - ps := s.link.PhysicalSections() + ps := s.link.GetAllPhysicalSection() for _, p := range ps { for _, pr := range p.LinkRanges() { - if isRangeOverlap(s.start, s.end, pr.Start(), pr.End()) { + if isRangeOverlap(s.link.Id(), s.start, s.end, pr.Link().Id(), pr.Start(), pr.End()) { ids = append(ids, p.Id()) } } } return ids } -func isRangeOverlap(r1Start int64, r1End int64, r2Start int64, r2End int64) bool { +func isRangeOverlap(r1LinkId string, r1Start int64, r1End int64, r2LinkId string, r2Start int64, r2End int64) bool { + if r1LinkId != r2LinkId { + return false + } r1Start, r1End = formatRange(r1Start, r1End) r2Start, r2End = formatRange(r2Start, r2End) if r2Start > r1Start && r2Start < r1End { @@ -231,9 +241,13 @@ func isRangeOverlap(r1Start int64, r1End int64, r2Start int64, r2End int64) bool if r2End > r1Start && r2End < r1End { return true } - if r1Start == r2Start && r1End == r2End { + if r1Start > r2Start && r1Start < r2End { return true } + if r1End > r2Start && r1End < r2End { + return true + } + // return false } func formatRange(start int64, end int64) (int64, int64) { @@ -243,3 +257,12 @@ func formatRange(start int64, end int64) (int64, int64) { return end, start } } +func printPhSectionRange(w ecs.World, id string) string { + wd := entity.GetWorldData(w) + var sb strings.Builder + sb.WriteString(fmt.Sprintf("物理区段[%s]range列表:", id)) + for _, r := range wd.Repo.FindPhysicalSection(id).LinkRanges() { + sb.WriteString(fmt.Sprintf("[linkId=%s start=%d end=%d],", r.Link().Id(), r.Start(), r.End())) + } + return sb.String() +}