修改bug以及循环引用

This commit is contained in:
joylink_zhangsai 2023-09-26 10:25:00 +08:00
parent caa8a60e2c
commit 842a37077a
15 changed files with 285 additions and 83 deletions

View File

@ -4,7 +4,7 @@ import (
"github.com/yohamta/donburi/component"
"joylink.club/ecs"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto"
"joylink.club/rtsssimulation/simulation"
"joylink.club/rtsssimulation/system"
)
@ -27,21 +27,35 @@ func CreateSwitch2jzdj9Entity(w ecs.World, switchId string) *ecs.Entry {
return e
}
func CreateTurnoutEntries(world ecs.World, turnouts []*repository.Turnout, systemTypeMap map[system.Type]bool) []*ecs.Entry {
func CreateTurnoutEntries(world ecs.World, turnouts []*repository.Turnout) []*ecs.Entry {
var entries []*ecs.Entry
for _, turnout := range turnouts {
if len(turnout.RelayGroups()) == 0 {
continue
}
var components []component.IComponentType
components = append(components, system.EntityIdentityComponent)
loadZdj9Double := turnout.SwitchMachineType() == proto.Turnout_ZDJ9_Double && systemTypeMap[system.SWITCH_ZDJ9_2]
if loadZdj9Double {
components = append(components, system.Switch2jZdj9StateComponent)
}
components = append(components, system.Switch2jZdj9StateComponent)
entry := world.Create(components...)
entries = append(entries, entry)
system.EntityIdentityComponent.Set(entry, &system.EntityIdentity{Id: turnout.Id()})
if loadZdj9Double {
system.Switch2jZdj9StateComponent.Set(entry, system.NewSwitch2jZdj9State())
}
system.Switch2jZdj9StateComponent.Set(entry, system.NewSwitch2jZdj9State())
}
return entries
}
func TurnToNormal(worldId ecs.WorldId, turnoutId string) {
sim := simulation.FindSimulation(worldId)
turnoutEntry := system.FindEntityById(sim.World(), turnoutId)
state := system.Switch2jZdj9StateComponent.Get(turnoutEntry)
state.YCJ = true
state.DCJ = true
}
func TurnToReverse(worldId ecs.WorldId, turnoutId string) {
sim := simulation.FindSimulation(worldId)
turnoutEntry := system.FindEntityById(sim.World(), turnoutId)
state := system.Switch2jZdj9StateComponent.Get(turnoutEntry)
state.YCJ = true
state.FCJ = true
}

View File

@ -3,6 +3,7 @@ package sjzdj9
import (
"fmt"
"joylink.club/rtsssimulation/repository/model/proto"
world2 "joylink.club/rtsssimulation/simulation/world"
"time"
"github.com/yohamta/donburi/filter"
@ -10,7 +11,6 @@ import (
"joylink.club/rtsssimulation/entities"
"joylink.club/rtsssimulation/examples/test1/tmodel"
"joylink.club/rtsssimulation/examples/test1/tstorages"
"joylink.club/rtsssimulation/simulation"
"joylink.club/rtsssimulation/system"
)
@ -22,13 +22,13 @@ func Test() {
addSwitchs(modelStorage)
worldStorage := &tstorages.WorldModelStorage{Share: modelStorage, Links: tstorages.NewModelStorage()}
//
worldConfig := simulation.WorldConfig{
worldConfig := world2.WorldConfig{
ModelManager: worldStorage,
Systems: []ecs.ISystem{system.NewSwitch2jZdj9System(), system.NewRelaySystem(), system.NewDebugSystem()},
Tick: 200,
InitTime: time.Now(),
}
world := simulation.InitializeWorld(&worldConfig)
world := world2.InitializeWorld(&worldConfig)
addEntities(world, modelStorage)
initComponents(world)
//

6
go.mod
View File

@ -7,4 +7,8 @@ require (
google.golang.org/protobuf v1.31.0
)
require github.com/google/go-cmp v0.5.9 // indirect
require (
github.com/google/go-cmp v0.5.9 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
)

16
go.sum
View File

@ -1,8 +1,24 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yohamta/donburi v1.3.8 h1:ca4NuhzJ8Jeb6GAEf6ecksa+l8JWaAnr0WLqG20TimU=
github.com/yohamta/donburi v1.3.8/go.mod h1:5QkyraUjkzbMVTD2b8jaPFy1Uwjm/zdFN1c1lZGaezg=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,12 +1,11 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.1.0/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hajimehoshi/ebiten/v2 v2.4.13/go.mod h1:BZcqCU4XHmScUi+lsKexocWcf4offMFwfp8dVGIB/G4=
github.com/hajimehoshi/file2byteslice v1.0.0/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/jezek/xgb v1.0.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/image v0.1.0/go.mod h1:iyPr49SD/G/TBxYVB/9RRtGUT5eNbo2u4NamWeQcD5c=

@ -1 +1 @@
Subproject commit 4997760ffb1284a7898ab84164671d5e21ec9248
Subproject commit 200352eb58f1704a741d367e3c8afd02ae492b58

View File

@ -19,7 +19,23 @@ func newRelay(id string, code string, model string) *Relay {
}
}
func (r *Relay) Code() string {
return r.code
}
func (r *Relay) Model() string {
return r.model
}
type RelayGroup struct {
code string
relays []*Relay
}
func (r *RelayGroup) Code() string {
return r.code
}
func (r *RelayGroup) Relays() []*Relay {
return r.relays
}

View File

@ -124,7 +124,7 @@ func (repo *Repository) RelayList() []*Relay {
return list
}
func (repo *Repository) getModel(deviceId string, deviceType proto.DeviceType) (Identity, error) {
func (repo *Repository) FindModel(deviceId string, deviceType proto.DeviceType) (Identity, error) {
switch deviceType {
case proto.DeviceType_DeviceType_PhysicalSection:
return repo.physicalSectionMap[deviceId], nil
@ -151,6 +151,10 @@ func (repo *Repository) FindLink(id string) *Link {
return repo.linkMap[id]
}
func (repo *Repository) FindTurnout(id string) *Turnout {
return repo.turnoutMap[id]
}
func (repo *Repository) AddPhysicalSection(section *PhysicalSection) {
repo.physicalSectionMap[section.Id()] = section
}

View File

@ -271,7 +271,7 @@ func buildTurnoutRelationShip(source *proto.Repository, repo *Repository) error
}
func buildTurnoutPortRelation(repo *Repository, turnout *Turnout, port proto.Port, protoDp *proto.DevicePort) error {
model, err := repo.getModel(protoDp.DeviceId, protoDp.DeviceType)
model, err := repo.FindModel(protoDp.DeviceId, protoDp.DeviceType)
if err != nil {
return err
}
@ -315,7 +315,7 @@ func buildPhysicalSectionRelationShip(source *proto.Repository, repository *Repo
// 构建物理区段指定端口的关联关系。区段{section}的{port}端口关联{protoDp}
func buildSectionPortRelation(repo *Repository, section *PhysicalSection, port proto.Port, protoDp *proto.DevicePort) error {
model, err := repo.getModel(protoDp.DeviceId, protoDp.DeviceType)
model, err := repo.FindModel(protoDp.DeviceId, protoDp.DeviceType)
if err != nil {
return err
}

View File

@ -248,6 +248,24 @@ func (t *Turnout) GetTurnoutKm(port proto.Port) *proto.Kilometer {
return t.km
}
func (t *Turnout) RelayGroups() []*RelayGroup {
return t.relayGroups
}
func (t *Turnout) FindRelay(groupCode, relayCode string) *Relay {
for _, group := range t.relayGroups {
if group.code != groupCode {
continue
}
for _, relay := range group.relays {
if relay.code == relayCode {
return relay
}
}
}
return nil
}
type TurnoutPort struct {
turnout *Turnout
port proto.Port

View File

@ -1,23 +0,0 @@
package simulation
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/entities"
"joylink.club/rtsssimulation/system"
)
// InitializeWorld 初始化仿真world
func InitializeWorld(config *WorldConfig) ecs.World {
world := ecs.NewWorld(config.Tick)
// 添加系统
world.AddSystem(system.NewTimerSystem())
world.AddSystem(system.NewPercentageMovableSystem())
for _, sys := range config.Systems {
world.AddSystem(sys)
}
// 添加内置实体
entities.CreateSystemTimerEntity(world, config.InitTime)
entities.CreateModelStorageEntity(world, config.ModelManager)
//
return world
}

View File

@ -2,10 +2,7 @@ package simulation
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/entities"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/system"
"time"
)
var (
@ -17,49 +14,20 @@ type Simulation struct {
repo *repository.Repository
}
func (s *Simulation) GetWorld() ecs.World {
func (s *Simulation) World() ecs.World {
return s.world
}
func (s *Simulation) GetRepo() *repository.Repository {
func (s *Simulation) Repo() *repository.Repository {
return s.repo
}
func CreateSimulation(repo *repository.Repository, systemTypes ...system.Type) int {
var systems []ecs.ISystem
systemTypeMap := make(map[system.Type]bool)
for _, systemType := range systemTypes {
switch systemType {
case system.SWITCH_ZDJ9_2:
systems = append(systems, system.NewSwitch2jZdj9System())
case system.RELAY:
systems = append(systems, system.NewRelaySystem())
case system.DEBUG:
systems = append(systems, system.NewDebugSystem())
}
systemTypeMap[systemType] = true
}
wc := &WorldConfig{
Systems: systems,
Tick: 200,
InitTime: time.Now(),
}
world := InitializeWorld(wc)
func CreateSimulation(repo *repository.Repository, world ecs.World) {
sim := &Simulation{
world: world,
repo: repo,
}
simulationManager[world.Id()] = sim
//添加实体
entities.CreateTurnoutEntries(world, repo.TurnoutList(), systemTypeMap)
if systemTypeMap[system.RELAY] {
entities.CreateRelayEntries(world, repo.RelayList())
}
//初始化组件
initComponent(world)
//启动
world.StartUp()
return int(world.Id())
}
func DestroySimulation(id ecs.WorldId) {
@ -69,7 +37,3 @@ func DestroySimulation(id ecs.WorldId) {
func FindSimulation(id ecs.WorldId) *Simulation {
return simulationManager[id]
}
func initComponent(world ecs.World) {
}

View File

@ -1,4 +1,4 @@
package simulation
package world
import (
"joylink.club/rtsssimulation/system"

190
simulation/world/init.go Normal file
View File

@ -0,0 +1,190 @@
package world
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/entities"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/simulation"
"joylink.club/rtsssimulation/system"
"time"
)
func CreateSimulation(repo *repository.Repository) ecs.WorldId {
var systems []ecs.ISystem
wc := &WorldConfig{
Systems: systems,
Tick: 200,
InitTime: time.Now(),
}
w := InitializeWorld(wc)
simulation.CreateSimulation(repo, w)
//添加实体
entities.CreateTurnoutEntries(w, repo.TurnoutList())
entities.CreateRelayEntries(w, repo.RelayList())
//初始化组件
InitComponent(w, repo)
//添加监听器
system.Switch2jzdj9AddListeners(w)
system.RelayAddListeners(w)
//启动
w.StartUp()
return w.Id()
}
// InitializeWorld 初始化仿真world
func InitializeWorld(config *WorldConfig) ecs.World {
world := ecs.NewWorld(config.Tick)
// 添加系统
world.AddSystem(system.NewTimerSystem())
world.AddSystem(system.NewPercentageMovableSystem())
for _, sys := range config.Systems {
world.AddSystem(sys)
}
// 添加内置实体
entities.CreateSystemTimerEntity(world, config.InitTime)
entities.CreateModelStorageEntity(world, config.ModelManager)
//
return world
}
func InitComponent(world ecs.World, repo *repository.Repository) {
query := ecs.NewQuery(filter.Contains(system.Switch2jZdj9StateComponent))
query.Each(world, func(turnoutEntry *ecs.Entry) {
turnout := repo.FindTurnout(system.EntityIdentityComponent.Get(turnoutEntry).Id)
turnoutState := system.Switch2jZdj9StateComponent.Get(turnoutEntry)
{
relay := turnout.FindRelay("TDC", "DCJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.DCJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDC", "FCJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.FCJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDC", "YCJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.YCJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDC", "ZDBJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.ZDBJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDC", "ZFBJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.ZFBJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "1DQJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_1DQJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "BHJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_BHJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "2DQJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_2DQJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "1DQJF")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_1DQJF
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "DBJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_DBJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "FBJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_FBJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "QDJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_QDJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ1", "ZBHJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J1_ZBHJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ2", "1DQJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J2_1DQJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ2", "BHJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J2_BHJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ2", "2DQJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J2_2DQJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ2", "1DQJF")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J2_1DQJF
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ2", "DBJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J2_DBJ
relayState.ResetNeedXh()
}
{
relay := turnout.FindRelay("TDFJ2", "FBJ")
relayEntry := system.FindEntityById(world, relay.Id())
relayState := system.RelayStateComponent.Get(relayEntry)
relayState.Xh = turnoutState.J2_FBJ
relayState.ResetNeedXh()
}
})
}

View File

@ -48,7 +48,7 @@ func FindModelStorage(world ecs.World) IModelManager {
e, _ := modelStorageQuery.First(world)
return ModelStorageComponent.Get(e).ModelManager
} else {
return simulation.FindSimulation(world.Id()).GetRepo()
return simulation.FindSimulation(world.Id()).Repo()
}
}