-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMain.m
More file actions
174 lines (131 loc) · 5.77 KB
/
Copy pathGameMain.m
File metadata and controls
174 lines (131 loc) · 5.77 KB
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
function logging = GameMain(gameName)
%% Globals
global quit_sim new_sim record_sim;
quit_sim = 0;
new_sim = 0;
record_sim = 0;
global env;
global E;
global O;
mycontrols;
load_networks;
%% Graphics and Recording
global G;
G = [];
logging = {};
recording = 0;
%% Counters
gameTerminatorCnt = TimeCounter(env.O.max_steps);
gamePlotterCnt = TimeCounter(env.O.plot_every);
for trial=1:env.O.max_trial
if quit_sim
break;
end
if strcmp(gameName,'Obstacle')
env.E.obstacles = [];
end
[env,x] = init_sim(env);
disp(sprintf('Trial %d', trial));
t_step = 0;
if strcmp(gameName,'Obstacle')
while 1
map = zeros(env.O.mapN,1);
obs_delta = bsxfun(@minus,env.E.obstacles,x(1:2));
obs_dists = sqrt(sum(obs_delta.^2,1));
obs_goal_dists = sqrt(sum(env.E.obstacles.^2,1));
if ~((min(obs_dists) <= (env.E.disk + env.E.tl + 1)) ...
|| (min(obs_goal_dists) <= 2*env.E.disk + 1))
% not inside disk
break;
else
env.E.obstacles = [];
[env,x] = init_sim(env);
end
end
end
% logging saves environment data,
% state data, and control data
logging{trial} = {env,zeros(4,env.O.max_steps),...
zeros(1,env.O.max_steps)};
while 1
t_step = t_step + 1;
if quit_sim
if recording
close(writerObj);
end
close all;
break;
end
if new_sim
gameTerminatorCnt(1);
new_sim = 0;
break;
end
%% Simulation Code Here
if gameTerminatorCnt(0) || TerminateCondition(env.E, x)
gameTerminatorCnt(1);
break;
end
switch gameName
case 'Obstacle'
psense = SenseProprioceptive(x);
gsense = SenseGoal(x);
osense = SenseObstacles(env,x);
m2 = FProp(hcontroller,[psense;gsense;osense]);
x_fic = ConvertBearingToVirtualPosition(env.E,m2,x);
ix = InternalStateObstacle(x_fic);
map = zeros(env.O.mapN,1);
[map,env.E.obstacles_detected] = ShootBeams(env,x,map,1);
case 'Truck'
ix = InternalStateTruck(x);
otherwise
error('Unknown Game');
end
m1 = lcontroller.FProp(ix);
logging{trial}{2}(:,t_step) = x;
logging{trial}{3}(:,t_step) = m1;
if strcmp(gameName,'Obstacle')
x = SimDynamics(env,x,m1,max(1,ceil(get(uictrls.speedslider,'Value'))));
%x = SimDynamics(env,x,m1,env.O.simCoarse);
else
x = TruckDynamics(env,x,m1,max(1,ceil(get(uictrls.speedslider,'Value'))));
end
jiggle_on = get(uictrls.jiggletoggle,'Value');
if strcmp(gameName,'Obstacle') && jiggle_on
[env,G] = JiggleObstacles(env,G);
end
%% Graphics Here
if gamePlotterCnt(0)
simdisp_on = get(uictrls.simdisptoggle,'Value');
if simdisp_on
% Draw Simulation
if strcmp(gameName,'Obstacle')
G = SimAnimation(env, G, x, m1);
DrawSubgoal(env,G,m2,x);
if strcmp(gameName,'Obstacle')
if ~isempty(G.lineplan)
delete(G.lineplan);
G.lineplan = [];
end
end
else
G = TruckAnimation(env, G, x, m1);
end
end
if record_sim
[fname, pname] = uiputfile('*.avi');
writerObj = VideoWriter(strcat(pname,fname),'Motion JPEG AVI');
writerObj.FrameRate = 60;
open(writerObj);
record_sim = false;
recording = 1;
end
drawnow;
if recording
set(0,'CurrentFigure',G.fig);
writeVideo(writerObj,getframe);
end
end
end
end
end