-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimulate.rb
239 lines (194 loc) · 6.66 KB
/
simulate.rb
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require 'json'
require 'chipmunk'
class SimulateInfo
attr_accessor :filename, :index, :positions, :sample_strength
def initialize(filename)
# 시뮬레이션 데이터 파일 읽기
file = File.read(filename)
json_data = JSON.parse(file)
@filename = filename
@index = json_data["index"]
@positions = json_data["positions"]
@sample_strength = json_data["strength"]
end
end
class Simulator
def initialize(info)
@info = info
# 데이터 파일로부터 내 돌과 상대 돌의 위치정보 추출
my_positions = Array.new
your_positions = Array.new
for position in @info.positions["my"] do
my_positions.push([position["x"], position["y"]])
end
for position in @info.positions["your"] do
your_positions.push([position["x"], position["y"]])
end
@positions = [my_positions, your_positions]
# 모든 돌의 위치정보로 가상 게임을 생성
@alggago = Alggago.new(@positions)
end
def run
# 입력파일에 결과를 추가하기 위해 json 파일 열기
file = File.read(@info.filename)
json_data = JSON.parse(file)
json_data["result"] = []
# 모든 샘플 데이터를 테스트
for strength in @info.sample_strength do
@alggago.init_game(@positions)
# 테스트 케이스 계산 후 돌에 물리값 적용
@alggago.calculate(@info.index, strength["x"], strength["y"])
# 모든 돌이 멈출 때 까지 물리엔진 갱신
while [email protected]_end do
@alggago.update
end
# 결과 데이터를 hash에 저장
my = []
your = []
if @alggago.players[0].stones.length > 0
my = @alggago.players[0].stones.map { |stone| { "x" => stone.body.p.x, "y" => stone.body.p.y } }
end
if @alggago.players[1].stones.length > 0
your = @alggago.players[1].stones.map { |stone| { "x" => stone.body.p.x, "y" => stone.body.p.y } }
end
json_data["result"].push({
"my" => my,
"your" => your
})
end
# 전체 시뮬레이션 결과를 파일로 출력
File.write("./#{@info.filename}", JSON.pretty_generate(json_data))
end
end
HEIGHT = 700
TICK = 1.0/60.0
STONE_DIAMETER = 50
RESTITUTION = 0.9
BOARD_FRICTION = 1.50
STONE_FRICTION = 0.5
ROTATIONAL_FRICTION = 0.04
MAX_POWER = 700.0
class Alggago
attr_reader :players
attr_accessor :turn_end
def initialize(positions)
@space = CP::Space.new
@players = Array.new
init_game(positions)
end
def init_game(positions)
@turn_end = false
@selected_stone = nil
@players.each do |player|
player.stones.each do |stone|
@space.remove_body(stone.body)
@space.remove_shape(stone.shape)
end
player.stones.clear
end
@players.clear
positions.each do | position |
player = Player.new(position)
player.stones.each do |stone|
@space.add_body(stone.body)
@space.add_shape(stone.shape)
end
@players.push(player)
end
end
def update
@space.step(TICK)
@turn_end = true
@players.each do |player|
player.update
player.stones.each do |stone|
@turn_end = false if (stone.body.v.x != 0) or (stone.body.v.y != 0)
if stone.should_delete
@space.remove_body(stone.body)
@space.remove_shape(stone.shape)
player.stones.delete(stone)
end
end
end
end
def calculate(index, x_strength, y_strength)
if @players[0].stones.length <= index
puts("Error: calculate() - stone index is out of bounds")
return
end
reduced_x, reduced_y = reduce_speed(x_strength, y_strength)
@players[0].stones[index].body.v = CP::Vec2.new(reduced_x, reduced_y)
end
def reduce_speed(x, y)
if x*x + y*y > MAX_POWER*MAX_POWER
co = MAX_POWER / Math.sqrt(x*x + y*y)
return x*co, y*co
else
return x, y
end
end
end
class Player
attr_reader :stones
def initialize(positions)
@stones = Array.new
positions.each { |x, y| @stones.push(Stone.new(x, y)) }
end
def update
@stones.each do |stone|
stone.update
if (stone.body.p.x + STONE_DIAMETER/2.0 > HEIGHT) or
(stone.body.p.x + STONE_DIAMETER/2.0 < 0) or
(stone.body.p.y + STONE_DIAMETER/2.0 > HEIGHT) or
(stone.body.p.y + STONE_DIAMETER/2.0 < 0)
stone.should_delete = true
end
end
end
end
class Stone
attr_reader :body, :shape
attr_accessor :should_delete
def initialize(x, y)
@should_delete = false
@body = CP::Body.new(1, CP::moment_for_circle(1.0, 0, 1, CP::Vec2.new(0, 0)))
@body.p = CP::Vec2.new(x, y)
@shape = CP::Shape::Circle.new(body, STONE_DIAMETER/2.0, CP::Vec2.new(0, 0))
@shape.e = RESTITUTION
@shape.u = STONE_FRICTION
end
def update
new_vel_x, new_vel_y = 0.0, 0.0
if @body.v.x != 0 or @body.v.y != 0
new_vel_x = get_reduced_velocity(@body.v.x, @body.v.length)
new_vel_y = get_reduced_velocity(@body.v.y, @body.v.length)
end
@body.v = CP::Vec2.new(new_vel_x, new_vel_y)
new_rotational_v = 0
new_rotational_v = get_reduced_rotational_velocity(@body.w) if @body.w != 0
@body.w = new_rotational_v
end
private
def get_reduced_velocity(original_velocity, original_velocity_length)
if original_velocity.abs <= BOARD_FRICTION * (original_velocity.abs / original_velocity_length)
return 0
else
return (original_velocity.abs / original_velocity) *
(original_velocity.abs - BOARD_FRICTION * (original_velocity.abs / original_velocity_length))
end
end
private
def get_reduced_rotational_velocity(velocity)
if velocity.abs <= ROTATIONAL_FRICTION
return 0
else
return (velocity.abs / velocity) * (velocity.abs - ROTATIONAL_FRICTION)
end
end
end
if ARGV.length != 1
puts "ERROR: Invalid Arguments!"
end
simulate_info = SimulateInfo.new(ARGV[0])
simulator = Simulator.new(simulate_info)
simulator.run