Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#1985 closed enhancement (fixed)

[PATCH] Building attacks should be able to use restricted and preferred classes

Reported by: michael Owned by: sanderd17
Priority: Should Have Milestone: Alpha 15
Component: Core engine Keywords: BuildingAI attack patch
Cc: Patch:

Description (last modified by michael)

Make cmpBuildingAI adhere to cmpAttack restrictions and preferences.

In the template <Attack> element:

<RestrictedClasses datatype="tokens">Organic</RestrictedClasses>
<PreferredClasses datatype="tokens">Gates Structure</PreferredClasses>

Forum Discussion: http://www.wildfiregames.com/forum/index.php?showtopic=17394

Attachments (4)

restrict-and-prefer-building-attacks-1985.diff (3.6 KB ) - added by James Scott 11 years ago.
restrict-and-prefer-building-attacks-1985_using_splice.diff (3.4 KB ) - added by James Scott 11 years ago.
Full diff including splice code review feedback
restrict-and-prefer-building-attacks-1985_using_splice_no_underscores.diff (3.4 KB ) - added by James Scott 11 years ago.
Full diff including splice and underscore property names code review feedback
1985-additional-fixes.diff (3.6 KB ) - added by James Scott 11 years ago.

Download all attachments as: .zip

Change History (20)

comment:1 by michael, 11 years ago

Description: modified (diff)

comment:2 by michael, 11 years ago

Description: modified (diff)

comment:3 by James Scott, 11 years ago

Keywords: review patch added
Summary: Building attacks should be able to use restricted and preferred classes[PATCH] Building attacks should be able to use restricted and preferred classes

This is my stab at implementing preferred classes for Building AI. It includes a new WeightedList JavaScript helper class for the random selection of targets with a preference score.

The weights used for WeightedList are the values returned by GetPreference. This means that a preferred target is 2x as likely to be selected as an regular target. If the attacking building has two preferences the 1st will 3x likely, 2nd 2x and so on. We may want to apply some down-weighting here - I'd be interested in your thoughts.

I wanted to have an instance of WeightedClass replace BuildingAI#targetUnits so that preference weighting would only need to be fetched as targets moved in to range rather than in every call to FireArrows. However this meant that saved games broke. I can attach the extended proposed patch if you're interested.

Restricted classes was added by another dev on a separate ticket but seems to mostly be a reimplementation of Attack#CanAttack so I've substituted that here.

Patch can be viewed on Github at https://github.com/jammus/0ad/compare/0ad:430af4a008...2dde082

Last edited 11 years ago by James Scott (previous) (diff)

comment:4 by sanderd17, 11 years ago

Looks like a very clean solution. I'd replace the randomIndex function with a getRandomElement function. So that saves a line, and I can't think how you'd use it otherwise.

Also, it's not usual to have underscores in front of object related variables in our code, but a dev more acquainted with the code should decide.

Why don't you use the splice function for the removeAt. It gets you the weight to remove, and deletes the right index from the array in one go.

Btw, thanks for fixing that CanAttack (I didn't remember I committed that, and I didn't remember there was an CanAttack function either).

But overall, a very good piece of code.

Last edited 11 years ago by sanderd17 (previous) (diff)

by James Scott, 11 years ago

Full diff including splice code review feedback

by James Scott, 11 years ago

Full diff including splice and underscore property names code review feedback

comment:5 by James Scott, 11 years ago

Thanks sanderd17. Good call with the splice suggestion, it's much cleaner that way. I've also attached a version with the underscore property names removed.

I chose to use randomIndex rather than randomItem so that targets can be easily removed by index if they fail the visibility test (line 306). An alternative would be to add a removeItem(target) method. My concern here was that this would work fine for this case where elements are integers or strings as the filter callback, or findItem internal method, would do a simple equality check. However if more complicated objects were stored in WeightedList this would need some rethink. I'm happy to implement this if preferred.

Github for latest version: https://github.com/jammus/0ad/compare/0ad:430af4a008...0796168

Last edited 11 years ago by James Scott (previous) (diff)

comment:6 by sanderd17, 11 years ago

You're right about the randomIndex. Best would be to leave it that way.

I think it's good enough to commit.

comment:7 by sanderd17, 11 years ago

Owner: set to sanderd17
Resolution: fixed
Status: newclosed

In 13823:

Add preferred classes to BuildingAI. Patch by jammus. Fixes #1985

comment:8 by sanderd17, 11 years ago

Keywords: simple review removed
Milestone: BacklogAlpha 15

comment:9 by mimo, 11 years ago

there are some flaws in the BuildingAI code which prevent it to work as expected :

  • line 292, when we have one PreferredClass, GetPreference returns 0, so the preferred class will have the same weight as the non-preferred ones. Futhermore, if we have several PreferredClasses, GetPreference will return a higher value for the less preferred one, and thus the less preferred one will have higer weight. These two problems would be solved by something like (a value of N=2 would look reasonnable to me)

preference = cmpAttack.GetPreference(target)

if (preference !== null)

weight = 1 + N/(1+preference)

else

weight = 1

  • in templates/template_structure_defense_defense_tower.xml, template_structure_defense_wall_tower.xml and template_structure_military_fortress.xml, the Preferred class should be Organic, and not organic, to be taken into account.
  • Then I think we need a semi-colon at the end of line 292, and it would be better to have variable declarations in separate statements (lines 291-292 and 297-298)

comment:10 by mimo, 11 years ago

Resolution: fixed
Status: closedreopened

comment:11 by James Scott, 11 years ago

Thanks for the spots. Patch incoming.

by James Scott, 11 years ago

Attachment: 1985-additional-fixes.diff added

comment:13 by James Scott, 11 years ago

Keywords: review added

comment:14 by mimo, 11 years ago

Thanks, the patch looks fine now. But while we are at it, it's maybe the opportunity to discuss what should be the preferred targets ? I would change it to "Siege Organic" instead of "Organic".

comment:15 by sanderd17, 11 years ago

Resolution: fixed
Status: reopenedclosed

In 14011:

fix some flaws in the preferred classes. Patch by Jammus, Fixes #1985

comment:16 by sanderd17, 11 years ago

Keywords: review removed
Note: See TracTickets for help on using tickets.