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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/** \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"
#include "memspaces.h"

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

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

#define STARTING_FUEL 65535
#define STARTING_AMMO 200
#define MAX_ROCKS	16
#define MAX_BULLETS	4
#define ROCK_VEL	128
#define MIN_RADIUS 	10000


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 ammo;
	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 PROGMEM 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->ammo == 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->ammo--;
}


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)
		{
			b->age--;
			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->ammo = 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, 5);
}


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 inline uint8_t
same_quad(
	int16_t p1,
	int16_t p2
)
{
	if (p1 < 0)
		return p2 < 0;
	if (p1 > 255)
		return p2 > 255;
	if (p2 < 0)
		return 0;
	if (p2 > 255)
		return 0;
	return 1;
}


static void
draw_path(
	uint8_t x,
	uint8_t y,
	const int8_t * const p,
	uint8_t n
)
{
	int16_t ox = x + p[0];
	int16_t oy = y + p[1];
	for (uint8_t i = 1 ; i < n ; i++)
	{
		int16_t px = x + p[2*i+0];
		int16_t py = y + p[2*i+1];

		uint8_t do_line = same_quad(px, ox) && same_quad(py, oy);

		uint8_t ox8 = ox;
		uint8_t oy8 = oy;
		uint8_t px8 = px;
		uint8_t py8 = py;

		if (do_line)
		{
#ifdef __i386__
			printf("%d %d\n%d %d\n\n", ox8, oy8, px8, py8);
#else
			line(ox8, oy8, px8, py8);
#endif
		}
		ox = px;
		oy = py;
	}
}


#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(-5, -5),
		ROTATE(0,10),
		ROTATE(+5,-5),
		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 rx = pgm_read_byte(&rp[2*i +  swap_xy]);
		int8_t ry = pgm_read_byte(&rp[2*i + !swap_xy]);
		int8_t x = (rx * s) / 4;
		int8_t y = (ry * 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)
{
	srand48(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);
#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 inline uint8_t
hexdigit(
	uint8_t x
)
{
	x &= 0xF;
	if (x < 0xA)
		return x + '0' - 0x0;
	else
		return x + 'A' - 0xA;
}


/**
 * Enable ADC and select input ADC0 / F0
 * Select high-speed mode, left aligned
 * Use clock divisor of 2
 * System clock is 16 MHz on teensy, 8 MHz on tiny,
 * conversions take 13 ticks, so divisor == 128 (1,1,1) should
 * give 9.6 KHz of samples.
 */
static uint8_t adc_input;
static uint16_t adc_values[4];

static void
joy_init(void)
{
	ADMUX = adc_input
		| (0 << REFS1)
		| (1 << REFS0)
		;

	ADCSRA = 0
		| (1 << ADEN) // enable ADC
		| (0 << ADSC) // don't start yet
		| (0 << ADIE) // don't enable the interrupts
		| (0 << ADPS2)
		| (1 << ADPS1)
		| (1 << ADPS0)
		;

	ADCSRB = 0
		| (1 << ADHSM) // enable highspeed mode
		;

	DDRF = 0;
	PORTF = 0x00;
	sbi(PORTF, 4);
	sbi(PORTF, 5);
	sbi(DIDR0, ADC0D);
	sbi(DIDR0, ADC1D);

	// Start the first conversion!
	sbi(ADCSRA, ADSC);
}


static void
adc_read(void)
{
	if (bit_is_set(ADCSRA, ADSC))
		return;

	adc_values[adc_input] = ADC;
	adc_input = (adc_input + 1) % 2;

	// Configure for the next input
	ADMUX = (ADMUX & ~0x1F) | adc_input;

	// Start the next conversion
	sbi(ADCSRA, ADSC);
}


static void
draw_hex(
	uint8_t x,
	uint8_t y,
	uint16_t v
)
{
	draw_char_small(x+0, y, hexdigit(v >> 8));
	draw_char_small(x+20, y, hexdigit(v >> 4));
	draw_char_small(x+40, y, hexdigit(v >> 0));
}


static game_t g;

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

	usb_init();
	joy_init();
	game_init(&g);

	DDRB = 0xFF;
	DDRD = 0xFF;

	uint8_t last_fire = 0;

	while (1)
	{
		adc_read();
		adc_values[2] = in(0xF4);
		adc_values[3] = in(0xF5);

		//line_horiz(0,0, 250);
		//line_vert(0,0, 250);
		game_vectors(&g);

		draw_char_small( 0, 230, 'F');
		draw_char_small(20, 230, '=');
		draw_hex(40, 230, g.s.fuel >> 4);

		draw_char_small( 0, 200, 'A');
		draw_char_small(20, 200, '=');
		draw_hex(40, 200, g.s.ammo);

		draw_hex(0, 100, adc_values[0]);
		draw_hex(80, 100, adc_values[1]);
		draw_hex(0, 80, adc_values[2]);
		draw_hex(80, 80, adc_values[3]);

/*
		if (in(BUTTON_L) && in(button_R))
			ship_hyperspace(&g.s);
*/

		int c = -1;

		int8_t rot = (adc_values[0] >> 6) - (512 >> 6);
		int8_t thrust = (adc_values[1] >> 2) - (512 >> 2);
		if (thrust < 0)
			thrust = 0;
	
		int8_t fire = adc_values[2] == 0;

		game_update(
			&g,
			-rot,
			thrust,
			last_fire ? 0 : fire
		);

		last_fire = 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.