// Source file for the Team Grey Area mod preview
// Written by James "Anthem" Costlow
// Released without license, free for all purposes.

extern DidYouKnow

globals
	NotifyDistance
	RescueComplete
	sharknode1
	sharknode2
	sharknode3
	sharknode4
endg

function LandControlAll
locals
	creaturepenpos
	creature
endl
begin
	blk	END

	pshi 	0x1
	pshi	0x0
	pshf	1.0
	sys	0x1C3
	pshf	0.0
	crd
	pshf	0.0
	crd
	pshf	15.0
	crd
	addc
	sys	0x1B
	popf	creaturepenpos

	sys	0x1CE
	jz	HASNOCREATURE
	pshf	[creaturepenpos]
	sys	0x17
	sys	0xFA
	jmp	ENDCREATURECHECK

HASNOCREATURE:
	pshb	true
	jz	ENDCREATURECHECK
	pshi	0xC
	pshi	0x0
	pshf	[creaturepenpos]
	sys	0x17
	sys	0x1B
	popf	creature

	pshf	[creature]
	sys	0x53

// SET_CREATURE_DEV_STAGE(MyCreature, 13)
	pshf	1.0
	sys	0xC5
	pshi	13
	sys	0xD0
// DEV_FUNCTION(2)
	pshi	2
	sys	0xCD
// DEV_FUNCTION(3)
	pshi	3
	sys	0xCD

// CREATURE_IN_DEV_SCRIPT(false, MyCreature)
	pshb	false
	pshf	1.0
	sys	0xC5
	sys	0x11A
// RELEASE_FROM_SCRIPT(MyCreature)
	pshf	1.0
	sys	0xC5
	sys	0x9F

	jmp	ENDCREATURECHECK
ENDCREATURECHECK:

	pshf	1.0
	sys	0xC5
	pshf	[creaturepenpos]
	sys	0x17
	sys	0xDF

	strt	RescueWomanMain
	strt	RockMother

	endb
	jmp	RET
END:
	iter
RET:
	ret
end

function RescueWomanMain
locals
	womanpos
	woman
	womanwalktarget
	housepos
	wolfpos
	wolf
	islandpos
	scroll
	dykpos
	count
endl
begin
	blk	END

	pshi	0x1
	pshi	0x0
	pshf	2535.81
	crd
	pshf	0.0
	crd
	pshf	2362.94
	crd
	sys	0x1B
	popf	womanpos

// create woman
	pshi	0x4
	pshi	0x31
	pshf	[womanpos]
	sys	0x17
	sys	0x1B
	popf	woman

// create target for woman's stroll
	pshi	1
	pshi	0
	pshf	2532.91
	crd
	pshf	0.0
	crd
	pshf	2373.87
	crd
	sys	0x1B
	popf	womanwalktarget

// create housepos
	pshi	0x1
	pshi	0x0
	pshf	2561.73
	crd
	pshf	0.0
	crd
	pshf	2450.0
	crd
	sys	0x1B
	popf	housepos

// position of wolf
	pshi	0x1
	pshi	0x0
	pshf	2514.00
	crd
	pshf	40.0
	crd
	pshf	2369.00
	crd
	sys	0x1B
	popf	wolfpos

	pshi	0x6
	pshi	0x2
	pshf	[wolfpos]
	sys	0x17
	sys	0x1B
	popf	wolf

	pshi	0x1
	pshi	0x0
	pshf	2685.0
	crd
	pshf	0.0
	crd
	pshf	2186.0
	crd
	sys	0x1B

	popf	islandpos

	pshf	0.0
	popf	scroll

	pshf	0.0
	popf	dykpos

// rescue not complete

	pshf	0.0
	popf	RescueComplete

// make movement
	pshf	[woman]
	pshf	[womanwalktarget]
	sys	0x17
	pshf	0.0
	sys	0x21

	pshf	[wolf]
	pshf	[womanwalktarget]
	sys	0x17
	sys	0x21

	pshi	0xB
	pshf	[wolf]
	pshf	1.0
	sys2	0x16


// init cutscene
INITCAM:
	sys	0x1E
	jz	INITCAM
	sys	0x80
	pshb	true
	sys	0x20

	pshf	2572.78
	crd
	pshf	40.0
	crd
	pshf	2372.87
	crd
	sys	0x1

	pshf	[woman]
	sys	0x17
	sys	0x2

// fade in
	pshf	3.0
	sys	0xF2

// start some music
	pshi	58
	sys	0x2C

WAITFORWALK:
	pshf	[woman]
	sys	0x17
	pshf	[womanwalktarget]
	sys	0x17
	sys	0x19
	pshf	0.0
	eq
	jz	WAITFORWALK

// face wolf
	pshf	[woman]
	pshf	[wolf]
	sys	0x17
	sys	0x22

//INITDLG:
//	sys	0x78
//	jz	INITDLG
//	pshb	false
//	pshi	0x641
//	pshi	0
//	sys	0xD
//RUN:
//	sys	0xF
//	jz	RUN
//	sys	0x79

// pause
SHOCKANDAWE:
	pshf	2.0
	dly
	jz	SHOCKANDAWE

// start running
	pshi	0xB
	pshf	[woman]
	pshf	1.0
	sys2	0x16

	pshf	[woman]
	pshf	[islandpos]
	sys	0x17
	pshf	0.0
	sys	0x21

	pshf	[wolf]
	pshf	[islandpos]
	sys	0x17
	pshf	0.0
	sys	0x21

	pshf	[islandpos]
	sys	0x17
	pshf	5.0
	crd
	pshf	5.0
	crd
	pshf	10.0
	neg
	crd
	addc
	pshf	25.0
	sys	0x3

	pshf	[womanpos]
	sys	0x17
	pshf	10.0
	sys	0x4

CHECKWOMANONISLAND:
	pshf	[wolf]
	sys	0x17
	pshf	[wolfpos]
	sys	0x17
	sys	0x19
	pshf	146.0
	gte
	jz	WOLFNOTTHERE
// halt the wolf
	pshi	0xB
	pshf	[wolf]
	pshf	0.0
	sys2	0x16

WOLFNOTTHERE:
	pshf	[woman]
	sys	0x17
	pshf	[islandpos]
	sys	0x17
	sys	0x19
	pshf	0.0
	eq
	jz	CHECKWOMANONISLAND

	pshf	[woman]
	pshf	[wolf]
	sys	0x17
	sys	0x22

	pshf	[islandpos]
	sys	0x17
	pshf	10.0
	neg
	crd
	pshf	5.0
	crd
	pshf	10.0
	crd
	addc
	pshf	4.0
	sys	0x3

	pshf	[woman]
	sys	0x17
	pshf	4.0
	sys	0x4

// create sharks
	strt	RescueWomanSharks

// create scroll
	pshi	0x3
	pshf	[woman]
	sys	0x17
	pshf	3.0
	crd
	pshf	0.0
	crd
	pshf	3.0
	neg
	crd
	addc
	pshi	0x40
	sys	0x110
	popf	scroll

// create dyk
	pshi	0x1
	pshi	0x0
	pshf	[woman]
	sys	0x17
	pshf	0.0
	crd

	pshf	0.0
	crd
	pshf	3.0
	neg
	crd
	addc
	sys	0x1B
	popf	dykpos

	pshf	[dykpos]
	pshi	0x1018
	flt
	pshi	0x2
	flt
	strt	DidYouKnow

	sys	0x2D

// end cutscene
	pshb	false
	sys	0x20
	sys	0x81
	sys	0x1F

ACTIVATEWAIT:
	pshf	[scroll]
	sys	0x10
	jz	ACTIVATEWAIT
	sys	0x9C

// start the wolf across the water
	pshi	0xB
	pshf	[wolf]
	pshf	0.3
	sys2	0x16

	pshf	[wolf]
	pshf	[woman]
	sys	0x17
	pshf	0.0
	sys	0x21

// wait until woman is dead/nonexistent or the wolf is gone
WAITCOMPLETE:
	pshf	[woman]
	sys	0x100
	pshi	0x1
	pshf	[woman]
	sys	0x15
	and
	pshf	[wolf]
	sys	0x100
	and

	pshi	0x1
	pshf	[wolf]
	sys	0x15
	and
	pshf	[wolf]
	sys	0x17
	pshf	[woman]
	sys	0x17
	sys	0x19
	pshf	1.0
	gt
	and
	not
	jz	WAITCOMPLETE

	pshf	0.0
	popf	count

	pshf	[woman]
	sys	0x100
	pshi	0x1
	pshf	[woman]
	sys	0x15
	pshf	0.0
	gt
	and
	pshf	[wolf]
	sys	0x100
	not
	pshi	0x1
	pshf	[wolf]
	sys	0x15
	pshf	0.0
	lte
	or
	and
	jz	KILLEDWOMAN
// wolf is dead, good ending
GOODENDING:
	blk	GOODENDINGITER
GOODENDINGLOOP:
	pshf	[count]
	pshf	50.0
	lt
	jz	GOODENDINGEND
	pshi	11
	pshi	1
	sys	0x170
	sys	0x17
	pshf	300.0
	pshf	300.0
	neg
	sys	0x1C
	pshc
	pshf	300.0
	pshf	300.0
	neg
	sys	0x1C
	addc
	pshi	1
	sys	0x170
	sys	0x17
	pshf	500.0
	pshf	500.0
	neg
	sys	0x1C
	pshc
	pshf	500.0
	pshf	500.0
	neg
	sys	0x1C
	addc
	pshf	5.0
	pshf	20.0
	pshf	5.0
	sys	0xC4
	pshf	1.0
	pshf	[count]
	add
	popf	count
GOODSPELLDELAY:
	pshf	1.5
	pshf	0.5
	sys	0x1C
	dly
	jz	GOODSPELLDELAY
	jmp	GOODENDINGLOOP
GOODENDINGEND:
	endb
	jmp	GOODENDINGOUT
GOODENDINGITER:
	iter
GOODENDINGOUT:
	jmp	RESOLVEDQUEST
KILLEDWOMAN:
// killed woman or wolf reached her, evil ending
EVILENDING:
	blk	EVILENDINGITER
EVILENDINGLOOP:
	pshf	[count]
	pshf	50.0
	lt
	jz	EVILENDINGEND
	pshi	3
	pshi	1
	sys	0x170
	sys	0x17
	pshf	300.0
	pshf	300.0
	neg
	sys	0x1C
	pshc
	pshf	300.0
	pshf	300.0
	neg
	sys	0x1C
	addc
	pshi	1
	sys	0x170
	sys	0x17
	pshf	500.0
	pshf	500.0
	neg
	sys	0x1C
	pshc
	pshf	500.0
	pshf	500.0
	neg
	sys	0x1C
	addc
	pshf	5.0
	pshf	20.0
	pshf	5.0
	sys	0xC4
	pshf	1.0
	pshf	[count]
	add




	popf	count
EVILSPELLDELAY:
	pshf	3.0
	pshf	0.5
	sys	0x1C
	dly
	jz	EVILSPELLDELAY
	jmp	EVILENDINGLOOP
EVILENDINGEND:
	endb
	jmp	EVILENDINGOUT
EVILENDINGITER:
	iter
EVILENDINGOUT:


RESOLVEDQUEST:
	pshf	1.0
	popf	RescueComplete
	jz	RESOLVEDQUEST

	endb

	jmp	RET
END:
	iter
RET:
	ret
end




function RescueWomanSharks
locals
	shark1
	shark2
	shark3
endl
begin
	blk	END


// create positions
	pshi	0x1
	pshi	0x0
	pshf	2678.22
	crd
	pshf	0.0
	crd
	pshf	2292.92
	crd
	sys	0x1B
	popf	sharknode1

	pshi	0x1
	pshi	0x0
	pshf	2618.22
	crd
	pshf	0.0
	crd
	pshf	2147.92
	crd
	sys	0x1B

	popf	sharknode2

	pshi	0x1
	pshi	0x0
	pshf	2590.22
	crd
	pshf	0.0
	crd
	pshf	2196.92
	crd
	sys	0x1B
	popf	sharknode3

	pshi	0x1
	pshi	0x0
	pshf	2641.22
	crd

	pshf	0.0
	crd
	pshf	2227.92
	crd
	sys	0x1B
	popf	sharknode4

// create and move sharks
	pshi	0x1A
	pshi	0x1388
	pshf	[sharknode1]
	sys	0x17
	sys	0x1B
	popf	shark1

	pshi	0x1A
	pshi	0x1388
	pshf	[sharknode2]
	sys	0x17
	sys	0x1B
	popf	shark2

	pshi	0x1A
	pshi	0x1388
	pshf	[sharknode3]
	sys	0x17
	sys	0x1B
	popf	shark3

// start circling
//	pshf	[shark1]
//	pshf	1.0
//	call	SharkCircle

//	pshf	[shark2]
//	pshf	2.0
//	strt	SharkCircle

//	pshf	[shark3]
//	pshf	3.0
//	strt	SharkCircle


	endb
	jmp	RET
END:
	iter
RET:
	ret
end



function SharkCircle
parms
	shark
	node
endp
begin

	blk	END
	popf	shark
	popf	node


	pshi	0xB
	pshf	[shark]
	sys2	0x15
	pshi	0xB
	pshf	[shark]
	pshf	1.0
	sys2	0x16


	blk	CIRCLELOOP_ITER
CIRCLELOOP:
	pshf	[RescueComplete]
	pshf	0.0


	eq



	jz	CIRCLELOOP_END


// if current node is 1, move to 2
	pshf	[node]

	pshf	1.0
	eq
	jz	CHECKNODE2
	pshf	[shark]
	pshf	[sharknode2]

	sys	0x17
	pshf	0.0
	sys	0x21
	pshf	2.0
	popf	node
NODE2WAIT:
	pshf	[shark]
	sys	0x17
	pshf	[sharknode2]
	sys	0x17

	sys	0x19
	pshf	0.0
	eq
	jz	NODE2WAIT

// if current node 2, move to 3
CHECKNODE2:
	pshf	[node]
	pshf	2.0
	eq
	jz	CHECKNODE3
	pshf	[shark]
	pshf	[sharknode3]
	sys	0x17
	pshf	0.0
	sys	0x21
	pshf	3.0
	popf	node


NODE3WAIT:
	pshf	[shark]
	sys	0x17
	pshf	[sharknode3]
	sys	0x17
	sys	0x19
	pshf	0.0
	eq
	jz	NODE3WAIT

// if current node 3, move to 4
CHECKNODE3:


	pshf	[node]
	pshf	3.0
	eq
	jz	CHECKNODE4
	pshf	[shark]
	pshf	[sharknode4]
	sys	0x17
	pshf	0.0
	sys	0x21
	pshf	4.0
	popf	node
NODE4WAIT:
	pshf	[shark]
	sys	0x17
	pshf	[sharknode4]
	sys	0x17
	sys	0x19
	pshf	0.0
	eq
	jz	NODE4WAIT

// if current node 4, move to 1
CHECKNODE4:
	pshf	[node]
	pshf	4.0
	eq
	jz	CHECKNONE
	pshf	[shark]
	pshf	[sharknode1]
	sys	0x17
	pshf	0.0
	sys	0x21
	pshf	1.0
	popf	node
NODE1WAIT:
	pshf	[shark]
	sys	0x17
	pshf	[sharknode1]
	sys	0x17
	sys	0x19
	pshf	0.0
	eq
	jz	NODE1WAIT

INIT:
	sys	0x78
	jz	INIT
	pshb	false
	pshi	0x1915
	pshi	0
	sys	0xD
RUN:
	sys	0xF
	jz	RUN
	sys	0x79

CHECKNONE:
	jmp	CIRCLELOOP
CIRCLELOOP_END:
	endb
	jmp	CIRCLELOOP_OUT
CIRCLELOOP_ITER:
	iter
CIRCLELOOP_OUT:

	endb
	jmp	RET
END:
	iter
RET:
	ret
end



function RockMother
locals
	rock
	town
	villager
	sex
	count
endl
begin
	blk	lbl0
	pshi	0x8
	pshi	0x5
	pshf	2432.000000
	crd	
	pshf	60.000000
	crd	
	pshf	2519.000000
	crd	
	sys	0x1B
	popf	rock
	pshi	0x0
	popf	town
	pshi	0x0
	popf	villager
	pshi	0x31
	popf	sex
	pshf	0.000000
	popf	count

	blk	lbl1
lbl10:
	pshb	1
	jz	lbl2
	blk	lbl3

lbl7:
	pshf	[rock]
	sys	0x10

	not
	jz	lbl4
	pshf	[rock]
	sys	0x100
	not
	jz	lbl5
	pshi	0x8
	pshi	0x5
	sys	0x6
	sys	0x1B
	popf	rock
	jmp	lbl5


lbl5:
	jmp	lbl7
lbl4:

	endb
	jmp	lbl8
lbl3:
	iter
lbl8:
	pshi	0x31
	popf	sex
	pshf	0.000000
	popf	count
	pshi	0x9
	pshi	0x1388
	pshf	[rock]
	sys	0x17
	pshf	1000.000000
	pshb	0
	sys	0x33
	popf	town
	blk	lbl9
lblE:
	pshf	[count]
	pshf	8.000000
	lt
	jz	lblA
	pshi	0x4
	pshf	[sex]
	pshf	[rock]

	sys	0x17
	pshf	5.000000
	crd	
	pshf	5.000000
	crd	
	pshc
	swp
	addc
	sys	0x1B
	popf	villager
	pshf	[villager]
	pshf	[town]
	pshb	0
	sys	0x25
	popo	
	zero	villager
	pshi	[count]
	pshf	1.000000
	add
	popf	count
	pshi	0x31
	pshf	[sex]
	eq
	jz	lblB
	pshi	0x34
	popf	sex
	jmp	lblC
lblB:
	pshb	1
	jz	lblD
	pshi	0x31
	popf	sex
lblC:
	jmp	lblD
lblD:
	jmp	lblE
lblA:
	endb
	jmp	lblF
lbl9:
	iter
lblF:
	sys	0x9C
	jmp	lbl10
lbl2:
	endb
	jmp	lbl11
lbl1:
	iter
lbl11:
	endb
	jmp	lbl12
lbl0:
	iter
lbl12:
	ret
end
