vectorscope / spacerocks.c

  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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/** \file
 * "Space Rocks" game.
 *
 * Might be similar to another game you've played.
 */
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "sin_table.h"

#ifdef __i386__
#define fastrand() lrand48()
#else
#include <avr/io.h>
#include "vector.h"
#include "bits.h"

static inline uint16_t
fastrand(void)
{
	return TCNT1 ^ (TCNT1 << 1);
}
#endif

#define STARTING_FUEL 65535
#define STARTING_AMMO 65535
#define MAX_ROCKS	32
#define MAX_BULLETS	4
#define ROCK_VEL	64
#define MIN_RADIUS 	10000

static int frame_num = 0;


typedef struct
{
	int16_t x;
	int16_t y;
	int16_t vx;
	int16_t vy;
} point_t;

typedef struct
{
	point_t p;
	int16_t ax;
	int16_t ay;
	uint16_t fuel;
	uint16_t shots;
	uint8_t angle;
	uint8_t dead;
} ship_t;

typedef struct
{
	point_t p;
	uint8_t age;
} bullet_t;

typedef struct
{
	point_t p;
	uint8_t type;
	uint16_t size;
} rock_t;


/** There are various types of rocks */
static const int8_t rock_paths[][8*2] = {
	{
		-4, -2,
		-4, +2,
		-2, +4,
		 0, +2,
		+2, +4,
		+4, -2,
		 0, -4,
		-4, -2,
	},
	{
		-4, -2,
		-3,  0,
		-4, +2,
		-2, +4,
		+4, +2,
		+2, +1,
		+4, -3,
		-4, -2,
	},
	{
		-2, -4,
		-4, -1,
		-3, +4,
		+2, +4,
		+4, +1,
		+3, -4,
		 0, -1,
		-2, -4,
	},
	{
		-4, -2,
		-4, +2,
		-2, +4,
		+2, +4,
		+4, +2,
		+4, -2,
		+1, -4,
		-4, -2,
	},
};


#define NUM_ROCK_TYPES (sizeof(rock_paths) / sizeof(*rock_paths))


typedef struct
{
	ship_t s;
	bullet_t b[MAX_BULLETS];
	rock_t r[MAX_ROCKS];
} game_t;


static void
point_update(
	point_t * const p
)
{
	p->x += p->vx;
	p->y += p->vy;
}


static int
collide(
	const point_t * const p,
	const point_t * const q,
	const int16_t radius
)
{
	int16_t dx = p->x - q->x;
	int16_t dy = p->y - q->y;

	if (-radius < dx && dx < radius
	&&  -radius < dy && dy < radius)
		return 1;

	// Not in the bounding box.
	return 0;
}


static void
ship_update_angle(
	ship_t * const s,
	int8_t rot
)
{
	if (rot == 0)
		return;

	s->angle += rot;
	s->ax = sin_lookup(s->angle);
	s->ay = cos_lookup(s->angle);
}


static void
ship_update_thrust(
	ship_t * const s,
	uint8_t thrust
)
{
	if (thrust == 0 || s->fuel == 0)
		return;

	if (s->fuel < thrust)
		thrust = s->fuel;

	s->fuel -= thrust;
	s->p.vx += (s->ax * thrust) / 128;
	s->p.vy += (s->ay * thrust) / 128;
}


static void
ship_fire(
	ship_t * const s,
	bullet_t * const b
)
{
	if (s->shots == 0)
		return;

#define BULLET_RANGE 255
#define BULLET_VEL 8

	b->age = BULLET_RANGE;
	b->p.x = s->p.x;
	b->p.y = s->p.y;
	b->p.vx = s->ax * BULLET_VEL + s->p.vx; // in the direction of the ship
	b->p.vy = s->ay * BULLET_VEL + s->p.vy; // in the direction of the ship

#ifdef __i386__
	fprintf(stderr, "fire: vx=%d vy=%d\n", b->p.vx, b->p.vy);
#endif

	s->shots--;
}


static void
ship_update(
	ship_t * const s,
	int8_t rot,
	uint8_t thrust
)
{
	ship_update_angle(s, rot);
	ship_update_thrust(s, thrust);
	point_update(&s->p);
}

static void
rock_create(
	rock_t * const rocks,
	int16_t x,
	int16_t y,
	int16_t size
)
{
	// find a free rock
	for (uint8_t i = 0 ; i < MAX_ROCKS ; i++)
	{
		rock_t * const r = &rocks[i];
		if (r->size != 0)
			continue;

		r->size = size;
		r->p.x = x;
		r->p.y = y;
		r->p.vx = fastrand() % ROCK_VEL;
		r->p.vy = fastrand() % ROCK_VEL;
		r->type = fastrand() % (NUM_ROCK_TYPES * 8);
		return;
	}
}


static void
rocks_update(
	ship_t * const s,
	bullet_t * const bullets,
	rock_t * const rocks
)
{
	for (uint8_t i = 0 ; i < MAX_ROCKS ; i++)
	{
		rock_t * const r = &rocks[i];
		if (r->size == 0)
			break;

		point_update(&r->p);
		uint8_t rock_dead = 0;

		// check for bullet collision
		for (uint8_t j = 0 ; j < MAX_BULLETS ; j++)
		{
			bullet_t * const b = &bullets[j];
			if (b->age == 0)
				continue;
			if (collide(&r->p, &b->p, r->size))
			{
				uint16_t new_size = r->size / 2;
				if (new_size > 256)
				{
					rock_create(rocks, r->p.x, r->p.y, new_size);
					rock_create(rocks, r->p.x, r->p.y, new_size);
					rock_create(rocks, r->p.x, r->p.y, new_size);
				}

				r->size = 0;
				b->age = 0;
				rock_dead = 1;

				break;
			}
		}

		if (rock_dead)
			continue;

		 if (collide(&r->p, &s->p, r->size))
			s->dead = 1;
	}
}


static void
bullets_update(
	ship_t * const s,
	bullet_t * const bullets,
	uint8_t fire
)
{
	for (uint8_t i = 0 ; i < MAX_BULLETS ; i++)
	{
		bullet_t * const b = &bullets[i];
		if (b->age != 0)
		{
			point_update(&b->p);
		} else
		if (fire)
		{
			// We can try to fire this one
			ship_fire(s, b);
			fire = 0;
		}
	}

	if (fire)
	{
#ifdef __i386__
		fprintf(stderr, "no bullets\n");
#endif
	}
}



static void
ship_init(
	ship_t * const s
)
{
	s->p.x = 0;
	s->p.y = 0;
	s->p.vx = 0;
	s->p.vy = 0;
	s->angle = fastrand();
	s->dead = 0;
	s->fuel = STARTING_FUEL;
	s->shots = STARTING_AMMO;
	s->ax = cos_lookup(s->angle);
	s->ay = sin_lookup(s->angle);
}


static void
bullets_init(
	bullet_t * const bullets
)
{
	for (uint8_t i = 0 ; i < MAX_BULLETS ; i++)
	{
		bullet_t * const b = &bullets[i];
		b->age = 0;
	}
}



static void
rocks_init(
	rock_t * const rocks,
	uint8_t num
)
{
	for (uint8_t i = 0 ; i < MAX_ROCKS ; i++)
	{
		rock_t * const r = &rocks[i];
		r->size = 0;
	}

	for (uint8_t i = 0 ; i < num ; i++)
	{
		// Make sure that there is space around the center
		int16_t x = fastrand();
		int16_t y = fastrand();
		uint16_t size = (fastrand() % 32) * 256 + 512;
		if (0 <= x)
			x += MIN_RADIUS;
		else
			x -= MIN_RADIUS;
		if (0 <= y)
			y += MIN_RADIUS;
		else
			y -= MIN_RADIUS;

		rock_create(rocks, x, y, size);

	}
}


static void
game_init(
	game_t * const g
)
{
	ship_init(&g->s);
	bullets_init(g->b);
	rocks_init(g->r, 8);
}


static void
game_update(
	game_t * const g,
	int8_t rot,
	uint8_t thrust,
	uint8_t fire
)
{
	//printf("rot=%d thrust=%d fire=%d\n", rot, thrust, fire);

	// Update our position before we fire the gun
	ship_update(&g->s, rot, thrust);

	// Update our bullets before the rocks move
	bullets_update(&g->s, g->b, fire);

	// Update the rocks, checking for collisions
	rocks_update(&g->s, g->b, g->r);

	// If we hit something, start over
	if (g->s.dead)
	{
#ifdef __i386__
		fprintf(stderr, "game over\n");
#endif
		game_init(g);
	}
}


static void
draw_path(
	uint8_t x,
	uint8_t y,
	const int8_t * const p,
	uint8_t n
)
{
#ifdef __i386__
	for (uint8_t i = 0 ; i < n ; i++)
	{
		uint8_t px = x + p[2*i+0];
		uint8_t py = y + p[2*i+1];
		printf("%d %d %d\n", px, py, frame_num);
	}

	printf("\n");
#else
	uint8_t ox = x + p[0];
	uint8_t oy = y + p[1];
	for (uint8_t i = 1 ; i < n ; i++)
	{
		const uint8_t px = x + p[2*i+0];
		const uint8_t py = y + p[2*i+1];

		line(ox, oy, px, py);
		ox = px;
		oy = py;
	}
#endif
}


#define ROTATE(x,y) \
	((x) * s->ay + (y) * s->ax) / 128, \
	((y) * s->ay - (x) * s->ax) / 128 \

/** Generate the ship vectors, rotated by the current angle.
 * The ship_update_rot() has cached the sin/cos value for us.
 */
static void
draw_ship(
	const ship_t * const s
)
{
	int8_t path[] = {
		ROTATE(0,0),
		ROTATE(-4, -4),
		ROTATE(0,8),
		ROTATE(+4,-4),
		ROTATE(0,0),
	};

	draw_path(s->p.x / 256 + 128, s->p.y / 256 + 128, path, 5);
}


/** Draw the bullets lined in the direction of travel */
static void
draw_bullet(
	const bullet_t * const b
)
{
	int8_t path[] = {
		-1, -1,
		-1, +1,
		+1, +1,
		+1, -1,
		-1, -1,
		//b->p.vx/64, b->p.vy/64,
	};

	draw_path(b->p.x / 256 + 128, b->p.y / 256 + 128, path, 5);
}



static void
draw_rock(
	const rock_t * const r
)
{
	//printf("rock %d,%d size %d\n", r->p.x/256+128, r->p.y/256+128, r->size/256);

	const int8_t s = r->size / 256;
	const uint8_t swap_xy = r->type & 1;
	const uint8_t flip_x = r->type & 2;
	const uint8_t flip_y = r->type & 4;
	const int8_t * const rp = rock_paths[r->type >> 3];

	int8_t path[8*2];
	for (uint8_t i = 0 ; i < 8 ; i++)
	{
		int8_t x = (rp[2*i +  swap_xy] * s) / 4;
		int8_t y = (rp[2*i + !swap_xy] * s) / 4;
		path[2*i+0] = flip_x ? -x : x;
		path[2*i+1] = flip_y ? -y : y;
	}

	draw_path(r->p.x / 256 + 128, r->p.y / 256 + 128, path, 8);
}


static void
game_vectors(
	const game_t * const g
)
{
	draw_ship(&g->s);

	for (uint8_t i = 0 ; i < MAX_ROCKS ; i++)
	{
		const rock_t * const r = &g->r[i];
		if (r->size == 0)
			continue;
		draw_rock(r);
	}

	for (uint8_t i = 0 ; i < MAX_BULLETS ; i++)
	{
		const bullet_t * const b = &g->b[i];
		if (b->age == 0)
			continue;
		draw_bullet(b);
	}
}


#ifdef __i386__
int main(void)
{
	srand(getpid());

	game_t g;

	game_init(&g);

	while (1)
	{
#if 0
		printf("---\nS: %+6d,%+6d %+6d,%+6d %d\n", g.s.p.x/256, g.s.p.y/256, g.s.p.vx, g.s.p.vy, g.s.angle);

		for (uint8_t i = 0 ; i < MAX_ROCKS ; i++)
		{
			const rock_t * const r = &g.r[i];
			if (r->size == 0)
				continue;
			printf("%d: %+6d,%+6d %+6d,%+6d\n", i, r->p.x/256, r->p.y/256, r->p.vx, r->p.vy);
		}
#else
		game_vectors(&g);
		frame_num++;
#endif

		int c;

		while (1)
		{
			c = getchar();
			if (c == '\n')
				continue;
			if (c == -1)
				return 0;
			break;
		}

		game_update(
			&g,
			c == 'l' ? -17 : c == 'r' ? +17 : 0,
			c == 't' ? 16 : 0,
			c == 'f' ? 1 : 0
		);
	}
}

#else

static game_t g;

int main(void)
{
	// set for 16 MHz clock
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
	CPU_PRESCALE(0);

	game_init(&g);

	while (1)
	{
		game_vectors(&g);

		int8_t rot = ADC - 128;
		uint8_t thrust = ADC;
		uint8_t fire = in(0xB7);

		game_update(
			&g,
			rot,
			thrust,
			fire
		);
	}
}
#endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.