Difference between revisions of "Template:Lit Radius"

From RimWorld Wiki
Jump to navigation Jump to search
(created template)
 
(added viete's algorithm too (usually not used, but needed to calculate the tiny lit radius of agarilux))
Line 7: Line 7:
 
*3: target light level - defaults to 0.3, which is all you probably want
 
*3: target light level - defaults to 0.3, which is all you probably want
 
===Examples===
 
===Examples===
Example: <code><nowiki>{{Lit Radius|12|{{#expr:217+217+208}}}}</nowiki></code> -> {{Lit Radius|12|{{#expr:217+217+208}}}}
+
*[[standing lamp]]<code><nowiki>{{Lit Radius|12|{{#expr:217+217+208}}}}</nowiki></code> -> {{Lit Radius|12|{{#expr:217+217+208}}}}
 +
*[[glow pod]]<code><nowiki>{{Lit Radius|6|{{#expr:113+141+117}}}}</nowiki></code> -> {{Lit Radius|6|{{#expr:113+141+117}}}}
 +
*[[agarilux]]<code><nowiki>{{Lit Radius|10|{{#expr:23+15+30}}}}</nowiki></code> -> {{Lit Radius|10|{{#expr:23+15+30}}}}
 
Combine with <code>#expr round</code> to improve readability: <code><nowiki>{{#expr: {{Lit Radius|12|{{#expr:217+217+208}}}} round 2}}</nowiki></code> -> {{#expr: {{Lit Radius|12|{{#expr:217+217+208}}}} round 2}}
 
Combine with <code>#expr round</code> to improve readability: <code><nowiki>{{#expr: {{Lit Radius|12|{{#expr:217+217+208}}}} round 2}}</nowiki></code> -> {{#expr: {{Lit Radius|12|{{#expr:217+217+208}}}} round 2}}
 
===Maths===
 
===Maths===
Line 37: Line 39:
 
-->{{#vardefine:a0|{{#expr:0 - {{#var:glow_radius}}*2/3}}}}<!--
 
-->{{#vardefine:a0|{{#expr:0 - {{#var:glow_radius}}*2/3}}}}<!--
 
-->{{#vardefine:a2|{{#expr:{{#var:glow_radius}}*({{#var:f}}*5/3 - 1)}}}}<!--
 
-->{{#vardefine:a2|{{#expr:{{#var:glow_radius}}*({{#var:f}}*5/3 - 1)}}}}<!--
cardano algorithm (a1 = 0)
+
cardano-viete (a1 = 0)
 
-->{{#vardefine:q|{{#expr:0 - {{#var:a2}}^2/9}}}}<!--
 
-->{{#vardefine:q|{{#expr:0 - {{#var:a2}}^2/9}}}}<!--
 
-->{{#vardefine:r|{{#expr:0 - {{#var:a0}}/2 - {{#var:a2}}^3/27}}}}<!--
 
-->{{#vardefine:r|{{#expr:0 - {{#var:a0}}/2 - {{#var:a2}}^3/27}}}}<!--
-->{{#vardefine:u|{{#expr:({{#var:r}} + ({{#var:r}}^2 + {{#var:q}}^3)^(1/2))^(1/3)}}}}<!--
+
-->{{#ifexpr:{{#var:r}}^2 + {{#var:q}}^3 > 0|<!-- cardano
-->{{#vardefine:v|{{#expr:({{#var:r}} - ({{#var:r}}^2 + {{#var:q}}^3)^(1/2))^(1/3)}}}}<!--
+
---->{{#vardefine:u|{{#expr:({{#var:r}} + ({{#var:r}}^2 + {{#var:q}}^3)^(1/2))^(1/3)}}}}<!--
-->{{#vardefine:z1|{{#expr:{{#var:u}} + {{#var:v}} - {{#var:a2}}/3}}}}<!--
+
---->{{#vardefine:v|{{#expr:({{#var:r}} - ({{#var:r}}^2 + {{#var:q}}^3)^(1/2))^(1/3)}}}}<!--
 +
---->{{#vardefine:z1|{{#expr:{{#var:u}} + {{#var:v}} - {{#var:a2}}/3}}}}<!--
 +
-->|<!-- viete
 +
---->{{#vardefine:theta|{{#expr:acos ({{#var:r}}/(0 - {{#var:q}})^(3/2))}}}}<!--
 +
---->{{#vardefine:theta1|{{#expr:{{#var:theta}}/3}}}}<!--
 +
---->{{#vardefine:z1|{{#expr:2 * (0 - {{#var:q}})^(1/2)*{{#expr:cos {{#var:theta1}}}} - {{#var:a2}}/3}}}}<!--
 +
-->}}<!--
  
 
-->{{#expr:{{#var:z1}} - 1}}
 
-->{{#expr:{{#var:z1}} - 1}}
 
</includeonly>
 
</includeonly>

Revision as of 22:25, 13 October 2021

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.