home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / apilot.lha / APilot / APilot_Ser / fuelpod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-03  |  4.0 KB  |  158 lines

  1. /***************************************************************************
  2.  *
  3.  * fuelpod.c -- Functions for handling fuelpods.
  4.  *
  5.  *-------------------------------------------------------------------------
  6.  * Authors: Casper Gripenberg  (casper@alpha.hut.fi)
  7.  *          Kjetil Jacobsen  (kjetilja@stud.cs.uit.no)
  8.  *
  9.  */
  10.  
  11. /*--------------------------------------------------------------------------*/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <exec/types.h>
  17. #include <libraries/dos.h>
  18.  
  19. #include "common.h"
  20. #include "fuelpod.h"
  21.  
  22. #include "main_protos.h"
  23.  
  24. /*--------------------------------------------------------------------------*/
  25.  
  26. #define OBJ_RANGE(o1, o2)       ( (((o1)->pos.x-(o2)->pos.x) *   \
  27.                                    ((o1)->pos.x-(o2)->pos.x)) +  \
  28.                                   (((o1)->pos.y-(o2)->pos.y) *   \
  29.                                    ((o1)->pos.y-(o2)->pos.y)) ) 
  30.  
  31. extern AWorld World;
  32.  
  33. /*--------------------------------------------------------------------------*/
  34.  
  35. /*
  36.  * alloc_fuelpod -- Allocate a new pod and at it to the world fuelpod list.
  37.  *
  38.  */                 
  39. AFuelPod *
  40. alloc_fuelpod(int map_x, int map_y)
  41. {
  42.   AFuelPod *pod;
  43.  
  44.   if ( (pod = (AFuelPod *) malloc(sizeof(AFuelPod))) == NULL )
  45.     cleanExit( RETURN_WARN, "** Unable to get memory for fuelpods.\n" );
  46.  
  47.   pod->mapos.x = map_x;
  48.   pod->mapos.y = map_y;
  49.   pod->pos.x   = map_x * MAP_BLOCKSIZE + MAP_BLOCKSIZE / 2;
  50.   pod->pos.y   = map_y * MAP_BLOCKSIZE + MAP_BLOCKSIZE / 2;
  51.   pod->fuel    = 0;
  52.   pod->fuelcount = 0;
  53.  
  54.   pod->next = World.fuelpods->next;
  55.   World.fuelpods->next = pod;
  56.     
  57.   return pod;
  58. }
  59.  
  60. /*--------------------------------------------------------------------------*/
  61.  
  62. /*
  63.  * fuel_ship -- If the ship tries to fuel check if there are some nearby
  64.  *              fuelpods; and if there are start fueling the ship.
  65.  */
  66. void
  67. fuel_ship( AShip *ship )
  68. {
  69.   int minrange = 0;
  70.   int fuelreq, fueltrans;
  71.   int range, x, y;
  72.   int mapp_x, mapp_y;
  73.   int start_x, end_x, start_y, end_y;
  74.   AFuelPod  *fuelpod;
  75.   MAP_Point **map_points = World.map_points;
  76.  
  77.   /*
  78.    * Check if the ship already is fueling.
  79.    */
  80.   if (ship->fpod != NULL) {
  81.     if (OBJ_RANGE(ship, ship->fpod) > 
  82.         (2 * ((MAP_BLOCKSIZE+MAP_BLOCKSIZE/3+MAP_BLOCKSIZE) * 
  83.               (MAP_BLOCKSIZE+MAP_BLOCKSIZE/3+MAP_BLOCKSIZE))) ) {
  84.       ship->fpod    = NULL;
  85.       ship->fueling = FALSE;
  86.       return;
  87.     } else {
  88.       fuelreq   = min(MAXFUEL-ship->fuel, FUELSPEED);
  89.       fueltrans = min(fuelreq, ship->fpod->fuel);
  90.       ship->fpod->fuel -= fueltrans;
  91.       ship->fuel       += fueltrans;
  92.       if (ship->fpod->fuel == 0) {
  93.         ship->fpod    = NULL;
  94.         ship->fueling = FALSE;
  95.       }
  96.       return;
  97.     }
  98.   }
  99.   
  100.   /*
  101.    * Ship is not fueling, find a suitable fuelpod (the closest in range)..
  102.    */
  103.   mapp_x = ship->pos.x / MAP_BLOCKSIZE;
  104.   mapp_y = ship->pos.y / MAP_BLOCKSIZE;
  105.   
  106.   start_x = max(0, mapp_x-1);
  107.   start_y = max(0, mapp_y-1);
  108.   end_x   = min(World.Width-1, mapp_x+1);
  109.   end_y   = min(World.Height-1, mapp_y+1);
  110.  
  111.   for (x = start_x; x <= end_x; x++) {
  112.     for (y = start_y; y <= end_y; y++) {
  113.       if (map_points[y][x].blocktype == BLOCK_FUEL) {
  114.         range = OBJ_RANGE(ship, (AFuelPod *)map_points[y][x].objectptr);
  115.         if (range < minrange || minrange == 0) {
  116.           minrange = range;
  117.           fuelpod = (AFuelPod *) map_points[y][x].objectptr;
  118.         }
  119.       }
  120.  
  121.     }
  122.   }
  123.  
  124.   if (minrange != 0) {
  125.     ship->fueling = TRUE;
  126.     ship->fpod = fuelpod;
  127.   } else {
  128.     ship->fueling = FALSE;
  129.     ship->fpod = NULL;
  130.   }
  131. }
  132.  
  133. /*--------------------------------------------------------------------------*/
  134.  
  135. /*
  136.  * update_fuelpods -- Goes through the whole fuelpod list and adds fuel
  137.  *                    to every pod.
  138.  */
  139. void
  140. update_fuelpods( UWORD nframes )
  141. {
  142.   AFuelPod *pod = World.fuelpods->next;
  143.  
  144.   while (pod->next != pod)
  145.   {
  146.     pod->fuelcount += nframes;
  147.  
  148.     if (pod->fuelcount >= FILLSPEED) {
  149.       pod->fuelcount = 0;
  150.       pod->fuel++;
  151.     }
  152.     if (pod->fuel > MAX_PODFUEL)
  153.       pod->fuel = MAX_PODFUEL;
  154.  
  155.     pod = pod->next;
  156.   }
  157. }
  158.