#name Code.Special
#type &ffd

#include <Bolmaster$Path>Sources.Structures
#include <Bolmaster$Path>Sources.Variables
#include <Bolmaster$Path>Sources.Macros
#include <Main$Path>Extasm.AsmLib.MYDIV

.processspecial
;process the special object table
STMFD R13!,{R14}
MOV R0,#0
MOV R1,R12
SWI Amnesia_Remote
MOV R0,#specialtab
MOV R1,#0
LDR R2,[R12,#v.masterplot]
SWI Amnesia_SelectTable

.looph
SWI Amnesia_ProcessTable
TST R2,#2
BLNE userplot
TST R2,#1<<8
BLNE timerexp
CMP R2,#0
BNE looph
LDMFD R13!,{PC}


.timerexp
; timer expiry
LDR R4,[R1,#4]
TST R4,#1<<31
BNE replace
STMFD R13!,{R0-R2,R14}
MOV R10,R1
LDMIA R10,{R1-R4}
MOV R5,#&02000100
CMP R1,R5
BEQ globe60
.timerexpins
LDMFD R13!,{R0-R2,PC}

.globe60
MOV R0,#bultab
MOV R1,#(bultab<<24)+4
MOV R2,#%1011110101+(1<<19)
LDR R5,[R12,#v.plxpos]
LDR R6,[R12,#v.plypos]
SUB R5,R5,R3
SUB R6,R6,R4
MOV R5,R5,ASR #6
MOV R6,R6,ASR #6
MOV R7,#&10000020
SWI Amnesia_MakeObject
B timerexpins

.replace
STMFD R13!,{R0-R2,R14}
MOV R10,R1
LDMIA R10,{R1-R4}
transdata R5,R3,R4
STRB R1,[R5]
MOV R0,#0
STR R0,[R10];delete object
LDMFD R13!,{R0-R2,PC}


;this routine is requested by the BMOG generator, and has an entry in the
;script file.  It's purpose is to plot a dalek and fire if necessary.
;dalek type is stored in the sprite entry
;
.userplot
.paintdalek
STMFD R13!,{R0-R2,R14}
MOV R10,R1
LDMIA R10,{R1-R4}
;plot the dalek body
LDR R0,[R12,#v.xplotofs]
ADD R3,R3,R0
LDR R0,[R12,#v.yplotofs]
ADD R4,R4,R0
MOV R1,R3,ASR #12
MOV R2,R4,ASR #12
LDR R0,[R10]
SWI FastSpr_Plot
LDMIA R10,{R1-R4}
TST R2,#1<<24
BNE paintrightdalek
MOV R8,#1
LDR R5,[R12,#v.plxpos]
LDR R6,[R12,#v.plypos]
SUB R5,R5,R3
SUB R6,R6,R4
CMP R5,#0
BGE pldins
MOV R5,R5,ASR #1
RSB R5,R5,#0; make R5 positive
CMP R6,R5
MOVGT R8,#0
CMN R6,R5
MOVLT R8,#2

.pldins
LDR R0,[R12,#v.xplotofs]
ADD R3,R3,R0
LDR R0,[R12,#v.yplotofs]
ADD R4,R4,R0
MOV R1,R3,ASR #12
MOV R2,R4,ASR #12
SUB R1,R1,#9
SUB R2,R2,#3
SUB R2,R2,R8,LSL #1
MOV R0,#&04000006
ADD R0,R0,R8,LSL #16
SWI FastSpr_Plot

MOV R1,R3,ASR #12
MOV R2,R4,ASR #12
SUB R1,R1,#8
SUB R2,R2,#12
;ADD R2,R2,R8,LSL #1
MOV R0,#&04020004
SUB R0,R0,R8,LSL #16
SWI FastSpr_Plot
.edgedalins
LDR R0,[R10]
AND R0,R0,#3
MOV R1,#&7F
;check if we need to shoot.  Change this value for more/less firing.
MOV R1,R1,LSR R0
LDR R0,[R12,#v.timer]
TST R0,R1
BNE edgedalnofire
LDMIA R10,{R1-R4}
TST R2,#1<<24
MOVEQ R5,#-2<<12
MOVNE R5,#2<<12
RSB R6,R8,#1
MOV R6,R6,ASL #12
BL dalekfire
.edgedalnofire
MOV R0,#0
STR R0,[R10]
LDMFD R13!,{R0-R2,PC}

.paintrightdalek
MOV R8,#1
LDR R5,[R12,#v.plxpos]
LDR R6,[R12,#v.plypos]
SUB R5,R5,R3
SUB R6,R6,R4
CMP R5,#0
BLE plrins
MOV R5,R5,ASR #1
CMP R6,R5
MOVGT R8,#0
CMN R6,R5
MOVLT R8,#2

.plrins
LDR R0,[R12,#v.xplotofs]
ADD R3,R3,R0
LDR R0,[R12,#v.yplotofs]
ADD R4,R4,R0
MOV R1,R3,ASR #12
MOV R2,R4,ASR #12
ADD R1,R1,#9
SUB R2,R2,#3
SUB R2,R2,R8,LSL #1
MOV R0,#&04020006
SUB R0,R0,R8,LSL #16
SWI FastSpr_Plot

MOV R1,R3,ASR #12
MOV R2,R4,ASR #12
ADD R1,R1,#8
SUB R2,R2,#12
;ADD R2,R2,R8,LSL #1
MOV R0,#&04020005
SUB R0,R0,R8,LSL #16
SWI FastSpr_Plot
B edgedalins

.dalekfire
STMFD R13!,{R0-R7,R14}
SUB R4,R4,#6<<12
CMP R5,#0
ADDGT R3,R3,#5<<12
SUBLT R3,R3,#5<<12
ADD R3,R3,R5,ASL #2
ADD R4,R4,R6,ASL #2
MOVS R0,R5
RSBMI R0,R0,#0
MOVS R1,R6
RSBMI R1,R1,#0
MOV R2,#1
CMP R5,#0
RSBLT R2,R2,#0
CMP R6,#0
RSBLT R2,R2,#0
CMP R0,R1,LSL #2
MOVGE R2,#0
MOV R1,#(bultab<<24)+(1<<16)
SUB R1,R1,R2,ASL #16
MOV R0,#bultab
MOV R2,#%1011010101+(1<<19)+(1<<24)
MOV R7,#&01000001
SWI Amnesia_MakeObject
LDMFD R13!,{R0-R7,PC}
