Automatically determine flank

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Automatically determine flank

Post by xavram »

I am going to put in the time to see if Maptool can automatically determine if Flanking (D&D and Pathfinder standard rules) is in effect given that Maptool knows who is attack and who the target is.

It strikes me that there is ALOT to this, especially when you start taking into account things like different sized tokens or weapons with a longer reach.

So I was wondering, before I burn the midnight oil figuring this out...has anyone already solved this problem? And if so, can you share some code so I can see how you skinned this cat?

Thanks!

User avatar
bubblobill
Giant
Posts: 167
Joined: Sun Jan 24, 2010 3:07 pm

Re: Automatically determine flank

Post by bubblobill »

Bone White wrote some code for determining flanking angles.
Check out viewtopic.php?f=20&t=19856&start=15#p228859
Bubblobill on the forum.
@Reverend on the MapTool Discord Server

Responsible for less atrocities than most... I do accept responsibility for these though: SmartDoors, Simple d20 Framework - barebones, Drop-in: All 3.5 SRD Monsters, Drop in: Simple Character Editor, Battletech Framework

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Automatically determine flank

Post by xavram »

Not quite what I'm looking forward, that link is for actual, math angles.

What I'm looking for is...

Standard grid, no facing for tokens.
Token A is to the left of Target...token B is to the right of Target.

How can Token A know that its target is being flanked by Token B?

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Automatically determine flank

Post by Full Bleed »

Here are a couple imperfect solutions (for example, none of these takes reach into account):

viewtopic.php?f=20&t=14915

I'm fairly certain that I still, principally, use that code in my attack macros with a check-box for a manual override to apply flanking for outliers. But this is something that's been around for a decade (heck, I had to go in and edit the the post to use the bbcode CODE support since the spoiler tag we used to use back then is no longer supported)...

It looks like Alias may have improved on it with this (though I can't comment on that specifically):

viewtopic.php?f=20&t=22327


It's too bad that MT doesn't have something more integrated though. Calculating flanking is one of the slower parts of attack macros and its lack of support with regard to "flanking reach" is a pretty significant drawback.

Who knows, maybe some newer devs will consider taking a swing at optimizing this process. ;)
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

User avatar
bubblobill
Giant
Posts: 167
Joined: Sun Jan 24, 2010 3:07 pm

Re: Automatically determine flank

Post by bubblobill »

You can get the PC tokens within a defined range of the target;

Code: Select all

[h: allies=getTokens('json', json.set('', 'range', json.set('', 'token', target, 'distancePerCell', 0, 'upto', x)))]
[h: allies=json.remove(allies, attacker)]
You can get the attacker's and target's locations using;

Code: Select all

[h: originX=getTokenX(0, target)]
[h: originY=getTokenY(0, target)]
[h: attX=getTokenX(0, attacker)]
[h: attY=getTokenY(0, attacker)]
For a square grid, the flanker needs to be diametrically opposed.

Code: Select all

[h: flankingX=2*originX-attX]
[h: flankingY=2*originY-attY]
Loop through the nearby PCs to see if they occupy the space;

Code: Select all

[r, foreach(ally, allies), code:{
	[if(getTokenX(0, ally)==flankingX && getTokenY(0, ally)==flankingY): getName(ally)+'is flanking the target.';'']
}]
Throw in some tests for reach, state, etc. and adjust accordingly and you have flanking detection. If you are not using snap-to grid then you would need to get co-ordinates in pixels instead of cells and test for a range.

Written as a UDF isFlankedSTG(attacker, target):

Code: Select all

[h: attacker=findToken(arg(0))][h: target=findToken(arg(1))]

[h: allies=getTokens('json', json.set('', 'range', json.set('', 'token', target, 'distancePerCell', 0, 'upto', 3)))]
[h, if(json.contains(allies, attacker)): allies=json.remove(allies, json.indexOf(allies, attacker));'']

[h: originX=getTokenX(0, target)]
[h: originY=getTokenY(0, target)]
[h: attX=getTokenX(0, attacker)]
[h: attY=getTokenY(0, attacker)]
[h: flankingX=2*originX-attX]
[h: flankingY=2*originY-attY]

[h, foreach(ally, allies), code:{
	[if(getTokenX(0, ally)==flankingX && getTokenY(0, ally)==flankingY): return(0, 1);'']
}]
[h: macro.return=0]
Bubblobill on the forum.
@Reverend on the MapTool Discord Server

Responsible for less atrocities than most... I do accept responsibility for these though: SmartDoors, Simple d20 Framework - barebones, Drop-in: All 3.5 SRD Monsters, Drop in: Simple Character Editor, Battletech Framework

User avatar
metatheurgist
Dragon
Posts: 364
Joined: Mon Oct 26, 2009 5:51 am

Re: Automatically determine flank

Post by metatheurgist »

Or you could just eyeball it and say "Yeah, that's flanking". This scenario is where I think you're running into diminishing returns. Don't forget you need to check for all the abilities that negate flanking.

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Automatically determine flank

Post by xavram »

bubblobill, I'm actually working on a solution that's very similar to what you've outlined...but man, the issues of different sized tokens is really making things hard! We'll see how it goes!

thanks for suggestions all!

Post Reply

Return to “MapTool”