OpenCores
URL https://opencores.org/ocsvn/openmsp430/openmsp430/trunk

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [fpga/] [actel_m1a3pl_dev_kit/] [software/] [spacewar/] [update.c] - Blame information for rev 212

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 80 olivier.gi
#include "spacewar.h"
2
 
3
//************************************************************
4
// externals
5
//
6
 
7
extern int bzsin(char);
8
 
9
//************************************************************
10
//
11
// update
12
//
13
//    checks rockets buttons and fires its torpedoes
14
//    updates rockets position
15
//    updates torpedos position
16
//
17
/* Description:
18
Generates the x, y vectors for the rocket at present angle.  Checks the A to D
19
passed into function for any keys pressed.  The A to D resistors are selected
20
to generate values in between the ranges checked.  If you push multiple buttons
21 212 olivier.gi
strange results will occure.  If no keys are pressed the fire flaag clears.
22 80 olivier.gi
Rotate counter clockwise increments the angle variable.  Rotate clockwise
23
decrements the angle variable.  If the thrust key is pressed a scaled
24
value of xsize and ysize is added into the x and y velocity of the rocket
25
creating acceleration.  If the fire button is pressed the fire flag is checked.
26
If no fire flag then a inactive torpedo is searched for.  If a inactive torpedo
27
is found the torpedo is given the present position of the rocket plus an x,y
28
offset to get it past the nose of the rocket.  The torpedo is given the present
29
velocity of the rocket plus additional speed based on xsize, ysize.
30
The rocket position is updated by adding a scaled velocity into position.
31
The position is masked to the DAC maximum.  The torpedo positions are updated
32
by adding a scaled torpedo velocity into torpedo position.  The torpedo
33
position is masked to the DAC maximum.
34
*/
35
void update(rkt_data *rkt, unsigned int new_a2d)
36
{
37
  int i;
38 212 olivier.gi
 
39 80 olivier.gi
  rkt->xsize = bzsin(rkt->ang + 64);    // this returns the cosine
40
  rkt->ysize = bzsin(rkt->ang);         // this returns the sine
41
 
42
  if(new_a2d > 0xD000){                 // rkt rotate CCW ?
43
    rkt->ang++;                         // yes
44 212 olivier.gi
  }
45 80 olivier.gi
  else {                                // no
46
    if(new_a2d > 0xA000){               // rkt rotate CW ?
47
      rkt->ang--;                       // yes
48 212 olivier.gi
    }
49 80 olivier.gi
    else {                              // no
50
      if(new_a2d > 0x6000) {            // is rkt thrusting
51
        rkt->xvel += ((long) rkt->xsize << 6);  // yes
52
        rkt->yvel += ((long) rkt->ysize << 6);
53
    }
54
      else {                            // no
55
        if(new_a2d > 0x2000) {          // is rkt firing now ?
56
          if((rkt->flags & fire_bit) == 0) {  // did rkt fire last loop ?
57
            for(i = 0; i < ammo; i++) {       // no - look for a torp read to fire
58
              if(rkt->pt_dx[i] == -1) {
59
                rkt->flags |= fire_bit;       // set firing flag
60
                rkt->pt_dx[i] = rkt->xdisp + (rkt->xsize << 1); // load up a new torps data
61
                rkt->pt_dy[i] = rkt->ydisp + (rkt->ysize << 1);
62
                rkt->pt_vx[i] = (rkt->xvel >> 16) + (rkt->xsize >> 3);
63
                rkt->pt_vy[i] = (rkt->yvel >> 16) + (rkt->ysize >> 3);
64
                break;
65
              }
66
            }
67 212 olivier.gi
          }
68 80 olivier.gi
        } else {
69
          rkt->flags &= ~fire_bit;            // clear fire flag
70
        }
71
      }
72
    }
73
  }
74
 
75
  rkt->xdisp = (rkt->xdisp + (rkt->xvel >> 16)) & max_dac; // update rkt x position
76
  rkt->ydisp = (rkt->ydisp + (rkt->yvel >> 16)) & max_dac; // update rkt y position
77
 
78
  for(i = 0; i < ammo; i++) {
79
    if(rkt->pt_dx[i] != -1) {
80
      rkt->pt_dx[i] = (rkt->pt_dx[i] + rkt->pt_vx[i]) & max_dac; // move torp x
81
      rkt->pt_dy[i] = (rkt->pt_dy[i] + rkt->pt_vy[i]) & max_dac; // move torp x
82
    }
83
  }
84
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.