- Joined
- May 26, 2007
- Messages
- 5,544
- Reaction score
- 1,320
Implementing Rain
Okay. Now I've just implemented the rain code in the Low Level Tempskron maps in my Butchered Client based upon the disputed release in this post.
It's posted by Harsaphes (MRX from the M.I.B. forums) but that also points us to MagicPT, and I believe Excelsior has claimed to be the person who implemented that code. I'm fairly certain that he (Excelsior) posted a direct dump from Olly of his code here, but that thread seems to have gone AWOL just as many other recent threads have.
Anyway, I'm going to walk you through my translation of that implementation, and if Excelsior wants to repost his thread, I'll take this post over to his thread and close mine.
In the process of reworking the implementation, I can see that parts of it are brilliantly optimised, and yet other parts look thrown in, or like they have been re-jiggled about by someone who understood what they where doing less. I can't claim that I fully understand how this code interacts with the main game, but I can see how it is working on it's own, and have broken it down and put it together again in a slightly different (cleaner) manner.
First off, I don't like adding sections. MRX has created an MRX section with access for any kind of operation from anywhere, rather like the Global Fantasy team did, and to me that's just an extra hole when PT has too many as it is. Once you have dumped all the XTrap code in a 1977 client you have quite a lot of free space in your exe to add new code and data anyway... and that's what I did.
I'll not give you the offsets I used, because you may be using a different client, or have already put your own groovy code into the space I used. Besides all of which, the ability to be more generally useful is the reason I prefer to see Tutorials rather than releases... let alone re-releases. XD
The code it's self has 3 main sections and a Table of DWords. However, the implementation in the posted game.exe is a little jumbled up, as the primary worker routine (I called it SubRain) is bunged in the middle of another routine, which then has to JMP over it. The alignment of the routines is kinda nasty too, suggesting that someone has been a little liberal with the Binary copy and paste functions in Olly whilst neglecting their fundamental understanding of the i386 PMode architecture. XD (I know Excelsior knows this well, I'm not so sure of the other signatorys')
Okay... on to code. If you're using a 1977 KPT based client as I am, you should consider the following code fragments to be labelled in accordance with the following EQUates:-
Code:
[COLOR=DarkOrange]MapLo[/COLOR] [B]EQU[/B] [COLOR=Red]0091C3BCh[/COLOR]
[COLOR=DarkOrange]MapHi[/COLOR] [B]EQU[/B] [COLOR=Red]0091C3B0h[/COLOR]
[COLOR=DarkOrange]RainRet1[/COLOR] [B]EQU[/B] [COLOR=Red]00440D8Ah[/COLOR]
[COLOR=DarkOrange]RainRet2[/COLOR] [B]EQU[/B] [COLOR=Red]00440E46h[/COLOR]
[COLOR=DarkOrange]RainRet3[/COLOR] [B]EQU[/B] [COLOR=Red]004435BCh[/COLOR]
[COLOR=DarkOrange]RainRet4[/COLOR] [B]EQU[/B] [COLOR=Red]00443123h[/COLOR]
[COLOR=DarkOrange]AmbientSound[/COLOR] [B]EQU[/B] [COLOR=Red]004E68C0h[/COLOR]
[COLOR=DarkOrange]WeatherSelector[/COLOR] [B]EQU[/B] [COLOR=Red]00533E60h[/COLOR]
If you are using a different basic version for your client, you should probably start by getting a 1977 KPT client, and examining the code at the addresses in the fragment above, in order to locate the equivalent code locations in your client.
Now we can look at the first block of code used to add rain. In KPT 1977, replace the code between 00440D61 and 00440D89 with a JMP to RainCode, and a fist full of NOPs.
Code:
[B]Align[/B] [COLOR=Red]4[/COLOR]
[COLOR=DarkOrange]RainCode[/COLOR]:
[B]call[/B] [COLOR=DarkOrange]SubRain[/COLOR]
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]00h[/COLOR]
[B]jne[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Skip1[/COLOR]
[B]Align[/B] [COLOR=Red]4[/COLOR] [COLOR=DarkGreen][I]; > Optional, but I would advise you try to align loops. <[/I][/COLOR]
[COLOR=DarkOrange].Loop[/COLOR]:
[B]mov[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]00h[/COLOR]
[B]mov[/B] [COLOR=Blue]eax[/COLOR],[[COLOR=Blue]esi[/COLOR]*[COLOR=Red]04h[/COLOR]+[COLOR=Blue]eax[/COLOR]+[COLOR=Red]C8h[/COLOR]]
[B]jmp[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Skip2[/COLOR]
[COLOR=DarkOrange].Skip1[/COLOR]:
[B]mov[/B] [COLOR=Blue]ecx[/COLOR],[[COLOR=Red]0091C430h[/COLOR]]
[B]mov[/B] [COLOR=Blue]ecx[/COLOR],[[COLOR=Blue]ecx[/COLOR]+[COLOR=Red]016Ch[/COLOR]]
[B]mov[/B] [COLOR=Blue]edx[/COLOR],[COLOR=Navy]ds:[/COLOR][[COLOR=Blue]ecx[/COLOR]*[COLOR=Red]04h[/COLOR]+[COLOR=Red]0091C360h[/COLOR]] [COLOR=DarkGreen][I]; IDK What this table represents.[/I][/COLOR]
[B]mov[/B] [COLOR=Blue]ecx[/COLOR],[[COLOR=Blue]edx[/COLOR]+[COLOR=Red]C4h[/COLOR]]
[B]cmp[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Red]0400h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Loop[/COLOR]
[B]cmp[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Red]0500h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Loop[/COLOR]
[B]cmp[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Red]0800h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Loop[/COLOR]
[B]cmp[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Red]0900h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Loop[/COLOR]
[B]mov[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Red]00h[/COLOR]
[COLOR=DarkOrange].Skip2[/COLOR]:
c[B]mp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Blue]ebx[/COLOR]
[B]jl[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Ret1[/COLOR]
[B]push[/B] [COLOR=Blue]eax[/COLOR] [I][COLOR=DarkGreen]; Arg1[/COLOR][/I]
[B]call[/B] [COLOR=DarkOrange]WeatherSelector[/COLOR] [I][COLOR=DarkGreen]; WeatherSelector(Arg 1)[/COLOR][/I]
[B]mov[/B] [COLOR=Blue]eax[/COLOR],[[COLOR=DarkOrange]MapLo[/COLOR]]
[B]add[/B] [COLOR=Blue]esp[/COLOR],[COLOR=Red]04h[/COLOR]
[B]cmp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Red]01h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Ret1[/COLOR]
[B]cmp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Blue]ebx[/COLOR]
[B]jne[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Sound[/COLOR]
[COLOR=DarkOrange].Ret1[/COLOR]:
[B]jmp[/B] [COLOR=DarkOrange]RainRet1[/COLOR]
[COLOR=DarkOrange].Sound[/COLOR]:
[B]push[/B] [COLOR=Red]0Dh [/COLOR] [I][COLOR=DarkGreen]; Arg1 = 13[/COLOR][/I]
[B]call[/B] [COLOR=DarkOrange]AmbientSound[/COLOR] [I][COLOR=DarkGreen]; AmbientSound(Arg 1)[/COLOR][/I]
[B]add[/B] [COLOR=Blue]esp[/COLOR],[COLOR=Red]04h[/COLOR]
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapHi[/COLOR]],[COLOR=Red]28h[/COLOR]
[B]jg[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].MapHiGT40[/COLOR]
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapHi[/COLOR]],[COLOR=Red]14h[/COLOR]
[B]jg[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].MapHiGT20[/COLOR]
[B]jmp[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].[/COLOR][COLOR=DarkOrange]Ret2[/COLOR]
[COLOR=DarkOrange].MapHiGT40[/COLOR]:
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapHi[/COLOR]],[COLOR=Red]2Dh[/COLOR]
[B]jl[/B] short [COLOR=DarkOrange].MapHiRangeOK[/COLOR]
[COLOR=DarkOrange].MapHiGT20[/COLOR]:
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapHi[/COLOR]],[COLOR=Red]24h[/COLOR]
[B]jl[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].MapHiRangeOK[/COLOR]
[B]jmp[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Ret2[/COLOR]
[COLOR=DarkOrange].MapHiRangeOK[/COLOR]:
[B]push[/B] [COLOR=Red]089Dh[/COLOR] [I][COLOR=DarkGreen]; Arg4 = 2205[/COLOR][/I]
[B]push[/B] [COLOR=Red]0190h[/COLOR] [I][COLOR=DarkGreen]; Arg3 = 400[/COLOR][/I]
[B]push[/B] [COLOR=Red]00h[/COLOR] [I][COLOR=DarkGreen]; Arg2 = 0[/COLOR][/I]
[B]push[/B] [COLOR=Red]02h[/COLOR] [I][COLOR=DarkGreen]; Arg1 = 2[/COLOR][/I]
[B]call[/B] [COLOR=Red]004E5110h[/COLOR] [I][COLOR=DarkGreen]; 004E5110(Arg1, Arg2, Arg3, Arg4)
; I've not been able to figure out what this
; routine is, but it's clearly important.[/COLOR][/I]
[B]add[/B] [COLOR=Blue]esp[/COLOR],[COLOR=Red]10h[/COLOR]
[COLOR=DarkOrange].Ret2[/COLOR]:
[B]jmp[/B] [COLOR=DarkOrange]RainRet2[/COLOR]
Look out for the routine @ 004E5110 if you aren't using KPT 1977 as your base. That is the only core routine I haven't given any name to, and the reason is... I don't understand it's function myself yet. (If I figure it out, or someone let's me know, I'll update this post.)
MapLo and MapHi are probably incorrect initial assumptions for the variables at the address they represent too. I initially thought, from the way they are used, that they related to the map / field in which the player is presently playing, but as I've gone through it's looking more likely that at least one of them relates to the current weather the player is experiencing. Again, I don't fully understand them, so guessing at a better name is pointless. Just be warned that those labels may be misleading.
If you are not familiar with it, the .Label notation in Assembler source suggests that the "scope" for that label is local to the last sub-routine (or, label without a "." before it) and does not apply globally to the entire source file, let alone the whole program. You can (and I do) create them in Olly, but it does not understand them when you are assembling a line of code. So those will need to be manually translated to an address when you are typing code into Olly, or you'll have to use globally unique labels that don't start with a dot. (period in US English or full-stop in British English, but "dot" will do for Web-heads)
Notice also that I advise that where ever you put these code sections you Align their start, and any iterated JMP destinations (loops) to DWord boundaries. Align 4 is a 4 byte, or 32-bit pure boundary, and executes much faster and easier for an x86 32-bit CPU. (some more so than others) It's only an advisory in loops because of the speed issue, but it's almost a must for the actual start of the routine... Olly can get very confused in Analysis if this isn't correct, and although it may only be called infrequently, the delay is compounded by the fact that most of these are not JMP Short instructions, but full Long Jumps.
Of course Olly can't Assemble an Align, because it's more like a compiler directive than and instruction. While in Olly, you really have to play the part of the compiler your self. From real source the Compiler (Assembler) would insert 0, 1, 2 or 3 NOP (or equivalent) instructions depending on how near or far from a 4 byte boundary the next instruction address is. I think you can manage that by hand.
For the next code segment (again, only in a 1977 based client) you should replace the "cmp [MapLo],ecx" @ 0014030E5 with a JMP to RainHack. You'll get an extra NOP out of the deal, because the JMP is smaller bytecode than the rather complex in-direct addressed CMP. (you probably wouldn't see instructions like this on a RISC Architecture CPU)
Code:
[B]Align[/B] [COLOR=Red]4[/COLOR]
[COLOR=DarkOrange]RainHack[/COLOR]: [COLOR=DarkGreen][I]; 3 choices are broken to this code from the main.[/I][/COLOR]
[B]mov[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Red]02h[/COLOR]
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]00h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].ZeroMapLo[/COLOR]
[B]mov[/B] [COLOR=Blue]eax[/COLOR],[[COLOR=Red]0091C430h[/COLOR]]
[B]mov[/B] [COLOR=Blue]eax[/COLOR],[[COLOR=Blue]eax[/COLOR]+[COLOR=Red]16Ch[/COLOR]]
[B]cmp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Blue]edi[/COLOR]
[B]jl[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Ret4[/COLOR]
[B]mov[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Blue]ds:[/COLOR][[COLOR=Blue]eax[/COLOR]*[COLOR=Red]04h[/COLOR]+[COLOR=Red]0091C360h[/COLOR]] [COLOR=DarkGreen][I]; There's that table that IDK what it is again. ;)[/I][/COLOR]
[B]mov[/B] [COLOR=Blue]eax[/COLOR],[[COLOR=Blue]ecx[/COLOR]+[COLOR=Red]C4h[/COLOR]]
[B]cmp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Red]0400h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].ZeroMapLo[/COLOR]
[B]cmp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Red]0500h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].ZeroMapLo[/COLOR]
[B]cmp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Red]0800h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].ZeroMapLo[/COLOR]
[B]cmp[/B] [COLOR=Blue]eax[/COLOR],[COLOR=Red]0900h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].ZeroMapLo[/COLOR]
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]01h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Ret3[/COLOR]
[COLOR=DarkOrange].Ret4[/COLOR]:
[B]jmp[/B] [COLOR=DarkOrange]RainRet4[/COLOR]
[COLOR=DarkOrange].ZeroMapLo[/COLOR]:
[B]mov[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]00h[/COLOR]
[COLOR=DarkOrange].Ret3[/COLOR]:
[B]jmp[/B] [COLOR=DarkOrange]RainRet3[/COLOR]
Code:
[B]Align[/B] [COLOR=Red]4[/COLOR]
[COLOR=DarkOrange]SubRain[/COLOR]:
[B]mov[/B] [COLOR=Blue]edx[/COLOR],[[COLOR=Red]005FB3B8h[/COLOR]]
[B]xor[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Blue]ecx[/COLOR]
[B]Align[/B] [COLOR=Red]4[/COLOR] [I][COLOR=DarkGreen]; > Optional, but I would advise you try to align loops. <[/COLOR][/I]
[COLOR=DarkOrange].Loop1[/COLOR]:
[B]cmp[/B] [COLOR=Blue]edx[/COLOR],[[COLOR=Blue]ecx[/COLOR]*[COLOR=Red]04h[/COLOR]+[COLOR=DarkOrange]RainTable[/COLOR]]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Loop2[/COLOR]
[B]inc[/B] [COLOR=Blue]ecx[/COLOR]
[B]cmp[/B] [COLOR=Blue]ecx[/COLOR],[COLOR=Red]05h[/COLOR]
[B]jl[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Loop1[/COLOR]
[B]mov[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]00h[/COLOR]
[B]ret[/B]
[B]Align[/B] [COLOR=Red]4[/COLOR] [I][COLOR=DarkGreen]; > Optional, but I would advise you try to align loops. <[/COLOR][/I]
[COLOR=DarkOrange].Loop2[/COLOR]:
[B]jmp[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Branch2[/COLOR]
[B]Align[/B] [COLOR=Red]4[/COLOR] [COLOR=DarkGreen][I]; > Optional, but I would advise you try to align loops. <[/I][/COLOR]
[COLOR=DarkOrange].Loop2_1[/COLOR]:
[B]jge[/B] short [COLOR=DarkOrange].Branch1[/COLOR]
[COLOR=DarkOrange].Loop2_2[/COLOR]:
[B]mov[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]01h[/COLOR]
[B]ret[/B]
[COLOR=DarkOrange].Branch1[/COLOR]:
[B]mov[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapLo[/COLOR]],[COLOR=Red]02h[/COLOR]
[B]ret[/B]
[B]Align[/B] [COLOR=Red]4[/COLOR] [COLOR=DarkGreen][I]; > Optional, but I would advise you try to align loops. <[/I][/COLOR]
[COLOR=DarkOrange].Branch2[/COLOR]:
[B]cmp[/B] [COLOR=Blue]edx[/COLOR],[COLOR=Red]10h[/COLOR]
[B]je[/B] [COLOR=Navy]short[/COLOR] [COLOR=DarkOrange].Loop2[/COLOR]
[B]cmp[/B] [COLOR=Navy]dword ptr[/COLOR] [[COLOR=DarkOrange]MapHi[/COLOR]],[COLOR=Red]0Ah[/COLOR]
[B]jmp[/B] short [COLOR=DarkOrange].Loop2_1[/COLOR]
In my listing, I stored a full 8 DWords, just so it takes up exactly 2 lines in a hex editor, and because I'm not at all short of space. This should be somewhere in .data or .rdata sections, but you can keep it in .text like a "jump table" if you really feel you need to.
Code:
[COLOR=DarkOrange]RainTable[/COLOR]:
[B]dd[/B] [COLOR=Red]01h[/COLOR] [COLOR=DarkGreen][I]; 1[/I][/COLOR]
[B]dd[/B] [COLOR=Red]06h[/COLOR] [COLOR=DarkGreen][I]; 6[/I][/COLOR]
[B]dd[/B] [COLOR=Red]0Bh[/COLOR] [I][COLOR=DarkGreen]; 11[/COLOR][/I]
[B]dd[/B] [COLOR=Red]10h[/COLOR] [COLOR=DarkGreen][I]; 16[/I][/COLOR]
[B]dd[/B] [COLOR=Red]15h[/COLOR] [COLOR=DarkGreen][I]; 21[/I][/COLOR]
[B]dd[/B] [COLOR=Red]00h[/COLOR] [COLOR=DarkGreen][I]; 0[/I][/COLOR]
[B]dd[/B] [COLOR=Red]00h[/COLOR] [COLOR=DarkGreen][I]; 0[/I][/COLOR]
[B]dd[/B] [COLOR=Red]00h[/COLOR] [COLOR=DarkGreen][I]; 0[/I][/COLOR]
In Olly, your best option is to find a hole for this, and display it as Integer -> Unsigned Long in the "dump" panel.
I guess, with all my talk about optimising the alignment of jump destinations, you may be able to squeeze a little more performance out of P4 / Athelon XP based processors by using "branch hinting" to indicate which side of the jumps should be kept in the level 1 and level 2 CPU instruction caches... if anyone would like to try that (just for the fun of it) I'd love to hear how your experiments go.
I guess that either the C++ compiler they use to build KPT1977 (Visual C++ 7 AKA .NET 2003 according to most analysis) doesn't optimise with "branch hinting", or they turn that optimisation off for some reason, as I've never seen it used in any PT client or server... or any of the simple things I've built with VC7. I know that Borland C++ Builder and GCC (the GNU C+ Compiler) can include them, and that the .net framework is supposed to have it in it's JIT compiler. I've also seen some great results with hand assembled demos that rapidly zoom a Mandelbrot fractal or such... but I suspect that it's more a gimmick in real world situations.
(I'll welcome any insight into any of the bit's I'm unsure of, or even thought I was sure of, but am wrong about. And I'll update the main post and credit you for pointing out what a wazzock I am.
)Again full credits to all who got me to the point where I could write this guide:-
Harsaphes (alias MRX) for making it work in 1977 and sharing with RZ, where I found it.
Exellsior who (I do believe) probably managed to make sense of it in the 166beta (and maybe a peek at a Sandurr client) so that it could be implemented in newer general clients, probably for MagicPT.
At least, that's how I'm assuming the information got to me in a manner I could use to make this guide. That's the problem when you don't cite your sources guys, the paper trail goes cold part way through, and someone gets hurt.
To view the content, you need to sign in or register
Last edited:

