Calculating distance on a map

I’m running a campaign in the Sword Coast, and like to provide consistent travel distance and time to my players. As far as I could find, there are no distance charts. And even if there were, they are unlikely to provide all locations (but I might stand corrected!).

A solution could be a piece of software which can open the high resolution Sword Coast map, in which I can draw lines, and which then tells me the length in pixels of the lines, so I could covert that back into miles, and ultimately derive a travel time.

Does anyone know of something to do this in?


  • Travel Chart for the Forgotten Realms

An efficient way of calculating πœ™(πœ™(p*q)) where p and q are prime

Let p and q be prime numbers and πœ™ Euler’s totient function. Is there an efficient way of computing πœ™(πœ™(p*q)) = πœ™(πœ™((p-1)(q-1)), that is not simply based on factoring (p-1) and (q-1)?

Obviously, if p and q do not equal two, (p-1) and (q-1) are even and consequently their prime factorization is entirely different from the prime factorization of p and q. Therefore I assume that no such shortcut exists.

Do I overlook something?

Calculating percentiles of a FROM command

I have a query which generates a table of trials and values from a database (example pic below)

SELECT * FROM [ReMetrica].[ReMetrica_Result].[GetTrialValuesAsDouble] ('BD5E92F3-3A3B-43B4-BC22-409F34CE5980', 'Total', 'Net Loss', 1) 

which is an expression we’ve been given with this bit of software.

enter image description here

I can calculate the mean of all these trials by using:

SELECT avg(Value) FROM [ReMetrica].[ReMetrica_Result].[GetTrialValuesAsDouble] ('BD5E92F3-3A3B-43B4-BC22-409F34CE5980', 'Total', 'Net Loss', 1) 

However I’m trying to extend this to calculating percentiles as well with

SELECT percentile_cont(Value) OVER [ReMetrica].[ReMetrica_Result].[GetTrialValuesAsDouble] ('BD5E92F3-3A3B-43B4-BC22-409F34CE5980', 'Total', 'Net Loss', 1) 

which doesn’t work due to an ‘incorrect’ syntax after OVER. Could someone help me with where I’m going wrong?

My any-dice program times out, when calculating large limit break checks

Someone in chat helped write an anydice program to calculate limit breaks in an RPG I’m developing, but after making some changes, it times out for dicepools > 7.

The system I have in mind, is that if any of the dice you roll is below a threshold, you can bank the sum of all failed rolls for later use, by converting it into a limit break token (currently, at an exchange rate of 1:4). I’m toying with requiring a certain number of successes before you can convert failed, which may or may not be slowing down the program.

function: sum X:s less than L with at least K successes {   R: 0   S: 0   loop I over X {      if I <= L { R: R + I }      if I > L { S: S + 1 }   }   if S >= K { result: R/4 }   if S < K { result: 0 }  } 

Is there a more efficient way of running this program? Initially before my tweaks, the same helpful person suggested this as an alternative to the function: output 3d{1..6, 0:6} named "Alt dice" but I can’t figure a way of running that, which is probably less likely to time out, and still check for a minimum number of successes.

Here is the code that causes the time out:

output [sum 1d12 less than 7 with at least 0 successes] named "1 die limit break" output [sum 2d12 less than 7 with at least 1 successes] named "2 die limit break" output [sum 3d12 less than 7 with at least 1 successes] named "3 die limit break" output [sum 4d12 less than 7 with at least 1 successes] named "4 die limit break" output [sum 5d12 less than 7 with at least 1 successes] named "5 die limit break" output [sum 6d12 less than 7 with at least 1 successes] named "6 die limit break" \Times out around here\ output [sum 7d12 less than 7 with at least 1 successes] named "7 die limit break" output [sum 8d12 less than 7 with at least 2 successes] named "7 die limit break" output [sum 9d12 less than 7 with at least 2 successes] named "7 die limit break" output [sum 10d12 less than 7 with at least 2 successes] named "7 die limit break" 

I found the timeout point by running each line individually.

Calculating the structural integrity of a pixel grid


So this is a question that came from an idea for a game. This game is voxel-based, and I am interested in calculating structural integrity, with some blocks that break after a limit has been reached.

I know Medieval Engineers and 7 Days to Die implemented something along these lines, though I would like to see if I can solve this (with some help) before ripping someone’s implementation.

Of course links to more examples are appreciated.


In a 2D grid of constant size, there exists an anchor which all pixels/nodes must connect to. For example the bottom row will act as the "ground" and anchor all blocks. Finding unanchored blocks is easy, so assume all blocks are anchored.

Each node/pixel will have a mass, for simplification, this will be "1 block" of mass.

From there we need to find how much load each node is under, and therefore how much "stress".

My Implementation

For every node:

  1. Split the mass of a block based on its valid connections i.e. paths that are not a dead end.
  2. The fraction of mass on the neighbor node represents the fraction of weight of the source node it supports.
  3. Repeatedly traverse splitting the mass until you get every path to an anchor node.

You should end up with a total number on each node representing how much "mass" it is supporting.


Grid Example

Mass distribution of one node

Each color represents the initial path from the source node/pixel for clarity. The source mass is divided into three since the bottom path (in this example one block) cannot reach the anchor without going back on itself.
From this we can see the node with the most stress from the source node is D2, with the runner-up being C2. B3 is not affected by the source because it is not supporting it.

Of course there are limitations with this solution:

  • Bad time complexity
  • Scales poorly with #nodes
  • adding or removing one node is expensive

My solution involves finding every path from source to anchor for every block, which is bad.


How do I improve this algorithm or simplify the model in which stress is calculated so it can perform near real-time?

Calculating the running time of Quicksort’s PARTITION procedure

I am confused about calculating the PARTITION procedure’s running time.

PARTITION procedure is used in the Quicksort Algorithm to partition the array $ A[p…r]$

I analyzed the PARTITION procedure provided in the book Introduction to Algorithms:

  PARTITION(A, p, r) 1.  x= A[r] 2.  i= p-1 3.  for(j = p to r-1) 4.       if(A[j]<= x) 5.         i= i+1 6.         exchange A[i] with A[j] 7.  exchange A[i+1] with A[r] 8.  return i+1 

Now what I figured out that:

  1. Every procedure except lines $ 3-6$ has a constant time i.e: $ \Theta(1)$
  2. For loop would execute $ r-1-p+1$ times.

Thus the running time should be: $ \Theta(r-p)$

While the book quotes:

The running time of PARTITION on the subarray $ A[p…r]$ is $ \Theta(n)$ where $ n= r-p+1$ .

Could someone please help me out in knowing how to prove the aforementioned complexity?

Thank you.

AnyDice — efficiency of code calculating rolls hitting a target with mixed pools; hitting the 5 second barrier

I have some code that is hitting the 5 second barrier;

function: target N:n of A:s B:s C:s {     result: [count {1..N, 1..(N/2)} in [sort {A, B, C}]] } output [target 7 of 4d12 0d20 0d8] output [target 7 of 4d12 2d20 0d8] output [target 7 of 4d12 4d20 0d8] 

Even if I remove the final output line, it still fails.

I believe the code does what I want it to – calculate the number of dice rolling at or under the target from mixed pools (it runs when using other pools: d20s seem to be a problem).

Is there anyway I can improve it so that at least the first two of these output lines will run (or better yet, all three of them)?

N.b. from my perspective these were some of the simplest pools I wanted to look at.

Warding Bond: What is the Order of Operations for calculating cleric damage taken?

The warding bond spell allows a support character (cleric) to buff another creature (Basic Rules p. 105):

While the target is within 60 feet of you, it gains a +1 bonus to AC and saving throws, and it has resistance to all damage. Also, each time it takes damage, you take the same amount of damage.

What is the Order of Operations used to arrive at the final damage that the cleric receives as his share?

Resistance rules brought this up (Basic Rules, p. 75) as we hoped to Ward a raged Barb – MEGA Tank – until our DM reminded us that you don’t double stack like resistances (PHB, p. 197).

If a creature or an object has resistance to a damage type, damage of that type is halved against it.

I’ll be buffing our Paladin.
Two cases:

  1. Weapon damage (a hammer blow, a claw strike, gored by a gorgon …)
  2. Damage where a saving throw versus magical damage (usually a spell or spell like effect) is required.

Case 1. I stay 30′ behind Paladin. Giant scores a hit, doing 16 points of bludgeoning damage. Warding Bond (resistance) reduces that to 8. I take 8 HP.

Case 2. The Wizard whom the Giant serves fireballs the Paladin on his action. (I am outside blast radius). Rolled damage is 24, Fire. Paladin has resistance to all damage (from Bond) thus 24 is halved to 12. He rolls a saving throw, and succeeds with a 17. He takes 6 damage. I take 6 damage.

My view is that we can’t assign damage to Cleric until we know total damage to Paladin.

Paladin and I weren’t sure about the case of fireball: should it be different from the Giant’s hammer, since you don’t get a save versus melee weapon damage? With fireball reduced damage (from resistance) at 12, do I take 12 unless I too save versus fire as the Paladin did?

I don’t think so. It seems to violate the KISS principle. But, the Bond ties the cleric magically to the Paladin. Is magic going to follow that path of least resistance?

Do I have the order of operations right?

  1. First resolve all damage to Paladin.
  2. Then apply that amount to Cleric.

Is there something we missed that would support the other order of operations?

Calculating the XP treshold for a party with “monster” companions

Suppose that a member of an adventuring party has a companion. I can think of the following broad categories …

  • Something that came with his/her class (familiar, beastmaster companion)
  • Part of a spell (summoned/conjured creatures, raised undead, Homunculus, awakened something, etc.)
  • Purchased mount (war horse, hippogriff, etc.)
  • A bound golem

… then are there cases (and perhaps there are others besides the ones I could think of) in which I should I calculate the XP treshold (DMG, page 82) differently (increasing the treshold), and if so, by how much?

Personally, I feel (but I’m quite inexperienced) that companions that originate from classes and spells are already calculated into the character level of the XP difficulty matrix. And that mounts may not be powerful enough to unbalance the encounter difficulty. But a party member that brings a golem, might be a different story.

I’m fairly sure that there is no rule for this, if so, then suggestions are welcome too.

Calculating Damage for the Fling Action

DND 5e. Among my group there seem to be multiple ways of interpreting the damage from the action “Fling”.

From the Kraken:

Fling. One Large or smaller object held or creature grappled by the kraken is thrown up to 60 feet in a random direction and knocked prone. If a thrown target strikes a solid surface, the target takes 3 (1d6) bludgeoning damage for every 10 feet it was thrown. If the target is thrown at another creature, that creature must succeed on a DC 18 Dexterity saving throw or take the same damage and be knocked prone.

The distance for the damage calculated is in question:

If the creature is thrown an intended 60ft but interrupted say 30ft in by a solid surface, is the distance then considered to only be 30ft for the damage calculation? This would seem to be supported by the “up to 60ft” phrasing, but also would then imply that the creature thrown had less momentum from an interpretation of physics simply due to a wall existing in the way.

Or would the damage be considered from the full 60ft of the initial throw? The “up to 60ft” would assert an individual could be thrown less distance by the kraken’s intent, randomly, DM, etc. Then, the damage in question could be based on this.

The interpretation of the “up to” is up to DM discretion. For damage which of these is correct?