

AlexSpl
Responsible
Supreme Hero

posted December 15, 2014 10:38 PM 

Edited by AlexSpl at 22:40, 15 Dec 2014.

Can you write an ERM script that will show the value of R4, provided a, b, c, and d are integers?
1. R1 = a*x + b*y + c*z + d
2. R2 = R1 / 65536 (integer division here!)
3. R3 = R2 mod 32768
4. R4 = R3 mod 100


kicferk
Known Hero

posted December 15, 2014 10:40 PM 


I think if you use e variables the game will recognize it. That's what floating point is for in the first place. v variables on the other hand seem like integer ones to me, so they are forced to be integers by game You will need to use a bit different commands, but all in all very similar.


AlexSpl
Responsible
Supreme Hero

posted December 15, 2014 10:44 PM 

Edited by AlexSpl at 22:46, 15 Dec 2014.

Or, using '%' symbol for a remainder:
1. R1 = a*x + b*y + c*z + d
2. R2 = R1 / 65536 (integer division here!)
3. R3 = R2 % 32768
4. R4 = R3 % 100
Where
a = 196143423
b = 928924629
c = 348991063
d = 2148842073
x, y, z  coordinates


Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 10:46 PM 



Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 10:53 PM 


The script:
!?CM0;
;get active hero position (x:y:z 14/18/0)
!!OW:A1/?y1;
!!HEy1:P?y2/?y3/?y4;
;R1 = a*x + b*y + c*z + d
!!VRy5:S196143423*y2;
!!VRy6:S928924629*y3;
!!VRy7:S348991063*y4;
!!VRv2:Sy5+y6+y7+2148842073;
;R2 = R1 / 65536 (integer division here!)
!!VRv3:Sv2:65536;
;R3 = R2 % 32768
!!VRv4:Sv3%32768;
;R4 = R3 % 100
!!VRv5:Sv4%100;
!!IF:M^%V5^;
I got R4=46.
____________
Era II mods and utilities


AlexSpl
Responsible
Supreme Hero

posted December 15, 2014 10:57 PM 


Well done. Now compare this number to 50.
R4 < 50 means you will see an upgraded stack, otherwise not.


Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 11:00 PM 


I got R4=25 if I move hero to other location. Is this possible or some error in my maths?
When you say:
;R2 = R1 / 65536 (integer division here!)
!!VRv3:Sv2:65536;
This is division, right? v2 divided by 65536, ok?
____________
Era II mods and utilities


AlexSpl
Responsible
Supreme Hero

posted December 15, 2014 11:02 PM 


You should use only unsigned integers.


kicferk
Known Hero

posted December 15, 2014 11:02 PM 


I bet you get an overflow here: !!VRv2:Sy5+y6+y7+2148842073;
Try dividing each of parts by 65k individually, keeping it in yvariables.


Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 11:04 PM 


Well yes the max range of integers is 2147483647 in erm. How do I avoid that, divide each result by 65536?
____________
Era II mods and utilities


AlexSpl
Responsible
Supreme Hero

posted December 15, 2014 11:05 PM 


Overflow is needed here. You cannot divide each part.
See this:
(1 + 2 + 3) / 6 = 1
1/6 + 2/6 + 3/6 = 0 + 0 + 0 = 0


AlexSpl
Responsible
Supreme Hero

posted December 15, 2014 11:07 PM 


Quote: Well yes the max range of integers is 2147483647 in erm
I should think about a workaround. Give me some time


Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 11:08 PM 


The problem is here, I need to rewrite it to get accurate result:
R1 = a*x + b*y + c*z + d
If I write this as it is in erm, we get
a*x+b, now the result will be multiplied by y, then add c then the result multiply by z and so on. So I need to divide the operation in valid parts.
____________
Era II mods and utilities


kicferk
Known Hero

posted December 15, 2014 11:09 PM 


Ok, so to deal with that you can do the following:
If you get a negative result after dividing by 65536 you make the number equal to 65536 + the result and carry on. I think it should return a correct result in the end


Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 11:12 PM 



kicferk
Known Hero

posted December 15, 2014 11:14 PM 

Edited by kicferk at 23:24, 15 Dec 2014.

Ok, it seems in this particular setup what I wrote will work. Normally you would need to adjust to the remainder of max number+1 divided by divisor, but in this case it is 0, so you can do what I wrote and it should work.
It exploits the fact that the remainder may have two types  positive and negative, and that adding the divisor to the negative remainder gives you positive one. And in this case it also exploits the fact that remainder of division of 2147483648 by 65536 is 0.


Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 11:24 PM 


kicferk said: Ok, so to deal with that you can do the following:
If you get a negative result after dividing by 65536 you make the number equal to 65536 + the result and carry on. I think it should return a correct result in the end
I don't have a clue of what to do
How changing a negative integer to 65536 will keep the thing accurate
Give me please an example based on that erm I wrote.
____________
Era II mods and utilities


kicferk
Known Hero

posted December 15, 2014 11:28 PM 


Take the example that gave you negative result.
v2 got negative and so did v3. But v3 is the remainder, so by what I wrote in "edit", you can get a positive integer with the same meaning if you add 65536 to it.
The reason it works is because of the way overflow is taken care of. after 2147483647 you get 2147483648, and then it goes normally(I think the range should be 2147483648 to + 2147483647)


Salamandre
Admirable
Omnipresent Hero
Wog refugee

posted December 15, 2014 11:32 PM 


ok, this is modified to add 65536 each time a result is negative, except for R4, right?:
!?CM0;
!!OW:A1/?y1;
!!HEy1:P?y2/?y3/?y4;
;R1 = a*x + b*y + c*z + d
!!VRy5:S196143423*y2;
!!VRy6:S928924629*y3;
!!VRy7:S348991063*y4;
!!VRv2:Sy5+y6+y7+2148842073;
;R2 = R1 / 65536 (integer division here!)
!!VRv3:Sv2:65536;
!!VRv3&v3<0:+65536;
;R3 = R2 % 32768
!!VRv4:Sv3%32768;
!!VRv4&v4<0:+65536;
;R4 = R3 % 100
!!VRv5:Sv4%100;
!!IF:M^%V5^;
Now I will test about upgraded/non upgraded stacks, within this script.
____________
Era II mods and utilities


kicferk
Known Hero

posted December 15, 2014 11:36 PM 


!!VRv4&v4<0:+65536;
This line is unnecessary, and is unlikely to ever being triggered. If it is ever triggered it should be
!!VRv4&v4<0:+32768;
because to get v4 you divided by 32768. And it will never occur, because there is no way the result would go negative after you took care of v3 being negative


