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

  1. /****************************************************************************
  2.  *
  3.  * lists.c -- Handling of list structures..
  4.  *
  5.  *-------------------------------------------------------------------------
  6.  * Authors: Casper Gripenberg  (casper@alpha.hut.fi)
  7.  *          Kjetil Jacobsen  (kjetilja@stud.cs.uit.no)
  8.  *
  9.  */
  10.  
  11. #include <stdlib.h>
  12. #include <exec/types.h>
  13. #include <libraries/dos.h>          /* Official return codes defined here */
  14.  
  15. #include "main_protos.h"
  16.  
  17. #include "common.h"
  18. #include "defs.h"
  19.  
  20. #include "lists.h"
  21.  
  22. /*-------------------------------------------------------------------------*/
  23.  
  24. extern AWorld World;
  25.  
  26. /*-------------------------------------------------------------------------*/
  27.  
  28. /*
  29.  * init_points -- Allocate a chuck of memory for points moving on the
  30.  *                screen and initialize the list structure.
  31.  */
  32. void
  33. init_points(void)
  34. {
  35.   int i;
  36.   APoint *pointail;
  37.  
  38.   World.points = (APoint *) malloc(sizeof(APoint));
  39.   pointail  = (APoint *) malloc(sizeof(APoint));
  40.   free_point_head = (APoint *) malloc (sizeof (APoint) * POINT_CHUNK_SIZE);
  41.   
  42.   if ((World.points == NULL) || (pointail == NULL) || (free_point_head == NULL))
  43.     cleanExit( RETURN_WARN, "** Failed to allocate space for points.\n" );
  44.  
  45.   for (i = 0; i < (POINT_CHUNK_SIZE - 1); i++)
  46.       free_point_head[i].next = &free_point_head[i + 1];
  47.   free_point_head[POINT_CHUNK_SIZE - 1].next = NULL;
  48.  
  49.   World.points->next = pointail;
  50.   World.points->prev = World.points;
  51.   pointail->next  = pointail;
  52.   pointail->prev  = World.points;
  53. }
  54.  
  55. /*-------------------------------------------------------------------------*/
  56.  
  57. /*
  58.  * alloc_point -- Allocate space for a point from the free_point list.
  59.  *
  60.  */
  61. APoint *
  62. alloc_point (void)
  63. {
  64.   int i;
  65.   APoint *newPoint;
  66.  
  67.   if (free_point_head == NULL)
  68.     return NULL;
  69.  
  70.   newPoint = free_point_head;
  71.   free_point_head = newPoint->next;
  72.  
  73.   newPoint->next  = World.points->next;
  74.   newPoint->prev  = World.points;
  75.   World.points->next->prev = newPoint;
  76.   World.points->next = newPoint;
  77.   
  78.   for (i = 0; i < MY_BUFFERS; i++)
  79.     newPoint->p_pos[i].x = newPoint->p_pos[i].y = 0;
  80.   newPoint->drawn   = 0;
  81.   newPoint->xcount  = newPoint->ycount = 0;
  82.   newPoint->draw_it = FALSE;
  83.   newPoint->lastmap = C_NONE;
  84.   return newPoint;
  85. }
  86.  
  87. /*-------------------------------------------------------------------------*/
  88.  
  89. /*
  90.  * free_point -- Frees a point from the active-points list and puts it in
  91.  *               the free_point list.
  92.  */
  93. void
  94. free_point (APoint *aPoint)
  95. {
  96.   aPoint->prev->next = aPoint->next;
  97.   aPoint->next->prev = aPoint->prev;
  98.   aPoint->next = free_point_head;
  99.   free_point_head = aPoint;
  100.   return;
  101. }
  102.  
  103. /*-------------------------------------------------------------------------*/
  104.  
  105. /*
  106.  * alloc_base -- Adds a base to the base list.
  107.  *
  108.  */
  109. ABase *
  110. alloc_base(int map_x, int map_y)
  111. {
  112.   ABase *base;
  113.  
  114.   if ( (base = (ABase *) malloc(sizeof(ABase))) == NULL )
  115.     cleanExit( RETURN_WARN, "** Unable to get memory for bases.\n" );
  116.  
  117.   base->mapos.x = map_x;
  118.   base->mapos.y = map_y;
  119.   base->owner   = NULL;
  120.  
  121.   base->next = World.bases->next;
  122.   World.bases->next = base;
  123.  
  124.   return base;
  125. }
  126.  
  127. /*-------------------------------------------------------------------------*/
  128.  
  129. /*
  130.  * get_base -- Gets the next free base from the base-list.
  131.  *
  132.  */
  133. ABase *
  134. get_base(AShip *ship)
  135. {
  136.   ABase *base      = World.bases->next;
  137.  
  138.   while (base->next != base) {
  139.     if (base->owner == NULL) {
  140.       /* Found an empty base */
  141.       base->owner = ship;
  142.       return base;
  143.     }
  144.     base = base->next;
  145.   }  
  146.   return NULL;
  147. }
  148.