Page 1 of 1

wild animal killing bonus

Posted: Fri Jun 01, 2018 3:43 am
by Dino the Dinosore
I'm playing with the new wild animal feature in my 2.6 ruleset ( see this thread - http://forum.freeciv.org/f/viewtopic.php?f=12&t=705 )
To balance the bad effects with something good, I'd like to have killing an animal give a bonus, like when you kill a barbarian leader. But I can't figure out a way to do that. The script event "unit_lost()" doesn't tell who killed the unit, and I don't see any way for a xxx.ruleset file to do it. Is this not possible?

Re: wild animal killing bonus

Posted: Fri Jun 01, 2018 6:37 am
by Caedo
Did you try giving the animal units the Barbarian Leader flag? I mean, it would make an animal worth the same amount as a barbarian leader, but it would be a reward.

Alternatively, you could just put something on the tile where the animal died that somehow gives a bonus. There might be some voodoo-coding approaches to do this, but I can't think of a good one right now.

Re: wild animal killing bonus

Posted: Mon Jun 04, 2018 9:35 pm
by Dino the Dinosore
Tried the Barbarian Leader flag - it sort of works, but there are problems in addition to the amount being the same as a barbarian leader. The message shown says "Barbarian leader killed" and i don't think that can be changed. And it doesn't work for animals on ocean tiles (like my Kraken), ruleset fails to load. I'd also like to reward a culture point in addition to 10 gold.
I have another elaborate idea to try - use the "action_started_unit_unit" event to see if an animal is involved, if so, store it and its foe in a "database" (a lua table), then when get a "unit_lost" event, look up the unit in the "database" to find the foe. Also have to use the "action_started_unit_units" event because there might be a stack of animals, or an animal attacking a stack. A lot of coding - maybe it will work.

Re: wild animal killing bonus

Posted: Tue Jul 10, 2018 3:35 am
by Dino the Dinosore
The "action_started_unit_unit" event doesn't work - it's only for diplomat actions and doesn't get triggered when units attack. I have another idea to try next.

Re: wild animal killing bonus

Posted: Tue Jul 10, 2018 6:32 am
by Caedo
Dino the Dinosore wrote:The "action_started_unit_unit" event doesn't work - it's only for diplomat actions and doesn't get triggered when units attack.

I believe this is different (and should work) in current developmental versions, since attacks have been unified with diplomat and caravan actions (along with a bunch of other stuff).

Re: wild animal killing bonus

Posted: Fri Jul 13, 2018 5:38 pm
by Dino the Dinosore
I was using freeciv 2.6.0b2, I'll try the 2.6.0-final which is scheduled to come out soon. (this weekend?)

Re: wild animal killing bonus

Posted: Sun Jul 22, 2018 6:00 pm
by Dino the Dinosore
Found out that attack will be an action in freeciv-3.0. So my script using "action_started_unit_unit" event won't work with 2.6 and will have to wait.

Meanwhile, I've got an inferior pal B working. Iterate the tiles around the killed animal's tile, iterate the units on those tiles, and find the owner of those units. If there's only one owner, that's the killer. The loophole is there could be units from >1 owners, then there's no way to tell which one was the killer. Here's the script code -

Code: Select all

function unit_lost_callback(unit, loser, reason)
  num_owners = 0
  owner = ""
  if reason == "killed" then
    nation = loser.nation:name_translation()
    if nation == "Animal Kingdom" then
      for tile in unit.tile:square_iterate(1) do
        for foe in tile:units_iterate() do
          if unit.tile ~= tile then
            foe_nation = foe.owner.nation:name_translation()
            if owner ~= foe.owner and foe_nation ~= "Animal Kingdom" then
              num_owners = num_owners + 1
              owner = foe.owner
            end
          end
        end
      end
    end

    if num_owners == 1 then
      gold = 10
      culture = 1
      edit.change_gold(owner, gold)
      edit.add_player_history(owner, 1)
      if owner:is_human() then
        notify.player(owner,
                    "Wild animal killed, reward is %d culture point and %d gold.", culture, gold)
      end
    end
  end

  -- continue processing
  return false
end


signal.connect("unit_lost", "unit_lost_callback")