home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / enemy.e < prev    next >
Text File  |  1994-01-02  |  10KB  |  389 lines

  1. -- enemy.e
  2. -- operate the enemy ships
  3.  
  4. procedure set_basic_color(natural c)
  5. -- set new color and "shape" for BASIC ships after truce/hostile change
  6.     sequence shape, new_shape
  7.  
  8.     object_color[BASIC_COL] = c
  9.     object_color[BASIC_COL+1] = c
  10.     for i = 2 to length(quadrant) do
  11.     if quadrant[i][Q_TYPE] = G_BAS then
  12.         shape = read_screen({quadrant[i][Q_X], length(BASIC_L)}, 
  13.                      quadrant[i][Q_Y])
  14.         -- reprint with new shape & color
  15.         if compare(shape, BASIC_L) = 0 then
  16.             new_shape = ship[G_BAS][1]
  17.         else
  18.             new_shape = ship[G_BAS][2]
  19.         end if
  20.         write_screen(quadrant[i][Q_X], quadrant[i][Q_Y], new_shape)
  21.     end if
  22.     end for
  23. end procedure
  24.  
  25. global procedure task_bstat()
  26. -- independent task: BASIC status change
  27.  
  28.     positive_atom w
  29.  
  30.     w = rand(400) + rand(400) + 20
  31.     if bstat = TRUCE then
  32.     if truce_broken then
  33.         truce_broken = FALSE
  34.         msg("TRUCE BROKEN!")
  35.     else
  36.         msg("BASIC STATUS: HOSTILE")
  37.     end if
  38.     if rand(20) < 16 then
  39.         w = w * 1.2
  40.         ship[G_BAS] = {BASIC_L, BASIC_R}
  41.         bstat = HOSTILE
  42.     else
  43.         w = w * .6
  44.         ship[G_BAS] = repeat(repeat(INVISIBLE_CHAR, length(BASIC_L)), 2)
  45.         bstat = CLOAKING
  46.     end if
  47.     set_basic_color(BLUE)
  48.     else
  49.     if rand(20) < 10 then
  50.         bstat = TRUCE
  51.         msg("BASIC STATUS: TRUCE")
  52.         w = w * .83
  53.         ship[G_BAS] = {BASIC_L, BASIC_R}
  54.         set_basic_color(LIGHT_BLUE)
  55.     else
  56.         if bstat = HOSTILE then
  57.         w = w * .6
  58.         bstat = CLOAKING
  59.         ship[G_BAS] = repeat(repeat(INVISIBLE_CHAR, length(BASIC_L)), 2)
  60.         else
  61.         w = w * 1.2
  62.         bstat = HOSTILE
  63.         ship[G_BAS] = {BASIC_L, BASIC_R}
  64.         end if
  65.         set_basic_color(BLUE)
  66.     end if
  67.     end if
  68.     wait[TASK_BSTAT] = w
  69.     if scanon then
  70.     gtext()
  71.     end if
  72. end procedure
  73.  
  74. procedure orient(valid_quadrant_row row)
  75. -- point the ship toward its target
  76.  
  77.     quadrant_row targ
  78.     h_coord targx, rowx
  79.     v_coord rowy
  80.     object_type t
  81.  
  82.     targ = quadrant[row][Q_TARG]
  83.     if targ = -1 then
  84.     -- no target
  85.     return
  86.     end if
  87.     targx = quadrant[targ][Q_X]
  88.     rowx = quadrant[row][Q_X]
  89.     rowy = quadrant[row][Q_Y]
  90.     t = quadrant[row][Q_TYPE]
  91.     if rowx < targx then
  92.     write_screen(rowx, rowy, ship[t][2])
  93.     else
  94.     write_screen(rowx, rowy, ship[t][1])
  95.     end if
  96. end procedure
  97.  
  98. procedure shoot(valid_quadrant_row row)
  99. -- select torpedo or phasor for enemy shot
  100.  
  101.     natural torp
  102.     positive_atom pen
  103.  
  104.     shooter = row
  105.     if quadrant[shooter][Q_TYPE] != G_BS then
  106.     orient(shooter)
  107.     end if
  108.     setpt(shooter)
  109.     torp = quadrant[shooter][Q_TORP]
  110.     if torp > 0 and rand(4) = 1 then
  111.     quadrant[shooter][Q_TORP] = torp - 1
  112.     weapon(W_TORPEDO, 4000)
  113.     else
  114.     pen = quadrant[shooter][Q_EN] / 8
  115.     if quadrant[shooter][Q_TYPE] = G_FOR then
  116.         fortran_phasor(pen)
  117.     else
  118.         weapon(W_PHASOR, pen)
  119.     end if
  120.     quadrant[shooter][Q_EN] = quadrant[shooter][Q_EN] - pen
  121.     end if
  122. end procedure
  123.  
  124.  
  125. global procedure task_fire()
  126. -- independent task: select an enemy ship for firing
  127.  
  128.     quadrant_row row
  129.     natural rate
  130.     quadrant_row targ
  131.  
  132.     if length(quadrant) = 1 then
  133.     return -- nobody in the quadrant
  134.     end if
  135.  
  136.     row = rand(length(quadrant)-1) + EUPHORIA  -- choose a random ship
  137.     if quadrant[row][Q_TYPE] = DEAD then
  138.         row = rand(length(quadrant)-1) + EUPHORIA  -- try again
  139.         if quadrant[row][Q_TYPE] = DEAD then
  140.         return
  141.     end if
  142.     end if
  143.     rate = quadrant[row][Q_FRATE]
  144.     if rate > rand(256) then
  145.     -- shoot
  146.     if quadrant[row][Q_TYPE] = G_BAS then
  147.         if bstat != TRUCE then
  148.         shoot(row)
  149.         else
  150.         if basic_targ != EUPHORIA then
  151.             if quadrant[basic_targ][Q_TYPE] != G_BS then
  152.             shoot(row)
  153.             end if
  154.         end if
  155.         end if
  156.  
  157.     elsif quadrant[row][Q_TYPE] = G_BS then
  158.         targ = quadrant[row][Q_TARG]
  159.         if targ != -1 then
  160.         if bstat != TRUCE then
  161.             shoot(row)
  162.         elsif quadrant[targ][Q_TYPE] != G_BAS then
  163.             shoot(row)
  164.         end if
  165.         end if
  166.  
  167.     else
  168.         shoot(row)
  169.     end if
  170.     end if
  171. end procedure
  172.  
  173.  
  174. global procedure task_move()
  175. -- independent task: select an enemy ship for moving
  176.  
  177.     quadrant_row row
  178.     natural mrate
  179.     h_coord fx
  180.     v_coord fy
  181.     extended_h_coord xtry
  182.     extended_v_coord ytry
  183.     sequence uchar, schar
  184.     natural t
  185.     natural len
  186.  
  187.     if length(quadrant) = 1 then
  188.     return -- nobody in the quadrant
  189.     end if
  190.  
  191.     row = rand(length(quadrant)-1) + EUPHORIA  -- choose a random ship
  192.     t = quadrant[row][Q_TYPE]
  193.     if t = DEAD then
  194.     return
  195.     end if
  196.     mrate = quadrant[row][Q_MRATE]
  197.     if mrate > rand(256) then
  198.     -- try to move
  199.     fx = quadrant[row][Q_X]
  200.     xtry = fx + rand(5) - 3
  201.     len = length(ship[t][1])
  202.     if xtry >= 2 and xtry <= HSIZE - len then
  203.         fy = quadrant[row][Q_Y]
  204.         ytry = fy + rand(3) - 2
  205.         if ytry >= 1 and ytry <= VSIZE then
  206.         schar = read_screen({xtry, len}, ytry)
  207.         if not find(FALSE, schar = ' ' or schar = STAR) then
  208.             uchar = quadrant[row][Q_UNDER]
  209.             quadrant[row][Q_UNDER] = schar
  210.             schar = read_screen({fx, len}, fy)
  211.             write_screen(fx, fy, uchar)
  212.             write_screen(xtry , ytry, schar)
  213.             quadrant[row][Q_X] = xtry
  214.             quadrant[row][Q_Y] = ytry
  215.         end if
  216.         end if
  217.     end if
  218.     orient(row)
  219.     end if
  220. end procedure
  221.  
  222. function add2quadrant(object_type t, h_coord x, v_coord y)
  223. -- add a ship to the quadrant sequence 
  224.  
  225.     quadrant_row targ
  226.     valid_quadrant_row row
  227.     sequence c
  228.  
  229.     -- try to reuse a place in quadrant sequence
  230.     row = 1
  231.     for i = 2 to length(quadrant) do
  232.     if quadrant[i][Q_TYPE] = DEAD then
  233.        row = i
  234.        exit
  235.     end if
  236.     end for
  237.     if row = 1 then
  238.     -- all slots in use - add a new row
  239.     quadrant = append(quadrant, repeat(0, length(quadrant[1])))
  240.     row = length(quadrant)
  241.     end if
  242.  
  243.     -- choose his target
  244.     if t < G_BAS then
  245.     if galaxy[qrow][qcol][G_BS] then
  246.         for r = 2 to length(quadrant) do
  247.         if quadrant[r][Q_TYPE] = G_BS then
  248.             targ = r
  249.             exit
  250.         end if
  251.         end for
  252.     else
  253.         targ = EUPHORIA
  254.     end if
  255.     elsif t = G_BAS then
  256.     if basic_targ = -1 then
  257.         if galaxy[qrow][qcol][G_BS] then
  258.         for r = 2 to length(quadrant) do
  259.             if quadrant[r][Q_TYPE] = G_BS then
  260.             basic_targ = r
  261.              exit
  262.             end if
  263.         end for
  264.         else
  265.         basic_targ = EUPHORIA
  266.         end if
  267.     end if
  268.     targ = basic_targ
  269.     else
  270.     targ = fortran_target(row)
  271.     end if
  272.  
  273.     quadrant[row] = stdtype[t]
  274.     quadrant[row][Q_X] = x
  275.     quadrant[row][Q_Y] = y
  276.     quadrant[row][Q_UNDER] = read_screen({x, length(ship[t][1])}, y)
  277.     quadrant[row][Q_TARG] = targ
  278.     if x < quadrant[EUPHORIA][Q_X] then
  279.     c = ship[t][2]
  280.     else
  281.     c = ship[t][1]
  282.     end if
  283.     write_screen(x, y, c)
  284.     return TRUE
  285. end function
  286.  
  287. global procedure task_enter()
  288. -- independent task: enemy ship enters quadrant
  289.  
  290.     natural q
  291.     h_coord enterx
  292.     v_coord entery
  293.     natural entert
  294.     sequence enterc
  295.     g_index randcol, randrow, fromcol, fromrow
  296.  
  297.     wait[TASK_ENTER] = 3 + rand(20) * (curwarp > 2) + 
  298.                quadrant[EUPHORIA][Q_EN]/(2000 + rand(6000))
  299.     if rand(4+8*(level = 'n')) != 1 then
  300.     return -- adjust wait time only
  301.     end if
  302.  
  303.     for i = 1 to 2 do
  304.         entert = 0
  305.         fromrow = qrow
  306.         fromcol = qcol
  307.         enterx = 2
  308.         entery = 1
  309.         q = rand(8)
  310.         if q = 1 then     -- left
  311.         fromcol = gmod(qcol-1)
  312.         entery = rand(VSIZE)
  313.         elsif q = 2 then  -- top left
  314.         fromrow = gmod(qrow-1)
  315.         fromcol = gmod(qcol-1)
  316.         elsif q = 3 then  -- top
  317.         enterx = 1 + rand(HSIZE - MAX_SHIP_WIDTH)
  318.         fromrow = gmod(qrow-1)
  319.         elsif q = 4 then  -- top right
  320.         enterx = HSIZE - MAX_SHIP_WIDTH
  321.         fromrow = gmod(qrow-1)
  322.         fromcol = gmod(qcol+1)
  323.         elsif q = 5 then  -- right
  324.         enterx = HSIZE - MAX_SHIP_WIDTH
  325.         entery = rand(VSIZE)
  326.         fromcol = gmod(qcol+1)
  327.         elsif q = 6 then  -- bottom right
  328.         enterx = HSIZE - MAX_SHIP_WIDTH
  329.         entery = VSIZE
  330.         fromrow = gmod(qrow+1)
  331.         fromcol = gmod(qcol+1)
  332.         elsif q = 7 then  -- bottom
  333.         enterx = 1 + rand(HSIZE - MAX_SHIP_WIDTH)
  334.         entery = VSIZE
  335.         fromrow = gmod(qrow+1)
  336.         else              -- bottom left
  337.         entery = VSIZE
  338.         fromrow = gmod(qrow+1)
  339.         fromcol = gmod(qcol-1)
  340.         end if
  341.         if galaxy[fromrow][fromcol][G_CPP] then
  342.         entert = G_CPP -- two tries to pick C++ to enter
  343.         exit
  344.         end if
  345.     end for
  346.     if entert = G_CPP then
  347.     -- C++
  348.     elsif galaxy[fromrow][fromcol][G_ANC] then
  349.     entert = G_ANC
  350.     elsif galaxy[fromrow][fromcol][G_KRC] then
  351.     entert = G_KRC
  352.     else
  353.     randcol = rand(G_SIZE)
  354.     randrow = rand(G_SIZE)
  355.     if randcol != qrow or randcol != qcol then
  356.         if galaxy[randrow][randcol][G_FOR] then
  357.         fromrow = randrow
  358.         fromcol = randcol
  359.         enterx = 1 + rand(HSIZE-MAX_SHIP_WIDTH)
  360.         entery = rand(VSIZE)
  361.         entert = G_FOR
  362.         end if
  363.     end if
  364.     end if
  365.     if entert = 0 then
  366.     if galaxy[fromrow][fromcol][G_BAS] then
  367.         entert = G_BAS
  368.     end if
  369.     end if
  370.     enterc = read_screen({enterx, MAX_SHIP_WIDTH}, entery)
  371.     if find(TRUE, enterc != ' ' and enterc != STAR) then
  372.     entert = 0
  373.     end if
  374.     if entert then
  375.     if add2quadrant(entert, enterx, entery) then
  376.         galaxy[qrow][qcol][entert] = galaxy[qrow][qcol][entert] + 1
  377.         galaxy[fromrow][fromcol][entert] = galaxy[fromrow][fromcol][entert]
  378.                          - 1
  379.         if entert < G_BAS then
  380.         upg(qrow, qcol)
  381.         gsbox(qrow, qcol)
  382.         upg(fromrow, fromcol)
  383.         end if
  384.         fmsg("%s HAS ENTERED QUADRANT", {otype[entert]})
  385.     end if
  386.     end if
  387. end procedure
  388.  
  389.