Template:Lit Radius

From RimWorld Wiki
Revision as of 22:25, 13 October 2021 by Cheldra (talk | contribs) (added viete's algorithm too (usually not used, but needed to calculate the tiny lit radius of agarilux))
Jump to navigation Jump to search

Calculates the max distance of tiles away from a light source that tiles can be before they fall below 30% light (lit). This value rounded down is the last tile in a straight line that will count as lit - this distance doesn't include the light source's tile itself.

Parameters

  • 1: glowRadius of the light source (e.g. 12 for standing lamp)
  • 2: sum of the glowColor (e.g. 217+217+208 for standing lamp)
  • 3: target light level - defaults to 0.3, which is all you probably want

Examples

  • standing lamp{{Lit Radius|12|{{#expr:217+217+208}}}} -> 9.092558072384
  • glow pod{{Lit Radius|6|{{#expr:113+141+117}}}} -> 3.4808904283651
  • agarilux{{Lit Radius|10|{{#expr:23+15+30}}}} -> 0.0029188183233999

Combine with #expr round to improve readability: {{#expr: {{Lit Radius|12|{{#expr:217+217+208}}}} round 2}} -> 9.09

Maths

The light value (glow) a certain distance away from a light source is determined as a combination of a linear decay based on distance relative to glowRadius (a), and the inverse of the distance squared (b). These two numbers are combined with 60% from (a) and 40% from (b) to give a multiplier (f) to the sum of the glowColors, which then directly determines the light value at the tile.

r = glowRadius 
d = distance + 1
c = sum(glowColors)

a = 1 - d/r
b = 1/(d*d)
f = a + (b-a)*0.4

glow = max(0, min(0.5, f*c/3/255*3.6))

The middle equations can be rearranged into a cubic equation in (d) in terms of (r) and (f), and the bottom equation can be rearranged for (f) in terms of (c) and (glow), provided 0 < (glow) < 0.5:

0 = -r*2/3 + r*(f*5/3 - 1)*d^2 + d^3

f = glow/c*3*255/3.6

This template substitutes (f) into the cubic equation and solves it using Cardano's algorithm.