Advertisement
ArranUbels

Untitled

Oct 14th, 2012
2,879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 6.13 KB | None | 0 0
  1. indexing
  2.     description: "Dragon actors"
  3.     author: "Remlie Ortencio"
  4.     date: "13-10-2012"
  5.     last_modified: "13-10-2012"
  6.  
  7. class
  8.     DRAGON
  9.  
  10. inherit
  11.     HOBBIT_ACTOR
  12.         redefine
  13.             make, act
  14.         end
  15.  
  16. create {HOBBIT_OBJECT_FACTORY} -- a DRAGON can only be created by a
  17.                                -- HOBBIT_OBJECT_FACTORY
  18.     make
  19.  
  20. feature{NONE} -- private attributes
  21.  
  22.     sleeping: BOOLEAN -- check on whether dragon is awake or asleep
  23.     hoard_location: LOCATION -- records where dragon's hoard is to allow it to return
  24.     travel_direction: INTEGER -- index into directions
  25.     next_location: LOCATION -- where the dragon is to move next
  26.     attack_command: ATTACK_COMMAND -- our own private attack command, which we will not make visible to others
  27.  
  28. feature{NONE} -- creation
  29.     make(new_name: STRING; new_description: STRING; the_simulation: HOBBIT_SIMULATION) is
  30.         local
  31.             weapon: OBJECT
  32.             take_command: TAKE_COMMAND
  33.         do
  34.             precursor(new_name, new_description, the_simulation)
  35.  
  36.             -- give the dragon an "active attack" command, which it will use to attack other actors it encounters
  37.             -- this is different from the "passive" attack commands that all HOBBIT_ACTOR actors have, that allows them to be attacked
  38.             create attack_command.make ("active attack", "<actor>")
  39.             attack_command.set_actor(current)
  40.  
  41.             -- Give the dragon jaws of fire, as blasting actors with fire is what they do best
  42.             -- Handled this way for simplicity, will need to handle deletion of weapon upon defeat (if ever possible, assumed immortal from currently implemented attacks)
  43.             weapon := simulation.object_factory.create_object(new_name + "'s Jaws of Fire", "the dragon's source of deathly hot flames")
  44.             weapon.set_symbol('') -- research on what this means and what symbol to use
  45.             create take_command.make("take", "<object>")
  46.             take_command.set_object(weapon)
  47.             weapon.add_command(take_command)
  48.             weapon.set_points(999,999) -- assuming this is damage points, research required
  49.             take_command.set_actor(current)
  50.             take_command.execute -- doing it this way ensures that house-keeping is done
  51.  
  52.             -- initialize behaviour
  53.             sleeping := TRUE -- sleeps and does nothing until it wakes
  54.         end -- make
  55.  
  56. feature -- game action
  57.     act is
  58.             -- the Dragon behaviour for The Hobbit game
  59.         require else
  60.             valid_location: location /= void -- the location must be set before the first call to act, as this defines the dragon's hoard's location
  61.         local
  62.             wake_chance: INTEGER -- holds a value between 1 to 10 to determine when dragon wakes
  63.         do
  64.             -- ### Initial Behaviour
  65.             if hoard_location = void then
  66.                 hoard_location := location -- set the hoard location on the first call to act
  67.             end
  68.  
  69.             if sleeping = TRUE then
  70.                 -- Dragon is still asleep
  71.                 -- code to wake up dragon at a 10% chance here
  72.                 wake_chance := simulation.random_generator.random_value_from_range(1, 10)
  73.                 if wake_chance = 1 then
  74.                     sleeping := FALSE
  75.                     simulation.interface.put_message(name + " has woken up!")
  76.                     set_up_travel -- decides where to go upon waking
  77.                 end -- inner if
  78.             else
  79.                 move
  80.                 if location = hoard_location then
  81.                     sleeping := TRUE
  82.                 else
  83.                     attack
  84.                 end -- else if
  85.             end -- outer if
  86.         end -- act
  87.  
  88. feature{NONE} -- private meathods
  89.     set_up_travel is
  90.             -- method to decide where the dragon will move to next
  91.         local
  92.             location_check: LOCATION
  93.         do
  94.             travel_direction := simulation.random_generator.random_value_from_range(directions.lower, directions.upper)
  95.             location_check := location.get_path(directions.item(travel_direction))
  96.             if location_check = void then
  97.                 set_up_travel -- should call itself until a valid direction is found, for when dragon is sleeping at edge of grid
  98.             end
  99.         end -- set_up_travel
  100.  
  101.     move is
  102.             -- handles dragon moving from hoard and back
  103.             -- navigation used to return to hoard here is the same as the method used in the REPLICATOR class
  104.             -- this form of navigation is used in case dragon encounters water (while unlikely, used just in case)
  105.             -- Note: does not handle if dragon is moving against water current
  106.         local
  107.             next_location: LOCATION
  108.             current_direction: INTEGER
  109.         do
  110.             if location /= hoard_location then
  111.                 from
  112.                     current_direction := directions.lower
  113.                 until
  114.                     current_direction >= directions.upper
  115.                 loop
  116.                     next_location := get_closer_location(current_target.location, next_location, location.get_path(directions.item(current_direction)))
  117.                     current_direction := current_direction + 1
  118.                 end -- loop
  119.             else
  120.                 next_location := location.get_path(directions.item(travel_direction))
  121.             end
  122.             set_location(next_location)
  123.         end -- move
  124.  
  125.     attack is
  126.             -- handles dragon attacking when it finds visible actor(s) in the area it is currently in
  127.     local
  128.         obj_list: LINKED_LIST[STRING]
  129.         obj: OBJECT
  130.         attack_obj_list: LINKED_LIST[STRING]
  131.         number_of_attack_areas: INTEGER
  132.     do
  133.         obj_list := location.list_contents
  134.         if obj_list /= void then
  135.             from
  136.                 obj_list.start
  137.             until
  138.                 obj_list.off
  139.             loop
  140.                 obj := location.get_contents(object_name_from_list_entry(obj_list.item))
  141.                 if obj.team_id /= simulation.default_team_id and obj.team_id /= team_id then
  142.                     -- we have found something to attack
  143.                     -- replace following code with code to attack all actors in the area, invisible or not
  144.                     --attack_command.set_object(obj)
  145.                     --attack_command.execute
  146.                     simulation.interface.put_message("An attack has been launched! " + name + weapon.get_name + " damages everyone in the area!")
  147.                     number_of_attack_areas := 1 -- fixed assuming there is 9 areas, one in each of the 8 directions and the current area
  148.                     from
  149.                         number_of_attack_areas = 1
  150.                     until
  151.                         number_of_attack_areas = 9
  152.                     loop
  153.  
  154.                     end
  155.                 end -- if
  156.                 obj_list.forth
  157.             end -- loop
  158.         end -- if
  159.     end -- attack
  160.  
  161.     -- TODO:
  162.     -- Make Dragon sleep with 10% chance of waking
  163.     -- When awake, make it move in one random direction, then check if actor can be seen in the area the dragon moved to
  164.     -- if visible actor exists, then attack, which also damages all actors in the area and all adjacent areas
  165.     -- after moving/attacking, go back to hoard
  166.     -- add treasure objects (look at hobbit_application object_factory)
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement