Annotation of researchv9/jtools/src/demo/asteroids/ship.c, revision 1.1.1.1

1.1       root        1: #include <jerq.h>
                      2: #include "ship.h"
                      3: #include "rock.h"
                      4: extern Bitmap *buildimage();
                      5: extern Rock *collision();
                      6: extern death;
                      7: extern notdrawn;
                      8: 
                      9: Bitmap *ship;
                     10: 
                     11: char nothrust[]={
                     12:        'R', -20, -20, 21, 21,
                     13:        'm', 20, 0,     /* must be tip of ship, where bombs go from */
                     14:        'l',  -11, 7,
                     15:        'l',  -11, -7,
                     16:        'l',  20, 0,
                     17:        0,
                     18: };
                     19: char thrust[]={
                     20:        'R', -20, -20, 21, 21,
                     21:        'm',  20, 0,
                     22:        'l',  -11, 7,
                     23:        'l',  -11, -7,
                     24:        'l',  20, 0,
                     25:        'm',  -11, 5,
                     26:        'l',  -20, 0,
                     27:        'l',  -11, -5,
                     28:        0
                     29: };
                     30: char shipim[sizeof thrust];    /* rotated into position */
                     31: int    angle=270;      /* up, actually */
                     32: int    costheta=0, sintheta= -1024;
                     33: int    oldangle;
                     34: int    thrusting;
                     35: int    oldthrusting;
                     36: Point  pos;
                     37: Point  oldpos;
                     38: int    reloading;
                     39: Point  vel;
                     40: Point  resid;
                     41: shipfree()
                     42: {
                     43:        bfree(ship);
                     44: }
                     45: initship()
                     46: {
                     47:        pos.x=Drect.corner.x/2;
                     48:        pos.y=Drect.corner.y/2;
                     49:        oldpos=pos;
                     50:        vel.x=0;
                     51:        vel.y=0;
                     52:        angle=270;
                     53:        thrusting=0;
                     54:        oldangle=angle;
                     55:        costheta=cos(angle);
                     56:        sintheta=sin(angle);
                     57:        oldthrusting=0;
                     58:        reloading=0;
                     59:        resid=vel;
                     60:        genship(nothrust);
                     61:        drawship();
                     62: }
                     63: genship(p)
                     64:        register char *p;
                     65: {
                     66:        register char *q=shipim;
                     67:        register i;
                     68:        bfree(ship);    /* safe if ship==0 */
                     69:        for(i=0; i<5; i++)
                     70:                *q++ = *p++;    /* copy bounding rectangle */
                     71:        while(*q++ = *p++){     /* copy opcode */
                     72:                *q++=m(sext(p[0]), costheta)-m(sext(p[1]), sintheta);
                     73:                *q++=m(sext(p[1]), costheta)+m(sext(p[0]), sintheta);
                     74:                p+=2;
                     75:        }
                     76:        ship=buildimage(shipim);
                     77: }
                     78: quickcheck()
                     79: {      register but=mouse.buttons&7;   /* sleazy, but avoid button1, etc. */
                     80:        if(death)
                     81:                return 0;
                     82:        if(computer && curthreat>=0)
                     83:                strat(), but = compbut;
                     84:        if((but&(4|1))==(4|1))  /* button1() && button3() ==> thrust */
                     85:                thrusting=1;
                     86:        else{
                     87:                thrusting=0;
                     88:                if(but&(4|1)){
                     89:                        if(but&4){      /* button1() ==> turn left */
                     90:                                angle-=DTHETA;
                     91:                                if(angle < 0)
                     92:                                        angle += 360;
                     93:                        }else{
                     94:                                angle+=DTHETA;
                     95:                                if(angle > 360)
                     96:                                        angle -= 360;
                     97:                        }
                     98:                        costheta=cos(angle);
                     99:                        sintheta=sin(angle);
                    100:                }
                    101:        }
                    102:        if(!reloading && (but&2)){
                    103:                shoot();
                    104:                reloading=RELOAD;
                    105:        }
                    106:        if(reloading>0)
                    107:                --reloading;
                    108:        if(thrusting){
                    109:                vel.x+=m(THRUST, costheta);
                    110:                vel.y+=m(THRUST, sintheta);
                    111:        }
                    112:        pos.x+=(vel.x+resid.x)/VSCALE;
                    113:        pos.y+=(vel.y+resid.y)/VSCALE;
                    114:        resid.x=(resid.x+vel.x)&(VSCALE-1);
                    115:        resid.y=(resid.y+vel.y)&(VSCALE-1);
                    116:        onscreen(&pos);
                    117:        if(collision(pos, hash(pos), RADSHIP)){
                    118:                allover(pos);
                    119:                return 0;
                    120:        }else
                    121:                return 1;
                    122: }
                    123: shoot()
                    124: {
                    125:        /* VSCALE/3 reflects the difference in updates between bombs and ship */
                    126:        startbomb(add(pos, Pt(sext(shipim[6]), sext(shipim[7]))), div(vel, VSCALE/3), angle);
                    127: }
                    128: longcheck()
                    129: {
                    130:        if(death)
                    131:                return 0;
                    132:        drawship();
                    133:        if(angle!=oldangle || thrusting!=oldthrusting){
                    134:                genship(thrusting? thrust : nothrust);
                    135:                notdrawn-=2;
                    136:        }
                    137:        oldpos=pos;
                    138:        oldangle=angle;
                    139:        oldthrusting=thrusting;
                    140:        drawship();
                    141:        if(vel.x > C)
                    142:                vel.x = C;
                    143:        if(vel.y > C)
                    144:                vel.y = C;
                    145:        if(vel.x < -C)
                    146:                vel.x = -C;
                    147:        if(vel.y < -C)
                    148:                vel.y = -C;
                    149:        return 1;
                    150: }
                    151: drawship()
                    152: {
                    153:        bitblt(ship, ship->rect, &display, add(transform(oldpos), ship->rect.origin), F_XOR);
                    154:        if(notdrawn>-20)
                    155:                --notdrawn;
                    156: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.