Ticket #563 (closed enhancement: fixed)

Opened 3 years ago

Last modified 2 years ago

[PATCH] Improve Animal AI

Reported by: Badmadblacksad Owned by:
Priority: Should Have Milestone: Alpha 4
Component: UI & Simulation Keywords: ai, animal, review
Cc:

Description (last modified by k776) (diff)

We have passive, idle, and skittish.

Add violent, aggressive and defensive behaviors to animalAI.js

Possibly generalize the code so it can be applied to opponent AI (#707)

Attachments

animalAI.patch (16.7 KB) - added by Badmadblacksad 3 years ago.
AnimalAIm-06-02-2010.patch (25.0 KB) - added by Badmadblacksad 2 years ago.
AnimalAImm-16-02-2011.patch (33.0 KB) - added by Badmadblacksad 2 years ago.

Change History

Changed 3 years ago by Badmadblacksad

comment:1 Changed 3 years ago by Philip

  • Keywords review, removed

Thanks, this looks good. My only concern is the growing duplication between AnimalAI and UnitAI - that was originally my fault, and this increases it, and we'll need more once players can capture units and give them move orders. Unless I'm missing some major problem (or some nicer way to manage the complexity), I think it'll be better in the long term to merge the animal behaviour into UnitAI (probably as an extra state group alongside FORMATIONCONTROLLER/FORMATIONMEMBER/INDIVIDUAL) so it can share all the basic functions. Applying this patch will add some complexity and make it harder to merge later, so I'd prefer them to be merged first.

comment:2 Changed 3 years ago by anonymous

  • Milestone Unclassified deleted

Milestone Unclassified deleted

comment:3 Changed 3 years ago by wacko

  • Milestone set to Backlog

comment:4 Changed 3 years ago by fabio

  • Milestone changed from Backlog to Alpha 3

comment:5 Changed 2 years ago by k776

  • Milestone changed from Alpha 3 to Alpha 4

comment:6 Changed 2 years ago by k776

  • Priority changed from minor to critical
  • Description modified (diff)
  • Summary changed from AnimalAI to Improve Animal AI

comment:7 Changed 2 years ago by k776

  • Type changed from defect to enhancement

Changed 2 years ago by Badmadblacksad

comment:8 Changed 2 years ago by Badmadblacksad

  • Keywords animal, review added; animal removed

Here is a new version. Waiting for feedback. tchô :)

comment:9 Changed 2 years ago by k776

  • Priority changed from Must Have to Should Have
  • Summary changed from Improve Animal AI to [PATCH] Improve Animal AI

comment:10 Changed 2 years ago by Philip

Thanks, this looks mostly sensible to me :-) . But I see some issues:

  • What is ANIMAL.FEEDING.Timer trying to do? It looks like it's finding the first entity which was in range during FEEDING.enter and is also in range during FEEDING.Timer, and then attacks it if it's a valid target and otherwise doesn't attack anything (even if the second entity is a good target); but I'm not sure why it's doing all that, instead of just doing a single check in ROAMING.enter or similar.
  • In ANIMAL.FEEDING.Timer, if Riposte returns false then I think the unit will be stuck in FEEDING forever.
  • Duplicating most of the COMBAT logic doesn't seem nice. It looks like the main difference is checking that the target is still visible - probably we should make normal units do the same check, so it doesn't need to be animal-specific. Then ANIMAL.COMBAT can be made a reference to INDIVIDUAL.COMBAT by extracting them into a separate var, or by using sharp variables.
  • ResetActiveQuery is relatively expensive (since it has to return the list of every single entity in range) - the purpose of active queries is that you don't need to repeatedly compute them, you just need to reset them once then listen for incremental LosRangeUpdates. So it shouldn't be used in COMBAT for checking the target is still visible - it should look for LosRangeUpdates where the current target is in msg.data.removed
  • ANIMAL doesn't need the ResourceGather handler (you can't gather resources until the animal has been killed, since we implemented hunting).
  • The AnimalAI changes from r8899 and r8900 need to be merged in. (I suppose that's my fault since I committed them after this patch :-p )
  • Rather than keeping a separate AnimalAI and copying the values into UnitAI, I think it'd be cleaner and more efficient to just put the animal values in the UnitAI schema instead. (Probably put them inside an <optional><interleave> ... </interleave></optional> block so that they can all be omitted, for non-animal units). (Efficiency matters a little because the template values copied into the UnitAI component will be serialised for saved games and sync-checking. Not a major concern, but it's nicer to avoid the redundancy and access the template directly instead of copying values.)
  • FLEEING shouldn't need a Timer, since there's no timer running in it.
  • Nitpicking:
    • this.behavior should be this.behaviour (since we mostly standardise on en-GB spellings). But actually it should probably just be this.template.NaturalBehaviour, given the previous comment about not copying values from templates.
    • Whitespace should be consistent with the existing code (e.g. if(msg.data.added.length>0) should instead be if (msg.data.added.length > 0), and UnitAI.prototype.Riposte should be indented with tabs).
    • template_unit_fauna.xml doesn't need to specify FormationController (it'll inherit the value from template_unit.xml and I think it's preferable to avoid redundancy).

Changed 2 years ago by Badmadblacksad

comment:11 Changed 2 years ago by Badmadblacksad

Hello. I tried to correct the things that Philip pointed out. COMBAT mode is still duplicate, I didn't manage to do better for now.

comment:12 Changed 2 years ago by Philip

Thanks again! I think this looks like an improvement. There's a few small changes I've made:

If a unit is owned by a non-Gaia player (which we'll probably want to support so players can capture animals and make them walk around), some orders will switch it to the INDIVIDUAL state and it'll forget to be an animal. I made it ignore walk orders, and made it unable to garrison, and I think no other orders will be possible (since animals won't have ResourceGatherer or Builder) so hopefully it's okay now.

LeaveFoundation needed to be Order.LeaveFoundation.

I changed the FSM system so the COMBAT sub-state can be shared by animals (and gave animals an IDLE so that FinishOrder works).

The LosRangeUpdate stuff seems a bit unreliable since it's only triggered when a unit enters the range; if two units enter, an aggressive animal will attack one and then never notice the second (unless it leaves and re-enters range). But that's probably okay for now.

Instead of having SetupRangeQuery handle owner == 0 specially, I changed the setup code so Gaia is a diplomatic enemy of everybody, since that seems a bit cleaner.

comment:13 Changed 2 years ago by philip

  • Status changed from new to closed
  • Resolution set to fixed

(In [8995]) # Improve animal AI, based on patch from Badmadblacksad. Fixes #563.

Note: See TracTickets for help on using tickets.