-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.go
170 lines (160 loc) · 3.27 KB
/
sim.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package mfm
import "sync"
// SimConfig holds configuration used to create a new simulation.
type SimConfig struct {
Mode SimMode
Seed uint64
Width int
Height int
}
// SimState contains public data related to the running the simulation.
type SimState struct {
Sites map[C2D]Atom
Mode SimMode
Events int
Width int
Height int
}
// SimMode defines various running modes of the sim.
type SimMode int
// Enumerate sim states.
const (
ModeRun = 0
ModePause SimMode = 1 << iota
ModeYield
)
// Sim represents a sparse atom simulation.
type Sim struct {
state SimState
win Window
nextID uint64
scond sync.Cond
smutex sync.Mutex
}
// New creates a new mfm sim.
func New(c *SimConfig) *Sim {
sim := &Sim{state: SimState{
Mode: c.Mode,
Width: c.Width,
Height: c.Height,
Sites: make(map[C2D]Atom),
}}
sim.scond.L = &sim.smutex
sim.win.rand.Seed(c.Seed)
return sim
}
func (s *Sim) SetMode(t SimMode) {
s.scond.L.Lock()
defer s.scond.L.Unlock()
s.state.Mode = t
if s.state.Mode == 0 {
s.scond.Signal()
}
}
func (s *Sim) Mode() SimMode { return s.state.Mode }
func (s *Sim) State() SimState {
state := SimState{
Sites: make(map[C2D]Atom),
Mode: s.state.Mode,
Events: s.state.Events,
Width: s.state.Width,
Height: s.state.Height,
}
for c, a := range s.state.Sites { // copy sites
state.Sites[c] = a
}
return state
}
func (s *Sim) Reset() { s.state.Sites = make(map[C2D]Atom) }
func (s *Sim) valid(c C2D) bool {
return (s.state.Width == 0 && s.state.Height == 0) ||
(c.X >= 0 && c.Y >= 0 && c.X < s.state.Width && c.Y < s.state.Height)
}
func (s *Sim) Set(c C2D, a Atom) (ok bool) {
if s.valid(c) {
ok = true
if a.Type == nil {
delete(s.state.Sites, c)
} else {
s.state.Sites[c] = a
}
}
return
}
func (s *Sim) Get(c C2D) (a Atom, ok bool) {
return s.state.Sites[c], s.valid(c)
}
func (s *Sim) Step() { // must be yielded or paused
var c2 C2D
s.state.Events += len(s.state.Sites)
for c, a := range s.state.Sites {
n := r[a.Type.Radius]
if n == 0 {
continue
}
s.win.loc = c
s.win.me = a
site := Site(uint32((uint64(uint32(s.win.rand.Uint64())) * uint64(n)) >> 32))
c2.Set(site)
c2.Add(c2, s.win.loc)
if !s.valid(c2) {
continue
}
s.win.Reset()
s.win.s = site
s.win.a, _ = s.state.Sites[c2]
a.Type.Func(&s.win)
var dst C2D
dst.Set(s.win.mut.dst)
dst.Add(dst, s.win.loc)
switch {
case s.win.mut.mode&mutSet != 0:
s.Set(c2, s.win.mut.atom)
case s.win.mut.mode&mutMove != 0:
a, _ := s.Get(c2)
s.Set(dst, a)
case s.win.mut.mode&mutSwap != 0:
a, _ := s.Get(c2)
if a2, ok := s.Get(dst); ok {
s.Set(dst, a)
s.Set(c2, a2)
}
}
}
}
func (s *Sim) Run() {
for {
s.scond.L.Lock()
for s.state.Mode != 0 {
s.scond.Wait()
}
s.Step()
s.scond.L.Unlock()
}
}
func (s *Sim) RegisterAtoms(atoms ...*AtomInfo) {
for _, a := range atoms {
s.nextID++
a.ID = s.nextID
}
}
func (s *Sim) RegisterHooks(hooks ...Hook) {
var hookMu sync.Mutex
for _, h := range hooks {
go func(h Hook) { // schedule hook
for {
h.Wait()
hookMu.Lock()
s.SetMode(s.state.Mode ^ ModeYield)
h.Call()
s.SetMode(s.state.Mode ^ ModeYield)
hookMu.Unlock()
}
}(h)
}
}
// Hook provides a extensible functionality the simulation.
type Hook interface {
Call()
Wait()
}