kalarhan said:builder of the gods said:Kalarkhan as you seem to now your stuff i am trying to add a ratio to my presentation for my mod For Rome
you can either work with operations that handle fixed point, or simple multiply everything for 100 before hand
x1=20
x2=40
res=50%
20/40 = 0 (as 0.5 doesn't compute)
(20*100) / (40) = 50 %
remember the % symbol already represents the /100
you should check for zero before the division, otherwise you will see the red text exploding on your screen if you ever change the balance
anything/0 = error
you don't need this testCode:(lt, ":addorsub", 50),
builder of the gods said:And why aren't you making a mod yourself
Maybe I did... several mods...
builder of the gods said:Why does the module system is harder than c# or something
Handling float calculation is something you need to learn in any language, even MBScript. Just google for it and check the discussions, like in
http://stackoverflow.com/questions/20788793/c-how-to-calculate-a-percentageperthousands-without-floating-point-precision
or check wikipedia
but if you want to learn about this you should actually search for a math book (Knuth is a great reference)
Fixed it: for other people struggling (needs WSE)
Calculates friendly and enemy troops, adds a small number to hostile because you dont now exact numbers.
Then makes a two decimal ratio of it
adding 150 troops for siege, is for My siege system, in my mod For Rome
(link to siege system: http://www.moddb.com/mods/for-rome/features/sieges-you-wont-believe-your-eyes)
Code:
(store_party_size_wo_prisoners, ":party_size", "p_main_party"),
(assign, reg5, ":party_size"),
(store_party_size_wo_prisoners, ":party_size2", "$g_encountered_party"), #get troops in city
(val_add, ":party_size2", 150), #extra in the battle that are citizens fighting
(store_random_in_range, ":missinfo", 0,100), #the info is 0 to 100 changing with the real info
(store_random_in_range, ":addorsub", 0,100), #this determines whether the chance is added or substracted
(try_begin),
(ge, ":addorsub", 50),
(val_add, ":party_size2", ":missinfo"),
(else_try),
(lt, ":addorsub", 50),
(val_sub, ":party_size2", ":missinfo"),
(try_end),
(assign, reg6, ":party_size2"),
(assign, ":ratio1", ":party_size"), #store friendlies to ratio
(val_div, ":ratio1", ":party_size2"), #divide friendlies with enemies giving a ratio number
(val_mul, ":ratio1", 100), #0,5 gives 50% 5 gives 500% so this makes it procentual (V1)
(assign, reg7, ":ratio1"), #stored to reg to be able to put it in the string
(str_store_string, s1, "@{reg7}"), #procentual put in string
#following requires WSE
(str_length, ":s1length", s1), #we store the length of the string
(str_store_string, s2, "@empty"),
(str_store_string, s3, "@empty"),
(try_begin),
(eq, ":s1length", 4), #as we divide by 100 it has two decimals: 3,21 or 9,47
(str_store_substring, s2, s1, 0, 2), #stores first number
(str_to_num, ":num1", s2), #make first letter an number
(assign, reg10, ":num1"), #store first letter number to reg10
(str_store_substring, s3, s1, 2, 4), #stores digits
(str_to_num, ":num2", s3), #make first letter an number
(assign, reg11, ":num2"), #store first letter number to reg10
(str_store_string, s2, "@{reg10}.{reg11}"),
(else_try),
(eq, ":s1length", 3), #as we divide by 100 it has two decimals: 3,21 or 9,47
(str_store_substring, s2, s1, 0, 1), #stores first number
(str_to_num, ":num1", s2), #make first letter an number
(assign, reg10, ":num1"), #store first letter number to reg10
(str_store_substring, s3, s1, 1, 3), #stores digits
(str_to_num, ":num2", s3), #make first letter an number
(assign, reg11, ":num2"), #store first letter number to reg10
(str_store_string, s2, "@{reg10}.{reg11}"),
(else_try)
(eq, ":s1length", 2),
(str_to_num, ":num3", s1), #make first letter an number
(assign, reg12, ":num3"), #store first letter number to reg10
(str_store_string, s2, "@0.{reg12}"),
(else_try),
(eq, ":s1length", 1),
(str_to_num, ":num4", s1), #make first letter an number
(assign, reg13, ":num4"), #store first letter number to reg10
(str_store_string, s2, "@0.0{reg13}"),
(try_end),