allowtrainrefit.asm
ttdpatch_ttdpatch/procs/allowtrainrefit.asm
#include <defs.inc>
#include <frag_mac.inc>


extern oldrefitplane,trainplanerefitcost,trainwindowrefit
extern trainwindowsize


global patchallowtrainrefit

begincodefragments

codefragment oldcheckinhangar,-0xf,-0x10
#if WINTTDX
//	cmp byte ptr getgs(bp),20h
	cmp byte [landscape5(bp)],0x20
	jz $+0xd+2
#else
	db 0x67,0x65,0x80,0x7e,0,0x20	// cmp byte ptr gs:[bp],20 but different order of prefixes
	jz $+2+0xc			// than normally generated by tasm
#endif

codefragment newcheckinhangar
	icall checkinhangarandstopped
	jc .ok
	pop ax
	mov ebx, 0x80000000
	ret
.ok:
	setfragmentsize 47+3*WINTTDX,1


codefragment newcalcrefitcap
	call runindex(calcplanetrainrefitcap)
	db 0xeb,0x8

glob_frag findtrainwindowelemliststore, trainwindowdata
codefragment trainwindowdata,-0x66
	db 0x44,0,0x55,0,0xcb

global normaltrainwindowptr
codefragment oldsetuptrainwindow
	db 0xc7,0x46,0x24
normaltrainwindowptr:
	dd 0

reusecodefragment findtrainwindowelemliststore, oldsetuptrainwindow,3

codefragment newsetuptrainwindow
	call runindex(setuptrainwindow)
	nop

codefragment oldtrainwindowfunc,-26
	or edx,0x1c0

codefragment newtrainwindowfunc
	call runindex(trainwindowfunc)
	setfragmentsize 13

codefragment refitplane, -38 	
	mov dx,98h
	mov ebp,0Ch

codefragment oldtrainreverse,0x33
	cmp cl,8
	jz short $+2+0x23

codefragment newtrainreverse
	call runindex(trainreverse)
	setfragmentsize 10

codefragment oldsetnewengine
	cmp bh,0
	je short .hassecondengine
	xor cx,cx
.hassecondengine:

codefragment newsetnewengine
	call runindex(refitsecondengine)
	jmp $+12

codefragment newrefitstorecap
	call runindex(refitstorecap)
	setfragmentsize 8

codefragment newcalcrefitcost
	mov ebx,[trainplanerefitcost]
	setfragmentsize 10


endcodefragments

patchallowtrainrefit:
	patchcode oldcheckinhangar,newcheckinhangar,1,1

	// also copy some other code
	add edi,byte 0x3b+lastediadj+3*WINTTDX
	storefragment newcalcrefitcap

	stringaddress trainwindowdata
	extern patchflags
	testmultiflags enhancegui
	jnz .nostore
	mov [normaltrainwindowptr],edi
.nostore:

		// create the new window setup with refit button
	mov esi,[normaltrainwindowptr]
	mov edi,trainwindowrefit
	mov ecx,trainwindowsize
	rep movsb
	mov word [trainwindowrefit+0x6a],0x2b4

	patchcode oldsetuptrainwindow,newsetuptrainwindow,1,1
	patchcode oldtrainwindowfunc,newtrainwindowfunc,1,1

	storeaddress refitplane,1,1,oldrefitplane

	patchcode oldtrainreverse,newtrainreverse,1,1
	patchcode oldsetnewengine,newsetnewengine,1,1
	add edi,byte lastediadj-0x26
	storefragment newrefitstorecap
	sub byte [edi+lastediadj+13],18
	add edi,lastediadj+0x6f
	storefragment newcalcrefitcost
	ret
