home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 98 / af098sub.adf / asteroids2.LZX / asteroids2 / rotx / player.c < prev    next >
C/C++ Source or Header  |  2009-05-15  |  8KB  |  372 lines

  1. #include <exec/types.h>
  2. #include <intuition/intuition.h>
  3. #include <stdio.h>
  4. #include <h/rot.h>
  5. #include <h/extern.h>
  6. #include <math.h>
  7. #include <graphics/gfxmacros.h>
  8.  
  9.  
  10.  
  11. LONG Random(bound)   /* Returns 0 to BOUND */
  12. DOUBLE bound;
  13. {
  14. DOUBLE x;
  15. LONG random;
  16.  
  17. x = lrand48();
  18. random = floor((x/2147483647.0)*(bound+1.0));
  19.  
  20. return((LONG)random);
  21. }
  22.  
  23.  
  24. InitBarrageFire(n)
  25. LONG n;
  26. {
  27. LONG num;
  28. LONG x,y,pos;
  29. LONG xx,yy;
  30.  
  31. for(y=0;y<3;y++)
  32. {
  33. num=-1;
  34. for(x=0;x<ship[n].pnum;x++)
  35.     {
  36.     if (ship[n].photI[x].flight == FALSE)
  37.         {
  38.         num = x;
  39.         break;
  40.         }
  41.     }
  42. if (num != -1)
  43.     {
  44.     pos = ship[n].pos+Random(2.0)-1;
  45.     if (pos > 31) pos = pos-31;
  46.     else
  47.     if (pos < 0) pos = 32+pos;
  48.  
  49.     xx = ship[n].x+id[ship[n].image+pos].wi/2;
  50.     yy = ship[n].y+id[ship[n].image+pos].he/2;
  51.  
  52.     makesound(0,0);
  53.     ship[n].photI[x].range = 18;
  54.     ship[n].photI[x].xp = xx;
  55.     ship[n].photI[x].yp = yy;
  56.     ship[n].photI[x].oxp = ship[n].x+id[ship[n].image+ship[n].pos].wi/2;
  57.     ship[n].photI[x].oyp = ship[n].y+id[ship[n].image+ship[n].pos].he/2;
  58.     ship[n].photI[x].pos = pos;
  59.     ship[n].photI[x].vx = ship[n].pvmax+Random(3.0);
  60.     ship[n].photI[x].vy = ship[n].pvmax+Random(3.0);
  61.     ship[n].photI[x].flight = TRUE;
  62.     ship[n].photI[x].type = BARRAGE;
  63.     ship[n].photI[x].wrap = control.firewrap;
  64.     ship[n].photI[x].image = ship[n].image;
  65.     }
  66. }
  67. }
  68.  
  69. InitHeavyFire(n)
  70. {
  71. LONG x,num;
  72.  
  73. num = -1;
  74. for(x=0;x<ship[n].pnum;x++)
  75.     if (ship[n].photI[x].flight == FALSE)
  76.         if (ship[n].photI[x].delay == 0)
  77.             {
  78.             num = x;
  79.             break;
  80.             }
  81.  
  82. if (num != -1)
  83.     {
  84.     if (n < control.playernum) makesound(0,0);
  85.     else                      makesound(5,0);
  86.     ship[n].photI[x].range = 24;
  87.     ship[n].photI[x].xp = ship[n].x+8;
  88.     ship[n].photI[x].yp = ship[n].y+8;
  89.     ship[n].photI[x].vx = ship[n].pvmax;
  90.     ship[n].photI[x].vy = ship[n].pvmax;
  91.     ship[n].photI[x].pos = ship[n].pos;
  92.     ship[n].photI[x].flight = TRUE;
  93.     ship[n].photI[x].wrap = control.firewrap;
  94.     ship[n].photI[x].type = DIRECT;
  95.     ship[n].photI[x].image = ship[n].image;
  96.     }
  97. }
  98.  
  99.  
  100.  
  101. InitDoubleFire(n)
  102. LONG n;
  103. {
  104. LONG num;
  105. LONG x,y,pos;
  106.  
  107. for(y=0;y<2;y++)
  108. {
  109. num=-1;
  110. for(x=0;x<ship[n].fnum;x++)
  111.     {
  112.     if (ship[n].shotI[x].flight == FALSE)
  113.         {
  114.         num = x;
  115.         break;
  116.         }
  117.     }
  118. if (num != -1)
  119.     {
  120.     pos = ship[n].pos;
  121.  
  122.     if (n < control.playernum) makesound(0,0);
  123.     else                      makesound(5,0);
  124.     ship[n].shotI[x].range = 24;
  125.     ship[n].shotI[x].xp = ship[n].x+id[ship[n].image+pos].wi/2+((2*y-1)*4*VyINC[pos])/100;
  126.     ship[n].shotI[x].yp = ship[n].y+id[ship[n].image+pos].he/2-((2*y-1)*4*VxINC[pos])/100;
  127.     ship[n].shotI[x].vx = ship[n].fvmax*VxINC[pos];
  128.     ship[n].shotI[x].vy = ship[n].fvmax*VyINC[pos];
  129.     ship[n].shotI[x].len = ship[n].flen;
  130.     ship[n].shotI[x].pos = pos;
  131.     ship[n].shotI[x].dx = (ship[n].shotI[x].len * VxINC[ship[n].shotI[x].pos]) /100;
  132.     ship[n].shotI[x].dy = (ship[n].shotI[x].len * VyINC[ship[n].shotI[x].pos]) /100;
  133.     ship[n].shotI[x].flight = TRUE;
  134.     }
  135. }
  136. }
  137.  
  138.  
  139.  
  140.  
  141. InitSuperFire(n)
  142. LONG n;
  143. {
  144. LONG x,y,num;
  145. LONG wi,he;
  146.  
  147. for(y=0;y<2;y++)
  148. {
  149. num=-1;
  150. for(x=0;x<ship[n].pnum;x++)
  151.     if (ship[n].photI[x].flight == FALSE)
  152.         if (ship[n].photI[x].delay == 0)
  153.             {
  154.             num = x;
  155.             break;
  156.             }
  157.  
  158. if (num != -1)
  159.     {
  160.     if (n < control.playernum) makesound(0,0);
  161.     else                      makesound(5,0);
  162.  
  163.     wi = id[ship[n].image+ship[n].pos].wi;
  164.     he = id[ship[n].image+ship[n].pos].he;
  165.     ship[n].photI[x].range = 24;
  166.     ship[n].photI[x].xp = ship[n].x+wi/2+((2*y-1)*4*VyINC[ship[n].pos])/100;
  167.     ship[n].photI[x].yp = ship[n].y+he/2-((2*y-1)*4*VxINC[ship[n].pos])/100;
  168.     ship[n].photI[x].vx = ship[n].pvmax;
  169.     ship[n].photI[x].vy = ship[n].pvmax;
  170.     ship[n].photI[x].pos = ship[n].pos;
  171.     ship[n].photI[x].flight = TRUE;
  172.     ship[n].photI[x].type = DIRECT;
  173.     ship[n].photI[x].image = ship[n].image;
  174.     }
  175. }
  176. }
  177.  
  178.  
  179. DrawShield(n)
  180. LONG n;
  181. {
  182. LONG xx,yy,pic;
  183.  
  184. if ((ship[n].shieldstat > 2) && (ship[n].pilot!=DESTROYED))
  185.     {
  186.     pic= ship[n].shIno;
  187.     xx = ship[n].x+id[ship[n].image+ship[n].pos].wi/2-id[pic].wi/2-1;
  188.     yy = ship[n].y+id[ship[n].image+ship[n].pos].he/2-id[pic].he/2-1;
  189.  
  190.     SetAPen(rp1[bit],1);
  191.     SetWrMsk(rp1[bit],0xfd);
  192.     BltTemplate(id[pic].data,0,2*id[pic].wo,rp1[bit],xx,yy,id[pic].wi,id[pic].he);
  193.     }
  194. }
  195.  
  196. EraseShield(n)
  197. LONG n;
  198. {
  199. LONG xx,yy,pic;
  200.  
  201. if (ship[n].shieldstat != 0)
  202.     {
  203.     --ship[n].shieldstat;
  204.  
  205.     pic= ship[n].shIno;
  206.     xx = ship[n].ox+id[ship[n].image+ship[n].pos].wi/2-id[pic].wi/2;
  207.     yy = ship[n].oy+id[ship[n].image+ship[n].pos].he/2-id[pic].he/2;
  208.  
  209.     SetWrMsk(rp1[1-bit],0xfd);
  210.     RectFill(rp1[1-bit],xx-4,yy-4,xx+id[pic].wi+4,yy+id[pic].he+4);
  211.     }
  212. }
  213.  
  214.  
  215. Hyperspace(n)
  216. LONG n;
  217. {
  218. LONG dx,dy;
  219.  
  220. makesound(7,2);
  221.  
  222. SetAPen(rp1[1-bit],0);
  223. RectFill(rp1[1-bit],ship[n].x,ship[n].y,ship[n].x+id[ship[n].image+ship[n].pos].wi,ship[n].y+id[ship[n].image+ship[n].pos].he);
  224.  
  225. if (ship[n].shieldstat != 0)
  226. RectFill(rp1[1-bit],ship[n].x-1,ship[n].y-1,ship[n].x+id[il.shield].wi,ship[n].y+id[il.shield].he);
  227.  
  228. dx = Random((DOUBLE)gi.dx);
  229. dy = Random((DOUBLE)gi.dy);
  230.  
  231. ship[n].ox = ship[n].x;
  232. ship[n].oy = ship[n].y;
  233.  
  234. ship[n].x = gi.x1 + dx;
  235. ship[n].y = gi.y1 + dy;
  236.  
  237. AllocateHyper(ship[n].x,ship[n].y);
  238. control.hyper[n] = 10;
  239. ship[n].pilot = DESTROYED;
  240. }
  241.  
  242.  
  243. DrawThrust()
  244. {
  245. LONG x,n;
  246.  
  247. SetAPen(rp1[bit],1);
  248. SetWrMsk(rp1[bit],0xfd);
  249. SetWrMsk(rp1[1-bit],0xfd);
  250.  
  251. for (n=0;n<control.playernum;n++)
  252. {
  253. for (x=0;x<control.thrustlength;x++)
  254.     {
  255.     if (ship[n].exhd[x] > 0)
  256.         {
  257.         ship[n].exhd[x]--;
  258.         WritePixel(rp1[bit],ship[n].exhlx[x],ship[n].exhly[x]);
  259.         WritePixel(rp1[bit],ship[n].exhrx[x],ship[n].exhry[x]);
  260.         }
  261.     else if (ship[n].exhd[x] == 0)
  262.         {
  263.         ship[n].exhd[x] = -1;
  264.         SetAPen(rp1[bit],0);
  265.         SetAPen(rp1[1-bit],0);
  266.  
  267.         WritePixel(rp1[bit],ship[n].exhlx[x],ship[n].exhly[x]);
  268.         WritePixel(rp1[1-bit],ship[n].exhlx[x],ship[n].exhly[x]);
  269.  
  270.         WritePixel(rp1[bit],ship[n].exhrx[x],ship[n].exhry[x]);
  271.         WritePixel(rp1[1-bit],ship[n].exhrx[x],ship[n].exhry[x]);
  272.  
  273.         SetAPen(rp1[bit],1);
  274.         }
  275.     }
  276. }
  277. }
  278.  
  279. IncVelocity(n)
  280. LONG n;
  281. {
  282. LONG x,num=-1;
  283. LONG wi,he;
  284.  
  285. makesound(1,1);
  286.  
  287. for (x=0;x<control.thrustlength;x++)
  288. if (ship[n].exhd[x] == -1)
  289.     {
  290.     num = x;
  291.     break;
  292.     }
  293.  
  294. if (num != -1)
  295.     {
  296.     wi = id[ship[n].image+ship[n].pos].wi;
  297.     he = id[ship[n].image+ship[n].pos].he;
  298.  
  299.     ship[n].exhlx[num] = ship[n].x+wi/2- (4*VyINC[ship[n].pos])/100;
  300.     ship[n].exhly[num] = ship[n].y+he/2+ (4*VxINC[ship[n].pos])/100;
  301.     ship[n].exhrx[num] = ship[n].x+wi/2+ (4*VyINC[ship[n].pos])/100;
  302.     ship[n].exhry[num] = ship[n].y+he/2- (4*VxINC[ship[n].pos])/100;
  303.     ship[n].exhd[num] = control.thrustlength;
  304.     }
  305.  
  306. ship[n].vx+=1*VxINC[ship[n].pos]; 
  307. ship[n].vy+=1*VyINC[ship[n].pos];
  308.  
  309. if (ship[n].vx > ship[n].vmax*100) ship[n].vx = ship[n].vmax*100;
  310. if (ship[n].vx <-ship[n].vmax*100) ship[n].vx =-ship[n].vmax*100;
  311. if (ship[n].vy > ship[n].vmax*100) ship[n].vy = ship[n].vmax*100;
  312. if (ship[n].vy <-ship[n].vmax*100) ship[n].vy =-ship[n].vmax*100;
  313. }
  314.  
  315.  
  316. UpdatePlayer(n)
  317. LONG n;
  318. {
  319. LONG wi,he;
  320.  
  321. ship[n].ox = ship[n].x;
  322. ship[n].oy = ship[n].y;
  323.  
  324. if (ship[n].pilot == HUMAN)
  325.     {
  326.     if (abs(ship[n].vx) > 0) ship[n].vx-=(abs(ship[n].vx)/40+1)*sign(ship[n].vx); /* damping */
  327.     if (abs(ship[n].vy) > 0) ship[n].vy-=(abs(ship[n].vy)/40+1)*sign(ship[n].vy);
  328.  
  329.     ship[n].modx += ship[n].vx%100;
  330.     ship[n].mody += ship[n].vy%100;
  331.     
  332.     ship[n].x += (ship[n].vx+ship[n].modx)/100;
  333.     ship[n].y += (ship[n].vy+ship[n].mody)/100;
  334.     
  335.     ship[n].modx -= (ship[n].modx/100)*100;
  336.     ship[n].mody -= (ship[n].mody/100)*100;
  337.  
  338.     wi = id[ship[n].image].wi/2;
  339.     he = id[ship[n].image].he/2;
  340.  
  341.     if (ship[n].x+wi > gi.x2) ship[n].x = gi.x1-wi;
  342.     if (ship[n].x+wi < gi.x1) ship[n].x = gi.x2-wi;
  343.     if (ship[n].y+he > gi.y2) ship[n].y = gi.y1-he;
  344.     if (ship[n].y+he < gi.y1) ship[n].y = gi.y2-he;
  345.  
  346.     todrawlist(ship[n].image+ship[n].pos,ship[n].x,ship[n].y,0xfd,1);
  347.     }
  348. }
  349.  
  350.  
  351. EraseAll()
  352. {
  353. LONG x,wi,he;
  354.  
  355. for (x=0;x<control.playernum;x++)
  356.         {
  357.         wi = id[ship[x].image+ship[x].pos].wi;
  358.         he = id[ship[x].image+ship[x].pos].he;
  359.         SetWrMsk(rp1[1-bit],0xfd);
  360.         RectFill(rp1[1-bit],ship[x].ox,ship[x].oy,ship[x].ox+wi,ship[x].oy+he);
  361.         WaitBlit();
  362.         }
  363.  
  364. for (x=control.playernum;x<control.playernum+control.maxenemynum;x++)
  365.     {
  366.     wi = id[ship[x].image+ship[x].pos].wi;
  367.     he = id[ship[x].image+ship[x].pos].he;
  368.     SetWrMsk(rp1[1-bit],0xfe);
  369.     RectFill(rp1[1-bit],ship[x].ox,ship[x].oy,ship[x].ox+wi,ship[x].oy+he);
  370.     }
  371. }
  372.