home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 030.lha / Ogre / ogrecom.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  5KB  |  234 lines

  1. #include "ext.h"
  2.  
  3. move_ogre()
  4. {
  5.    init_move_ogre();
  6.    ogre.moves_left = ogre.movement;
  7.    while (ogre.moves_left > 0)
  8.    {
  9.       move_ogre1();
  10.       ogre_ram();
  11.       cycle();
  12.    }
  13. }
  14.  
  15. #define INFINITY 32767
  16.  
  17. move_ogre1()
  18. {
  19.    int weight[7];
  20.    int i, max;
  21.    char a, b, olda, oldb;
  22.  
  23.    a = ogre.l_hex;
  24.    b = ogre.r_hex;
  25.  
  26.    weight[0] = - INFINITY;
  27.    weight[1] = getweight(a - 1, b - 1);
  28.    weight[2] = getweight(a - 1, b);
  29.    weight[3] = getweight(a, b + 1);
  30.    weight[4] = getweight(a + 1, b + 1);
  31.    weight[5] = getweight(a + 1, b);
  32.    weight[6] = getweight(a, b - 1);
  33.    max = 0;
  34.    for (i = 1; i < 7; ++i)
  35.       if (weight[i] > weight[max]) max = i;
  36.  
  37.    switch (max)
  38.    {
  39.       case 0:
  40.          break;
  41.       case 1:
  42.          --a;
  43.          --b;
  44.          break;
  45.       case 2:
  46.          --a;
  47.          break;
  48.       case 3:
  49.          ++b;
  50.          break;
  51.       case 4:
  52.          ++a;
  53.          ++b;
  54.          break;
  55.       case 5:
  56.          ++a;
  57.          break;
  58.       case 6:
  59.          --b;
  60.          break;
  61.    }
  62.    olda = ogre.l_hex;
  63.    oldb = ogre.r_hex;
  64.    ogre.l_hex = a;
  65.    ogre.r_hex = b;
  66.    update_hex(olda, oldb);
  67.    disp_ogre();
  68.    ogre.moves_left -= 1;
  69. }
  70.  
  71. getweight(a, b)
  72. char a, b;
  73. {
  74.    int weight, total_attacks, to_target, i;
  75.  
  76.    weight = - range(a, b, unit[0].l_hex, unit[0].r_hex);
  77.    total_attacks = ogre.missiles + ogre.main_bats + ogre.sec_bats;
  78.  
  79.    for (i = 0; i < n_units; ++i)
  80.    {
  81.       if (unit[i].status == DESTROYED) continue;
  82.       to_target = range(a, b, unit[i].l_hex, unit[i].r_hex);
  83.       if (to_target == 0)
  84.       {
  85.          if (unit[i].type == CP) weight = 50;
  86.          else weight = 10 * unit[i].attack;
  87.          break;
  88.       }
  89.       if (total_attacks <= 0) continue;
  90.       if (to_target <= RANGE_MISSILES && ogre.missiles > 0)
  91.       {
  92.          weight += unit[i].attack;
  93.          weight += 4 - unit[i].movement;
  94.          total_attacks -= 1;
  95.          continue;
  96.       }
  97.  
  98.       if (to_target <= RANGE_MAIN && ogre.main_bats > 0)
  99.       {
  100.          weight += unit[i].attack;
  101.          weight += 4 - unit[i].movement;
  102.          total_attacks -= 1;
  103.          continue;
  104.       }
  105.  
  106.       if (to_target <= RANGE_SECONDARY && ogre.sec_bats > 0)
  107.       {
  108.          weight += unit[i].attack;
  109.          weight += 4 - unit[i].movement;
  110.          total_attacks -= 1;
  111.          continue;
  112.       }
  113.  
  114.       if (to_target <= RANGE_AP && ogre.ap > 0 && unit[i].type == INFANTRY ||
  115.        unit[i].type == CP)
  116.       {
  117.          weight += unit[i].attack;
  118.          weight += 4 - unit[i].movement;
  119.          total_attacks -= 1;
  120.          continue;
  121.       }
  122.    }
  123.  
  124.    if (off_map(a, b) || blocked(a, b)) weight = - INFINITY;
  125.  
  126.    return(weight);
  127. }
  128.  
  129. #define INCR(i) i = (i == n_units - 1) ? 0 : i + 1
  130. #define MIN(a, b) (((a) > (b)) ? (a) : (b))
  131.  
  132. assign_fire_ogre()
  133. {
  134.    int i, unitno, nmissiles;
  135.  
  136.    init_ogre_attack();
  137.  
  138.    unitno = nextunit(RANGE_AP, 0);
  139.    for (i = 0; i < ogre.ap; ++i)
  140.    {
  141.       if (unit[unitno].range_to_ogre <= RANGE_AP &&
  142.          (unit[unitno].type == CP || unit[unitno].type == INFANTRY))
  143.       {
  144.          unit[unitno].fired += ATK_AP;
  145.          display_attack("AP", unitno);
  146.       }
  147.       unitno = nextunit(RANGE_AP, unitno);
  148.    }
  149.    unitno = nextunit(RANGE_SECONDARY, unitno);
  150.    for (i = 0; i < ogre.sec_bats; ++i)
  151.    {
  152.       if (unit[unitno].range_to_ogre <= RANGE_SECONDARY)
  153.       {
  154.          unit[unitno].fired += ATK_SECONDARY;
  155.          display_attack("secondary battery", unitno);
  156.       }
  157.       unitno = nextunit(RANGE_SECONDARY, unitno);
  158.    }
  159.  
  160.    unitno = nextunit(RANGE_MAIN, unitno);
  161.    for (i = 0; i < ogre.main_bats; ++i)
  162.    {
  163.       if (unit[unitno].range_to_ogre <= RANGE_MAIN)
  164.       {
  165.          unit[unitno].fired += ATK_MAIN;
  166.          display_attack("main battery", unitno);
  167.       }
  168.       unitno = nextunit(RANGE_MAIN, unitno);
  169.    }
  170.  
  171.    unitno = nextunit(RANGE_MISSILES, unitno);
  172.  
  173.    nmissiles  = ogre.missiles;
  174.    for (i = 0; i < nmissiles; ++i)
  175.    {
  176.       if (unit[unitno].status != DESTROYED && unit[unitno].type != INFANTRY
  177.          && unit[unitno].range_to_ogre <= RANGE_MISSILES)
  178.       {
  179.          unit[unitno].fired += ATK_MISSILES;
  180.          ogre.missiles -= 1;
  181.          display_attack("missile", unitno);
  182.          disp_ogre_status(FALSE);
  183.       }
  184.       unitno = nextunit(RANGE_MISSILES, unitno);
  185.    }
  186. }
  187.  
  188. cycle()
  189. {
  190.    Delay(60);
  191. }
  192.  
  193. display_attack(weapon, target)
  194. char *weapon;
  195. int target;
  196. {
  197.    char c[80];
  198.  
  199.    if (unit[target].status == DESTROYED) return;
  200.    sprintf(c, "Ogre fires %s at unit at hex %d%d      ", weapon,
  201.      unit[target].l_hex, unit[target].r_hex);
  202.    movecur(16, 0);
  203.    Amiga_puts(c);
  204.    movecur_hex(unit[target].l_hex, unit[target].r_hex);
  205.    cycle();
  206.    def_resolve(target);
  207. }
  208.  
  209. nextunit(range, unitno)
  210. int range, unitno;
  211. {
  212.    int start;
  213.  
  214.    start = unitno;
  215.    INCR(unitno);
  216.    while (unitno != start)
  217.    {
  218.       if (range == 1)
  219.       {
  220.          if (unit[unitno].status != DESTROYED &&
  221.             (unit[unitno].type == CP || unit[unitno].type == INFANTRY) &&
  222.             unit[unitno].range_to_ogre <= range)
  223.                return(unitno);
  224.       }
  225.       else
  226.       {
  227.          if (unit[unitno].status != DESTROYED && unit[unitno].range_to_ogre
  228.             <= range)   return(unitno);
  229.       }
  230.       INCR(unitno);
  231.    }
  232.    return(unitno);
  233. }
  234.