home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / conqsrc.lha / Conquest / src / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-02  |  1.9 KB  |  114 lines

  1. /* Utils.c: Diverse small utility funcitons that shouldn't lie all over */
  2.  
  3. #include <stdio.h>
  4. #include "defs.h"
  5. #include "structs.h"
  6. #include "vars.h"
  7. #include "protos.h"
  8.  
  9. double fmin(double a, double b)
  10. {
  11.   return(a<b?a:b);
  12. }
  13.  
  14. void point(int col, int row)
  15. {
  16.   switch ( terminal_type )
  17.   {
  18.    case adm3: 
  19.     printf("\33=%c%c", row+31, col+31);
  20.     break;
  21.    case vt52: 
  22.     printf("\33[%d;%dH", row, col);
  23.     break;
  24.    case vis400:
  25.     printf("\33[%d;%dH", row, col);
  26.     break;
  27.    case concept:
  28.     printf("\33a%c%c", row+31, col+31);
  29.     break;
  30.    default: 
  31.     putchar('\n');
  32.   }
  33.   x_cursor = col;
  34.   y_cursor = row;
  35.   if ( (x_cursor < 20) && (y_cursor != 18) )
  36.     left_line[y_cursor] = true;
  37. }
  38.  
  39. /* Relative (dis)placement. Doesn't set x_cursor & y_cursor */
  40. void move(int cols, int rows)
  41. {
  42.   if ((terminal_type == vt52) || (terminal_type == vis400))
  43.   {
  44.     if (cols > 0)
  45.       printf("\33[%dC", cols);
  46.     else if (cols < 0)
  47.       printf("\33[%dD", cols);
  48.   }
  49. }
  50.  
  51. /* Random integer in [1..i] */
  52. int rnd(int i)
  53. {
  54.   return(rand()%i+1);
  55. }
  56.  
  57. int round(float x)
  58. {
  59.   return(x<0.0?(int)(x-0.5):(int)(x+.5));
  60. }
  61.  
  62. int min(int x1, int x2)
  63. {
  64.   return(x1>x2?x2:x1);
  65. }
  66.  
  67. int max(int x1, int x2)
  68. {
  69.   return(x1<x2?x2:x1);
  70. }
  71.  
  72. /* Are there any battlestars or cruisers at this star? */
  73. boolean any_bc(tteam team, int starnum)
  74. {
  75.   boolean any;  
  76.   int tf_number;
  77.   
  78.   any = FALSE;
  79.   if (tf_stars[starnum][team]>0) 
  80.   {
  81.     for (tf_number = 1;(!any) && (tf_number <= MAX_FLEETS); tf_number++)
  82.     {
  83.       any = ((tf[team][tf_number].dest==starnum) &&
  84.          (tf[team][tf_number].eta==0) &&
  85.          ((tf[team][tf_number].c>0) || (tf[team][tf_number].b>0)));
  86.     }
  87.   }
  88.   return (any);
  89. }
  90.  
  91. double fact(int k)
  92.   int res;
  93.  
  94.   for (res = 1; k > 1; res *= k, k--);
  95.   
  96.   return(res);
  97. }
  98.  
  99. void swap(int *a, int *b)
  100. {
  101.   int t;
  102.   
  103.   t = *a;
  104.   *a = *b;
  105.   *b = t;
  106. }
  107.  
  108. int conv_bcd(int nibble, char byte)
  109. {
  110.   if (nibble == 1) return (byte & 0x0f);
  111.   return((byte >> 4) & 0x0f);
  112. }
  113.