-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.bt
More file actions
323 lines (284 loc) · 13.1 KB
/
Copy pathpong.bt
File metadata and controls
323 lines (284 loc) · 13.1 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// This is based on Masanori Misono's bpftrace-tetris.
// The license is the same as the original.
//
// Copyright (C) 2020 Rylan Dmello <mail@rylan.coffee>
//
// This file is free software and is distributed under the terms of the GNU
// General Public License (GPL); either version 2, or (at your option) any
// later version.
// A simple pong game in bpftrace, meant for educational purposes.
// Inspired by bpftrace-tetris.
// pty_write(), defined in drivers/tty/pty.c.
// Most terminal emulators write to the pty to ensure that job control
// is managed by the kernel in a predictable way.
// TODO: is there a way to intercept raw keypresses instead?
// Filter to just one byte writes using the /../ syntax.
kprobe:pty_write /arg2 == 1/ {
// '$' introduces a local variable.
// Also, C-style casts are allowed in bpftrace syntax.
$input_buffer_ptr = (int8*) arg1;
// Store the current keypress in a BPF map called "key"
// '@' introduces a global scope map.
@key = *($input_buffer_ptr);
// For debugging, will print the ASCII keycode entered by the user.
// printf("%d\n", @key);
}
// The BEGIN block is executed before any other probes are run.
// Use this to perform some basic game initialization.
BEGIN {
printf("\033[H\033[2J"); // clear screen
printf("\033[H"); // move cursor to top left
printf("Welcome to PONG!");
// These are configurable via the input arguments.
@WIDTH = $1; // Number of width blocks.
@HEIGHT = $2; // Number of height blocks.
@DIFFICULTY = $3; // Difficulty range between 1 to 100.
// Initial ball position, in the middle of the screen.
@ball_position = ((int64) ((uint64) @WIDTH / 2), (int64) ((uint64) @HEIGHT / 2));
// Initial ball velocity. Add some jitter.
$x_velocity = ((int64) ((rand % 2) * 2) - 1) * ((int64) (rand % 2) + 1); // [-2, -1, 1, 2]
$y_velocity = ((int64) ((rand % 2) * 2) - 1) * ((int64) (rand % 2) + 1); // [-2, -1, 1, 2]
@ball_velocity = ($x_velocity, $y_velocity);
// Initial paddle positions. This is the position of the center of the paddle.
@left_paddle = (int64) ((uint64) @HEIGHT / 2);
@right_paddle = (int64) ((uint64) @HEIGHT / 2);
@paddle_radius = 2;
// Scoring
@score = (0, 0); // (player, computer)
}
// The actual game loop. Runs every 0.098s.
interval:ms:98 {
/////////////////////////////////////////////////////////
//// Update player paddle position with keypresses //////
/////////////////////////////////////////////////////////
if (@key == 0x6A) {
// Down keypress.
@left_paddle += 1;
} else if (@key == 0x6B) {
// Up keypress.
@left_paddle -= 1;
} else {
// Do nothing.
}
// Reset the @key value, so that we re-read the key on the next iteration.
@key = 0;
//////////////////////////////////////////////////////////////
//// Update opponent paddle position based on heuristic //////
//////////////////////////////////////////////////////////////
// Very simple heuristic: if ball is up, move up. vice versa.
$position_difference = @ball_position.1 - @right_paddle;
// Add some jitter to the AI movement on lower difficulty
$roll = (int64) (rand % 99) + 1;
if ($roll > @DIFFICULTY) {
// Move in the opposite direction or sometimes forget to move.
$position_difference *= ((rand % 3) == 0) ? 0 : -1;
}
// Only update AI position when the ball is moving towards them.
if (@ball_velocity.0 > 0) {
if ($position_difference == 0) {
// At the same height, nothing to do.
} else if ($position_difference > 0) {
// Need to move down.
++@right_paddle;
} else if ($position_difference < 0) {
// Need to move up.
--@right_paddle;
}
}
///////////////////////////////////
//// Bound paddle positions //////
///////////////////////////////////
// Bound the paddle positions by the top and bottom walls.
if (@left_paddle < @paddle_radius - 1) {
@left_paddle = @paddle_radius - 1;
}
if (@left_paddle > @HEIGHT - @paddle_radius) {
@left_paddle = @HEIGHT - @paddle_radius;
}
// Also do this for the opponent paddle.
if (@right_paddle < @paddle_radius - 1) {
@right_paddle = @paddle_radius - 1;
}
if (@right_paddle > @HEIGHT - @paddle_radius) {
@right_paddle = @HEIGHT - @paddle_radius;
}
/////////////////////////////////////////////////////////
///// Update ball position according to ~~physics~~ /////
/////////////////////////////////////////////////////////
// Update position with velocity.
@ball_position = (@ball_position.0 + @ball_velocity.0, @ball_position.1 + @ball_velocity.1);
// Bounce off the paddles.
$needs_reset = false;
if (@ball_position.0 <= 1) {
// Check if the player's paddle is in the correct position.
$is_hit = (@ball_position.1 > @left_paddle - @paddle_radius) &&
(@ball_position.1 < @left_paddle + @paddle_radius);
if ($is_hit) {
// Flip y-velocity if the ball hit the "edge" of the paddle.
$is_edge_hit = (@ball_position.1 == @left_paddle - @paddle_radius + 1 && @ball_velocity.1 > 0) ||
(@ball_position.1 == @left_paddle + @paddle_radius - 1 && @ball_velocity.1 < 0);
@ball_position = (1, @ball_position.1);
@ball_velocity = (-1 * @ball_velocity.0, ($is_edge_hit ? -1 : 1) * @ball_velocity.1);
} else {
// Its a miss...update score, reset ball position.
@score = (@score.0, @score.1 + 1);
$needs_reset = true;
}
} else if (@ball_position.0 >= @WIDTH-1) {
// Check if the AI's paddle is in the correct position.
$is_hit = (@ball_position.1 > @right_paddle - @paddle_radius) &&
(@ball_position.1 < @right_paddle + @paddle_radius);
if ($is_hit) {
// Flip y-velocity if the ball hit the "edge" of the paddle.
$is_edge_hit = (@ball_position.1 == @right_paddle - @paddle_radius + 1 && @ball_velocity.1 > 0) ||
(@ball_position.1 == @right_paddle + @paddle_radius - 1 && @ball_velocity.1 < 0);
@ball_position = (@WIDTH-2, @ball_position.1);
@ball_velocity = (-1 * @ball_velocity.0, ($is_edge_hit ? -1 : 1) * @ball_velocity.1);
} else {
// Its a miss...update score, reset ball position.
@score = (@score.0 + 1, @score.1);
$needs_reset = true;
}
}
// Reset ball position after loss.
if ($needs_reset) {
@ball_position = ((int64) ((uint64) @WIDTH / 2), (int64) ((uint64) @HEIGHT / 2));
// Initial ball velocity. Add some jitter.
$x_velocity = ((int64) ((rand % 2) * 2) - 1) * ((int64) (rand % 2) + 1); // [-2, -1, 1, 2]
$y_velocity = ((int64) ((rand % 2) * 2) - 1) * ((int64) (rand % 2) + 1); // [-2, -1, 1, 2]
@ball_velocity = ($x_velocity, $y_velocity);
}
// Bounce off the top walls.
if (@ball_position.1 <= 0) {
@ball_position = (@ball_position.0, 0);
@ball_velocity = (@ball_velocity.0, -1 * @ball_velocity.1); // Flip y-velocity.
} else if (@ball_position.1 >= @HEIGHT-1) {
@ball_position = (@ball_position.0, @HEIGHT-1);
@ball_velocity = (@ball_velocity.0, -1 * @ball_velocity.1); // Flip y-velocity.
}
/////////////////////////////////////////////////////////
///// Populate new ball/paddle position in the buffer. //
/////////////////////////////////////////////////////////
//// Build the buffer that will be displayed.
// This is preferable to directly printing to the screen
// since it means that we don't have to manually control moving
// back and forth in the screen using terminal codes.
// Instead everything can be printed through one sequential
// pass through the buffer.
// First index: x, [0,@WIDTH). Second index: y, [0,@HEIGHT).
// Value:
// 0 - blank space
// 1 - ball_position
// 2 - left paddle
// 3 - right paddle
// 4 - center line
$i = 0;
unroll ($1) {
$j = 0;
unroll ($2) {
@screenbuffer[$i,$j] = 0;
++$j;
}
++$i;
}
// Add the center line to the screenbuffer
$j = 0;
unroll($2) {
@screenbuffer[(uint64) @WIDTH/2, $j] = 4;
++$j;
}
// Add the ball_position position to the screenbuffer.
@screenbuffer[@ball_position.0, @ball_position.1] = 1;
// Add the paddle positions to the screenbuffer.
$j = 0;
unroll($2) {
if ($j > (@left_paddle - @paddle_radius) && $j < (@left_paddle + @paddle_radius)) {
@screenbuffer[0, $j] = 2;
}
if ($j > (@right_paddle - @paddle_radius) && $j < (@right_paddle + @paddle_radius)) {
@screenbuffer[@WIDTH-1, $j] = 3;
}
++$j;
}
//////////////////////////////////////////////
///// Draw the actual screenbuffer now. //////
//////////////////////////////////////////////
printf("\033[H"); // move cursor to top left
printf("\n"); // Start on the next line.
// Print the top wall.
printf("\033[44m \033[m");
unroll($1) {
printf("\033[44m \033[m");
}
printf("\033[44m \033[m");
printf("\n");
// Print the vertical walls and the actual buffer.
$j = 0;
unroll($2) {
printf("\033[44m \033[m");
$i = 0;
unroll($1) {
// Print this position in the screenbuffer.
if (@screenbuffer[$i,$j] == 1) {
printf("\033[33mO\033[m"); // ball
} else if (@screenbuffer[$i,$j] == 2) {
printf("\033[42m \033[m"); // player's paddle (left)
} else if (@screenbuffer[$i,$j] == 3) {
printf("\033[41m \033[m"); // computer's paddle (right)
} else if (@screenbuffer[$i,$j] == 4) {
printf("|");
} else {
printf(" "); // blank space
}
++$i;
}
printf("\033[44m \033[m");
printf("\n");
++$j;
}
// Print the bottom wall.
printf("\033[44m \033[m");
unroll($1) {
printf("\033[44m \033[m");
}
printf("\033[44m \033[m");
printf("\n");
// Print the score
printf("| Move: J/K | Score: %d - %d | Exit: Ctrl+C |\n", @score.0, @score.1);
// Exit if either player has hit 7 points.
if (@score.0 >= 7 || @score.1 >= 7) {
exit();
}
}
// Executed when the BPF program is terminating...
END {
// Print a victory screen if any player got over 7 points.
if (@score.0 >= 7 || @score.1 >= 7) {
if (@score.0 >= 7) {
printf(" _ ___ __ __\n");
printf(" | | / (_)____/ /_____ _______ __/ /\n");
printf(" | | / / / ___/ __/ __ \\/ ___/ / / / / \n");
printf(" | |/ / / /__/ /_/ /_/ / / / /_/ /_/ \n");
printf(" |___/_/\\___/\\__/\\____/_/ \\__, (_) \n");
printf(" /____/ \n");
} else {
printf(" __ __\n");
printf(" / / ____ __________/ /\n");
printf(" / / / __ \\/ ___/ ___/ / \n");
printf(" / /___/ /_/ (__ |__ )_/ \n");
printf(" /_____/\\____/____/____(_) \n");
}
}
// Clear all open maps, so they are not printed to screen on exit.
clear(@screenbuffer);
clear(@key);
clear(@ball_position);
clear(@ball_velocity);
clear(@left_paddle);
clear(@right_paddle);
clear(@paddle_radius);
clear(@score);
clear(@WIDTH);
clear(@HEIGHT);
clear(@DIFFICULTY);
}