Non-threaded

Forums » Advanced Editor Forum » Read Thread

Get help from the experts on variables, scripts, items, and other scary things.

Square roots in the editor?

17 hours ago
Still in the process of writing my first work here, which is partially a story, partially a combat simulator. Last night I created and tested an "alpha" version of my combat system. Today I'm reviewing the system and making changes to balance it out. The core of the combat system is dice rolling; there is a player dice roll and an opponent dice roll. After that, the script I wrote checks for "mismatches" between the player's skills and their opponent's skills. Any mismatches in the favor of the player adds a certain bonus to their dice roll, and vice versa for the opponent. This is obviously a very simplified explanation of the system, but I wanted to give a fair amount of context. Presently this mismatching reward system is a linear bonus of +5, +10, etc. to a combatant's dice roll. For reasons relating to the formula I created, I wanted to incorporate square root values into the system. It doesn't seem that the Advanced Editor has native sqrt support, but I'm hoping the dream may not be dead if there are workarounds anyone knows. Thanks!

Square roots in the editor?

13 hours ago
I don’t think there is any support for sqrt, but you may be interested in reading up on some approximations of the square root function online.

Square roots in the editor?

11 hours ago

You can roughly approximate a square root in a script using Heron's method, but you're only going to get coarse integer results.


# The value to square root
%NUM := 33

# Begin with an estimate
%SQRT := %NUM / 100 + 1

# Iterate over the algorithm
%SQRT := (%SQRT + %NUM / %SQRT) / 2
%SQRT := (%SQRT + %NUM / %SQRT) / 2
%SQRT := (%SQRT + %NUM / %SQRT) / 2
%SQRT := (%SQRT + %NUM / %SQRT) / 2
%SQRT := (%SQRT + %NUM / %SQRT) / 2

With a NUM of 33, SQRT will yield 5, which is an underestimate but in line with the way scripts truncate the fractional part of numbers. With the estimate formula NUM / 100 + 1 and five iterations of the algorithm, there should be no error for any root that's a whole number up to 5002, but you could add more iterations to lessen the error at higher ranges.




Of course, if you know you're only going to be taking the square root of a small range of numbers, it might be simpler to just do a couple IF statements.


IF %NUM >= 36 THEN %SQRT := 6
ELSE IF %NUM >= 25 THEN %SQRT := 5
ELSE IF %NUM >= 16 THEN %SQRT := 4
ELSE IF %NUM >= 9 THEN %SQRT := 3
ELSE IF %NUM >= 4 THEN %SQRT := 2
ELSE %SQRT := 1

Square roots in the editor?

10 hours ago
I think Heron's method would technically work best for the system since the values I'm taking the square root of are variable, otherwise I would've jumped for the IF chain. My only concern is that I'll be approximating the square root calculations of multiple variables at once, and it feels like that's something that can get tedious and error-prone fast. I can only get so far with the barebones-ness of CYS Script :sadge: I'll take a look into it though; maybe I do settle for something less variable. Thanks Brad!