endeditactual:
	move.w #2,windowtype(a5)
	bsr updatewindow
	bne nowindow
	bsr wind2setup
	rts
editactual:
	move.w #3,windowtype(a5)
	bsr updatewindow
	bne nowindow
	move.l #164,d6			;bp strt
	move.l #205,d7			;stop
	bsr plotpal
	
	move.b #0,d0
	move.l #208,d2
	bsr palcol_arrow
	
	bsr draweditblock
	
	rts

drawactual:

	rts
	
draweditblock:
	moveq.l #0,d0
	move.w blockwidth(a5),d0
	add.l d0,d0	*2
	add.l d0,d0	*4
	add.l d0,d0	*8
	move.w d0,blockpixelwidth(a5)
	moveq.l #0,d1
	move.w #160,d1
	divu d0,d1
	move.w d1,eb_dx(a5)
	move.w blockheight(a5),d0
	moveq.l #0,d1
	move.w #160,d1
	divu d0,d1
	move.w d1,eb_dy(a5)
	move.w d1,d0
	cmp.w eb_dx(a5),d0
	blo .x
	move.w eb_dx(a5),eb_dy(a5)
	bra .y
.x	move.w eb_dy(a5),eb_dx(a5)
.y
	moveq #0,d0
	move.w blockpixelwidth(a5),d1
	cmp.w #80,d1
	bhs .gotit	;nodraw
	move.w #480,d0
	sub.w d1,d0
	lsr.w #1,d0	;divu #2
	
.gotit
	move.w d0,blockeditactualx(a5)
	
	move.w #160,d0
	move.w blockheight(a5),d1
	sub.w d1,d0
	lsr.w #1,d0
	move.w d0,blockeditactualy(a5)
	
	bsr g_maskswap
	move.w blockeditactualx(a5),d0
	beq .nodrawa
	move.w blockeditactualy(a5),d1

	move.l currentblockadr(a5),a0
	bsr drawblock
.nodrawa	
	rts	
	
drawblock: ;draw block, d0 = x, d1 = y, a0 = block adr.
	move.l d0,d6
	move.l d1,d7

	lea block_gfx(a0),a0
	
	move.l chipblockbase(a5),a1
	move.l exec(a5),a6

	moveq.l #0,d4
	move.w blockwidth(a5),d4
	btst #0,d4
	beq .normal

	move.w blockheight(a5),d3

	mulu.w bitplanes(a5),d3
	sub.w #1,d3
.cloop

	move.l d4,d0

	jsr -624(a6)		;copy mem

	addq.l #1,a1

	dbra d3,.cloop

	bra .back
.normal
	mulu.w blockheight(a5),d4

	mulu bitplanes(a5),d4
	move.l d4,d0
	jsr -624(a6)
.back

	move.l d6,d0
	move.l d7,d1
	ext.l d0
	ext.l d1

	lea chipimage(pc),a1
	move.l intbase(a5),a6
	move.l rastport(a5),a0
	jsr -114(a6)
	
	rts
chipimage: dc.l 0,0,0,0,0,0

draweditrec: ;draw block, block: d0 = x, d1 = y, d2 = colour.

	move.l gfxbase(a5),a6
	move.l rastport(a5),a2

	exg.l d0,d2
	move.l d1,d3
.loop
	move.l a2,a1
	jsr -342(a6)

	move.l a2,a1
	
	move.l d2,d0
	mulu eb_dx(a5),d0
	move.l d3,d1
	mulu eb_dy(a5),d1
	move.l d0,d2
	move.l d1,d3
	add.w eb_dx(a5),d2
	sub.w #1,d2
	add.w eb_dy(a5),d3
	sub.w #1,d3

	jsr -306(a6)
	
	rts


getpixelnumber:	;get block pixel: d0 = x, d1 = y, a0 = block
	        ;d0-d5,a0 = currupted. d0 = pixel colour
	mulu blockwidth(a5),d1
	lea (a0,d1.w),a0	;=at line of block
	move.l d0,d1
	lsr.w #3,d0
	lea (a0,d0.w),a0	;=at start of byte
	lsl.w #3,d0
	sub.w d0,d1
	move.w #7,d0
	sub.w d1,d0		;=bit to Btst
	move.w bitplanes(a5),d1
	subq #1,d1
	moveq.l #0,d2
	moveq.l #0,d3
	move.w blockwidth(a5),d3
	mulu.w blockheight(a5),d3	;= bitplane size
	moveq.l #1,d4
.bitplanes
	move.b (a0),d5
	btst d0,d5
	beq .nobit
	or.w d4,d2	
.nobit	lsl.w #1,d4
	lea (a0,d3.w),a0
	dbra d1,.bitplanes
	move.l d2,d0	
	rts
getmasknumber:	;get block pixel: d0 = x, d1 = y, a0 = block
	        ;d0-d5,a0 = currupted. d0 = pixel colour
	mulu blockwidth(a5),d1
	lea (a0,d1.w),a0	;=at line of block
	move.l d0,d1
	lsr.w #3,d0
	lea (a0,d0.w),a0	;=at start of byte
	lsl.w #3,d0
	sub.w d0,d1
	move.w #7,d0
	sub.w d1,d0		;=bit to Btst
	moveq.l #0,d2
	moveq.l #0,d3
	move.w blockwidth(a5),d3
	mulu.w blockheight(a5),d3	;= bitplane size
	moveq.l #1,d4
.bitplanes
	move.b (a0),d5
	btst d0,d5
	beq .nobit
	or.w d4,d2	
.nobit	move.l d2,d0	
	rts


writepixel: ;draws a pixel: d0 = x pos, d1 = ypos a0 = block d5 = colour
	mulu blockwidth(a5),d1
	lea (a0,d1.w),a0	;=at line of block
	move.l d0,d1
	lsr.w #3,d0
	lea (a0,d0.w),a0	;=at start of byte
	lsl.w #3,d0
	sub.w d0,d1
	move.w #7,d0
	sub.w d1,d0		;=bit to set
	move.w bitplanes(a5),d1
	subq #1,d1
	moveq.l #0,d2
	moveq.l #0,d3
	move.w blockwidth(a5),d3
	mulu.w blockheight(a5),d3	;= bitplane size

.bitplanes
	;move.b (a0),d4	;the byte of data of block
	btst #0,d5
	beq .nobit
	bset d0,(a0)
	bra .nobitr
.nobit	bclr d0,(a0)
.nobitr
	lsr.w #1,d5
	lea (a0,d3.w),a0
	dbra d1,.bitplanes
	move.l d2,d0	
	rts

editmouse:
	moveq.l #0,d6
	move.l d6,d7
	move.w mousex(a5),d6
	move.w mousey(a5),d7

	moveq.l #0,d0
	moveq.l #0,d1
	cmp.w #164-3,d7
	blo .splot
	cmp.w #200-3,d7
	bhi .splot
	move.w d6,d0
	move.w palspace(a5),d1
	divu d1,d0
	move.w palcolours(a5),d2
	cmp.w d2,d0
	bhi .nope

	move.l d0,-(sp)
	move.b #1,d0
	move.l #208,d2
	bsr palcol_arrow
	move.l (sp)+,d0
	move.w d0,currentcolour(a5)
	move.b #0,d0
	move.l #208,d2
	bsr palcol_arrow

.nope
	rts
.splot				;d6 = x, d7 = y


 ;	sub.l #3,d7
 	tst.l d7
	bmi .nope
	tst.l d6
	bmi .nope
	divu eb_dy(a5),d7

	divu eb_dx(a5),d6
	ext.l d7
	ext.l d6
	move.l d7,d1
	move.l d6,d0
	
	cmp.w blockpixelwidth(a5),d0
	bhs .nope
	cmp.w blockheight(a5),d1
	bhs .nope
	
	move.w drawgad1+12,d6
	
	btst #7,d6
	beq .off
			;Reached when Mask Selected. D0 = X pixel, D1 = Y pixel
	move.l currentblockadr(a5),a0
	lea block_gfx(a0),a0
	
	move.l d0,d6
	move.l d1,d7
	bsr getpixelnumber
	move.l d0,d2
	move.l d6,d0
	move.l d7,d1
	bsr draweditrec

	move.l d6,d0
	move.l d7,d1

	move.w drawgad2+12,d5
	btst #7,d5
	beq .setd
	move.l currentblockadr(a5),a4
	move.w blockmask(a5),d2
	lea (a4,d2.w),a0
	move.l d0,d6
	move.l d1,d7
	moveq #0,d5

	bsr writepixelmask
	rts
.setd
	move.l currentblockadr(a5),a4
	move.w blockmask(a5),d2
	lea (a4,d2.w),a0
	move.l d0,d6
	move.l d1,d7
	moveq #1,d5

	bsr writepixelmask
	move.l d6,d0
	move.l d7,d1

	mulu eb_dy(a5),d1
	mulu eb_dx(a5),d0
	move.w eb_dx(a5),d2
	move.w eb_dx(a5),d3
	ext.l d2
	ext.l d3
	bsr drawdotty
	rts	
	
.off
	
	move.l currentblockadr(a5),a4
	lea block_gfx(a4),a0
	move.l d0,d6
	move.l d1,d7
	moveq.l #0,d5
	move.w currentcolour(a5),d5
	bsr writepixel
	cmp.w #80,blockpixelwidth(a5)
	bhi .noactual
	move.w currentcolour(a5),d0
	move.l rastport(a5),a1
	move.l gfxbase(a5),a6
	jsr -342(a6)
	move.l d6,d0
	move.l d7,d1
	add.w blockeditactualx(a5),d0
	add.w blockeditactualy(a5),d1
	move.l rastport(a5),a1
	jsr -324(a6)
.noactual

	move.l d6,d0
	move.l d7,d1
	moveq.l #0,d2
	move.w currentcolour(a5),d2


	bsr draweditrec
	rts

writepixelmask: ;draws a pixel: d0 = x pos, d1 = ypos a0 = block ;d5 = bit to save 
	mulu blockwidth(a5),d1
	lea (a0,d1.w),a0	;=at line of block
	move.l d0,d1
	lsr.w #3,d0
	lea (a0,d0.w),a0	;=at start of byte
	lsl.w #3,d0
	sub.w d0,d1
	move.w #7,d0
	sub.w d1,d0		;=bit to set
	moveq.l #0,d3
	move.w blockwidth(a5),d3
	mulu.w blockheight(a5),d3	;= bitplane size

	btst #0,d5
	beq .nobit
	bset d0,(a0)
	bra .nobitr
.nobit	bclr d0,(a0)
.nobitr
		
	rts
	

drawdotty: * Draws "Dots" to show selected. It "And's" %01010... to bitplanes.
	   * Call D0 = x pos, d1 = y pos, d2 = x width, d3 = y height.
	subq #1,d2
	addq #2,d1		;correct it
	move.w bitplanes(a5),d4
	subq.w #1,d4
	lea bp1(a5),a4
	
	move.l (a4)+,a0
	subq.w #1,d3
	mulu #screenwidth,d1
	move.w d0,d5
	lsr.w #5,d5	;/32
	move.w d5,d7
	add.w d7,d7	;*2
	add.w d7,d7	;*4
	
	add.w d7,d1
	lsl.w #5,d5	;*32
	sub.w d5,d0	;d0 = x pixels right to strt.
	lea (a0,d1.w),a0
	
	move.w d0,d7
	add.w d2,d7
	cmp.w #32,d7
	bhs .not1c
	moveq.l #-1,d5
	lsr.l d0,d5
	move.w #31,d0
	sub.w d7,d0
	move.l #$88888888,d6
	lsr.l d0,d5
	lsl.l d0,d5
	move.l d6,d7
	lsr.l #2,d7
	and.l d5,d6
	and.l d5,d7
	
	
.loop2	move.w d3,d5
.loop	eor.l d6,(a0)
	subq #1,d5
	bmi .done

	eor.l d7,40(a0)
	lea 80(a0),a0
	dbra d5,.loop
.done	move.l (a4)+,a0
	lea (a0,d1.w),a0
	dbra d4,.loop2
	rts
.not1c	
	moveq.l #-1,d5
	lsr.l d0,d5
	move.l #$88888888,d2
	move.l d2,d6
	move.l d6,d0
	lsr.l #2,d0
	move.l d0,eormidb(a5)
	move.l d2,eormida(a5)
	and.l d5,d6
	and.l d5,d0
	move.l d0,eorstrta(a5)
	move.l d6,eorstrtb(a5)
	sub.w #32,d7
	move.l d7,d5
	lsr.w #5,d5
	move.w d5,numberofeorlongs(a5)
	lsl.w #5,d5
	sub.w d5,d7
	move.w #31,d0
	sub.w d7,d0
	moveq.l #-1,d5
	lsr.l d0,d5
	lsl.l d0,d5
	move.l d2,d6
	lsr.l #2,d6
	and.l d5,d6
	and.l d5,d2
	move.l d2,eorenda(a5)
	move.l d6,eorendb(a5)
	move.w numberofeorlongs(a5),d7
	sub.w #1,numberofeorlongs(a5)
	add.w #1,d7
	add.w d7,d7
	add.w d7,d7
	move.w #40,d6
	sub.w d7,d6
.outer	move.w d3,d5
.lines	move.l eorstrta(a5),d7
	eor.l d7,(a0)+
	move.w numberofeorlongs(a5),d0
	bmi .donelongs
	move.l eormida(a5),d7
.longloop eor.l d7,(a0)+
	dbra d0,.longloop
.donelongs
	move.l eorenda(a5),d7
	eor.l d7,(a0)
	subq #1,d5
	bmi .donelines
	lea (a0,d6.w),a0
	move.l eorstrtb(a5),d7
	eor.l d7,(a0)+
	move.w numberofeorlongs(a5),d0
	bmi .donelongs2
	move.l eormidb(a5),d7
.longloop2 eor.l d7,(a0)+
	dbra d0,.longloop2
.donelongs2
	move.l eorendb(a5),d7
	eor.l d7,(a0)
	lea (a0,d6.w),a0
	dbra d5,.lines
.donelines
	move.l (a4)+,a0
	lea (a0,d1.w),a0
	dbra d4,.outer

	rts

g_back:
	move.w #0,windowtype(a5)
	bsr updatewindow
	bne nowindow
	rts
g_editblock:
	move.w #2,windowtype(a5)
	bsr updatewindow
	bne nowindow
	bsr wind2setup

	rts

editgad17
	dc.l editgad18
	dc.w 220,74,90,30,5,$1,1
	dc.l image_90_30
	dc.l 0,txted17,0,0,0,0
txted17: dc.b 11,0,0,0
	dc.w 10,10,0,0
	dc.l txt_saveiffblock,0
	dc.l g_saveiffblock
txt_saveiffblock: dc.b "Save IFF",0
	even
editgad18
	dc.l editgad19
	dc.w 220,220,90,30,5,$1,1
	dc.l image_90_30
	dc.l 0,txted18,0,0,0,0
txted18: dc.b 13,0,0,0
	dc.w 10,10,0,0
	dc.l txt_deleteblock,0
	dc.l g_deleteblock
txt_deleteblock: dc.b "REMOVE!!!",0
	even
editgad19
	dc.l 0
	dc.w 130,220,90,30,5,$1,1
	dc.l image_90_30
	dc.l 0,txted19,0,0,0,0
txted19: dc.b 11,0,0,0
	dc.w 6,10,0,0
	dc.l txt_insertblock,0
	dc.l g_insertblock
txt_insertblock: dc.b "New Block",0
	even
drawgad1
	dc.l drawgad2
	dc.w 10,224,199,27,5,$101,1
	dc.l image_199_27
	dc.l 0,txtdraw1,0,0,0,0
txtdraw1: dc.b 11,0,0,0
	dc.w 6,8,0,0
	dc.l txt_maskon,0
	dc.l g_maskswap
txt_maskon: dc.b "Boxed = Edit Mask On",0
	even
drawgad2
	dc.l 0
	dc.w 220,222,90,30,5,$101,1
	dc.l image_90_30
	dc.l 0,txtdraw2,0,0,0,0
txtdraw2: dc.b 11,0,0,0
	dc.w 6,10,0,0
	dc.l txt_setclear,0
	dc.l g_setclear
txt_setclear: dc.b "Set/Clear",0
	even
g_setclear:
	rts	
g_maskswap:
	move.w #0,tempspace(a5)
	move.w drawgad1+12,d6
	btst #7,d6
	beq .off
	move.w #-1,tempspace(a5)
.off
	move.l currentblockadr(a5),a4
	move.w blockmask(a5),d0
	lea (a4,d0.w),a4
	moveq.l #0,d7	;y
.yline	moveq.l #0,d6	;x
.xline
	move.l currentblockadr(a5),a0
	lea block_gfx(a0),a0
	
	move.l d6,d0
	move.l d7,d1
	bsr getpixelnumber
	move.l d0,d2
	move.l d6,d0
	move.l d7,d1
	bsr draweditrec

	tst.w tempspace(a5)
	beq .nodraw

	move.l a4,a0
	move.l d6,d0
	move.l d7,d1
	bsr getmasknumber
	tst.b d0
	beq .nodraw
	move.l d6,d0
	move.l d7,d1
	mulu eb_dy(a5),d1
	mulu eb_dx(a5),d0
	move.w eb_dx(a5),d2
	move.w eb_dx(a5),d3
	ext.l d2
	ext.l d3
	movem.l d0-d7/a0-a6,-(sp)
	bsr drawdotty
	movem.l (sp)+,d0-d7/a0-a6
.nodraw
	add.w #1,d6
	cmp.w blockpixelwidth(a5),d6
	blo .xline
	add.w #1,d7
	cmp.w blockheight(a5),d7
	blo .yline
	

	rts

g_namechoose
	move.w #4,windowtype(a5)
	
	move.w #0,choose_type(a5)
	move.l currentblockadr(a5),a0
	move.w block_name(a0),choose_num(a5)
	move.l #ch_blockname,choose_name(a5)

	bsr gochoose
	move.l currentblockadr(a5),a0
	move.w choose_num(a5),block_name(a0)
	move.w #0,exitbit(a5)
	move.w #2,windowtype(a5)
	bsr updatewindow
	bne nowindow
	bsr wind2setup

	rts
g_bye:	move.w #1,exitbit(a5)
	rts

g_spec1choose
	move.w #4,windowtype(a5)
	
	move.w #1,choose_type(a5)
	move.l currentblockadr(a5),a0
	move.w block_special(a0),choose_num(a5)
	move.l #ch_blocktype,choose_name(a5)

	bsr gochoose
	move.l currentblockadr(a5),a0
	move.w choose_num(a5),block_special(a0)
	move.w #0,exitbit(a5)
	move.w #2,windowtype(a5)
	bsr updatewindow
	bne nowindow
	bsr wind2setup

	rts

ch_blocktype: dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l ch_blocktypea,0
ch_blocktypea dc.b "Extra Name (id=1)",0
	even


g_blockspec1plus
	move.l currentblockadr(a5),a0
	move.w block_special(a0),d0
	add.w #1,d0
	beq .error
	move.w #1,d1
	bsr getnameadr
	bne .error
	move.l currentblockadr(a5),a0
	add.w #1,block_special(a0)
	bsr updateeditscreen
.error	rts

g_blockspec1minus
	move.l currentblockadr(a5),a0
	move.w block_special(a0),d0
	beq .error
	sub.w #1,d0
	move.w #1,d1
	bsr getnameadr
	bne .error
	move.l currentblockadr(a5),a0
	sub.w #1,block_special(a0)
	bsr updateeditscreen
.error	rts

g_blockspec2plus
	move.l currentblockadr(a5),a0
	move.b block_special+2(a0),d0
	add.b #1,d0
	beq .error
	move.b d0,block_special+2(a0)
	bsr updateeditscreen
.error	rts
	
g_blockspec2minus
	move.l currentblockadr(a5),a0
	move.b block_special+2(a0),d0
	beq .error
	sub.b #1,d0
	move.b d0,block_special+2(a0)
	bsr updateeditscreen
.error	rts

g_blockspec3plus
	move.l currentblockadr(a5),a0
	move.b block_special+3(a0),d0
	add.b #1,d0
	beq .error
	move.b d0,block_special+3(a0)
	bsr updateeditscreen
.error	rts

g_blockspec3minus
	move.l currentblockadr(a5),a0
	move.b block_special+3(a0),d0
	beq .error
	sub.b #1,d0
	move.b d0,block_special+3(a0)
	bsr updateeditscreen
.error	rts

g_editname:
	bsr scrback
	lea txt_editaname,a0
	bsr plotmess
	lea tempspace(a5),a0
	move.l a0,a2
	move.l #98,d3
	bsr getmessage
	move.b #10,tempspace+14(a5)
	cmp.b #10,tempspace(a5)
	beq .noname
	lea tempspace(a5),a0
	moveq.l #0,d0
.loop	cmp.b #10,(a0)+

	beq .got
	addq.l #1,d0	
	bra .loop
.got				* D0 = Length of new name.
	move.b #-1,-1(a0)
	move.l d0,d5
	lea map_text,a0
	bsr opennewfile		;open text file
	bne .errorbig

	lea saving,a0
	bsr plotmess

	move.w choose_num(a5),d0
	move.w choose_type(a5),d1
	move.l textbase(a5),a0
	move.w d0,d2
.xloop	move.b (a0)+,d0
	cmp.b d0,d1
	beq .xfound
.xzip	move.b (a0)+,d0
	cmp.b #" ",d0
	bhs .xzip
	sub.l #1,a0
	bra .xloop
.xfound	
	tst.w d2
	beq .xgot
	sub.w #1,d2
	bra .xzip
.xgot				;a0 = start of new text
	move.l a0,d6
	move.l a0,d0
	move.l textbase(a5),a0
	sub.l a0,d0

	bsr savetofile		;save text to start of edit bit
	bne .errorbig
	move.l d5,d0
	lea tempspace(a5),a0
	bsr savetofile
	bne .errorbig
	
	move.l d6,a0
	
.xfo	move.b (a0)+,d0
	cmp.b #$ff,d0
	beq .xddd
	cmp.b #" ",d0
	blo .xddd
	sub.l #1,d5
	bra .xfo

.xddd	sub.l #1,a0
	
	move.l a0,a1
	moveq.l #0,d0
.xgetlast
	add.l #1,d0
	cmp.b #$ff,(a1)+
	bne .xgetlast
	bsr savetofile
	bne .errorbig
	bsr closefile
	add.l textsize(a5),d5
	
	bsr releasetext
	move.l d5,textsize(a5)
.retry	bsr allocatetext
	bne .baderror
.retry2	
	lea map_text,a0
	bsr openoldfile
	bne .ebaderror

	lea loading,a0
	bsr plotmess

	move.l textbase(a5),a0
	move.l textsize(a5),d0
	bsr loadtofile
	bne .ebaderror
	bsr closefile

	move.l textsize(a5),theprefs(a5)
.retry3
	lea map_prefs,a0
	bsr opennewfile
	bne .dbaderror

	lea saving,a0
	bsr plotmess

	lea theprefs(a5),a0
	move.l #92,d0
	bsr savetofile
	bne .dbaderror
	bsr closefile

	move.l currentblockadr(a5),a0
	move.w choose_num(a5),d0
	move.w d0,block_name(a0)


	bsr updatechoosescreen
.noname	
	bsr scrfront
	rts
.error	lea nomem,a0
	bsr plotmess
	bsr readkey
	bra .noname
.errorbig
	lea serror,a0
	bsr plotmess
	bsr readkey
	bra .noname
.baderror
	lea baderror,a0
	bsr plotmess
	bsr askyn
	beq .retry
.plis
	move.l bye(a5),a7
	rts
.ebaderror
	lea noloade,a0
	bsr plotmess
	bsr askyn
	beq .retry2
	bra .plis
.dbaderror
	lea nosaved,a0
	bsr plotmess
	bsr askyn
	beq .retry3
	bra .plis

g_insertname:
	bsr scrback
	lea txt_insertaname,a0
	bsr plotmess
	lea tempspace(a5),a0
	move.l a0,a2
	move.l #98,d3
	bsr getmessage
	move.b #10,tempspace+14(a5)
	cmp.b #10,tempspace(a5)
	beq .noname
	lea tempspace(a5),a0
	moveq.l #1,d0
.loop	cmp.b #10,(a0)+

	beq .got
	addq.l #1,d0	
	bra .loop
.got				* D0 = Length of new name.
	move.b #-1,-1(a0)
	move.l d0,d5
	lea map_text,a0
	bsr opennewfile		;open text file
	bne .errorbig

	lea saving,a0
	bsr plotmess

	move.l textbase(a5),a0
	move.l textsize(a5),d0
	sub.l #1,d0
	bsr savetofile		;save text (except -1)
	bne .errorbig
	
	lea choose_type+1(a5),a0
	moveq.l #1,d0
	bsr savetofile
	bne .errorbig
	
	move.l d5,d0
	lea tempspace(a5),a0
	bsr savetofile
	bne .errorbig
	bsr closefile
	add.l textsize(a5),d5
	
	bsr releasetext
	move.l d5,textsize(a5)
.retry	bsr allocatetext
	bne .baderror
.retry2	
	lea map_text,a0
	bsr openoldfile
	bne .ebaderror

	lea loading,a0
	bsr plotmess

	move.l textbase(a5),a0
	move.l textsize(a5),d0
	bsr loadtofile
	bne .ebaderror
	bsr closefile

	move.w choose_type(a5),d0
	bsr getlastname
	move.w d0,choose_num(a5)

	move.l textsize(a5),theprefs(a5)
.retry3
	lea map_prefs,a0
	bsr opennewfile
	bne .dbaderror

	lea saving,a0
	bsr plotmess

	lea theprefs(a5),a0
	move.l #92,d0
	bsr savetofile
	bne .dbaderror
	bsr closefile

	move.l currentblockadr(a5),a0
	move.w choose_num(a5),d0
	move.w d0,block_name(a0)


	bsr updatechoosescreen
.noname	
	bsr scrfront
	rts
.error	lea nomem,a0
	bsr plotmess
	bsr readkey
	bra .noname
.errorbig
	lea serror,a0
	bsr plotmess
	bsr readkey
	bra .noname
.baderror
	lea baderror,a0
	bsr plotmess
	bsr askyn
	beq .retry
.plis
	move.l bye(a5),a7
	rts
.ebaderror
	lea noloade,a0
	bsr plotmess
	bsr askyn
	beq .retry2
	bra .plis
.dbaderror
	lea nosaved,a0
	bsr plotmess
	bsr askyn
	beq .retry3
	bra .plis

getlastname:
	move.b d0,d1
	move.l textbase(a5),a1

	moveq #-1,d0
.loop
	cmp.b (a1),d1
	bne .neq
	addq.l #1,d0
.neq
	cmp.b #-1,(a1)+
	beq .gotit
.blip	move.b (a1)+,d3
	cmp.b #-1,d3
	beq .gotit
	cmp.b #" ",d3
	bhs .blip
	sub.l #1,a1
	bra .loop	
	
.gotit	rts
	
noloade: dc.b "Cannot load text. Y = Try again. N = exit program",10,0
nosaved: dc.b "Cannot save prefs. Y = Try again. N = exit program",10,0

baderror: dc.b "Serious Error. Cannot allocate memory for text. Do you want ",10
	dc.b "to try again (Y) or exit this program (N)?",10,0
serror: dc.b "ERROR - Cannot Save! Go to the main menu and try again!!!",10
	dc.b "press return please [Return]",0
txt_editaname: dc.b "Edit name: Please type in the new name.",10
	dc.b "Just press return to cancel this option.",10
	dc.b "Maximum length is 13 characters",10,10,0
txt_insertaname: dc.b "Please type in the new name to be inserted.",10
	dc.b "Just press return to cancel this option.",10
	dc.b "Maximum length is 13 characters",10,10,0
	even

gochoose:

	bsr updatewindow
	bne nowindow

	

	move.l intbase(a5),a6
	lea ch_txt1,a1
	move.l #0,d0
	move.l #0,d1
	move.l rastport(a5),a0
	jsr -216(a6)
	move.l choose_name(a5),a1
	move.l #140,d0
	move.l #0,d1
	move.l rastport(a5),a0
	jsr -216(a6)
	move.l intbase(a5),a6
	lea ch_txt2,a1
	move.l #0,d0
	move.l #16,d1
	move.l rastport(a5),a0
	jsr -216(a6)
	move.l intbase(a5),a6
	lea ch_txt3,a1
	move.l #0,d0
	move.l #32,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	bsr updatechoosescreen
	bsr adit
	
	rts
updatechoosescreen:
	move.l #130,d0
	move.l #18,d1
	bsr writetextnum

	move.l #80,d0
	move.l #32,d1
	bsr writechoosename
	rts


writechoosename:

	move.l d0,d6
	move.l d1,d7
;	move.l currentblockadr(a5),a0
;	move.w block_name(a0),d0
	move.w choose_num(a5),d0
	move.w choose_type(a5),d1
	bsr getnameadr
	move.l a0,cbnstra

	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts


writetextnum:

	move.l d0,d6
	move.l d1,d7
	moveq #0,d0
	move.w choose_num(a5),d0
	lea tempspace(a5),a0
	bsr worddecconv
	move.b #0,(a0)
	lea tempspace(a5),a0
	move.l a0,cbnstra

	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts


ch_txt1: dc.b 1,0,0,0
	dc.w 0,0,0,0
	dc.l ch_txt1a,0
ch_txt1a dc.b "Text screen for:",0
	even
ch_txt2: dc.b 11,0,0,0
	dc.w 0,0,0,0
	dc.l ch_txt2a,0
ch_txt2a dc.b "Text number:",0
	even
ch_txt3: dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l ch_txt3a,0
ch_txt3a dc.b "Its name:",0
	even


ch_blockname: dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l ch_blocknamea,0
ch_blocknamea dc.b "Block Name (id=0)",0
	even



g_blockchooseplus:
	move.w choose_num(a5),d0
	add.w #1,d0
	beq .error
	move.w choose_type(a5),d1
	bsr getnameadr
	bne .error
	add.w #1,choose_num(a5)
	move.l currentblockadr(a5),a0
	move.w choose_num(a5),d0
	move.w d0,block_name(a0)


	bsr updatechoosescreen
.error	rts



g_blockchooseminus:
	move.w choose_num(a5),d0
	beq .error
	sub.w #1,d0
	move.w choose_type(a5),d1
	bsr getnameadr
	bne .error
	sub.w #1,choose_num(a5)
	move.l currentblockadr(a5),a0
	move.w choose_num(a5),d0
	move.w d0,block_name(a0)


	bsr updatechoosescreen
.error	rts



g_insertblock:
	cmp.w #65534,numberofblocks(a5)
	beq .error
	move.l exec(a5),a6
	move.l #$10000,d1
	moveq.l #0,d0
	move.w blocksize(a5),d0
	jsr -198(a6)
	tst.l d0
	beq .error
	move.l d0,a4
	move.l currentblockadr(a5),a0
.getlast move.l (a0),d0
	beq .gotlast
	move.l d0,a0
	bra .getlast
.gotlast
	move.w numberofblocks(a5),d0
	move.w d0,block_number(a4)
	add.w #1,numberofblocks(a5)

	move.l a0,block_prev(a4)
	move.l a4,(a0)

	move.w d0,currentblock(a5)
	move.l a4,currentblockadr(a5)

	bsr updateeditscreen

	rts

.error	bsr warning
	rts

wind2setup:
	move.l intbase(a5),a6
	lea prmbs,a1
	move.l #0,d0
	move.l #0,d1
	move.l rastport(a5),a0
	jsr -216(a6)


	lea bnst,a1
	move.l #0,d0
	move.l #10,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	lea bnnam,a1
	move.l #0,d0
	move.l #32,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	lea bnspec1,a1
	move.l #0,d0
	move.l #52,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	lea bnspec2,a1
	move.l #0,d0
	move.l #72,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	lea bnspec3,a1
	move.l #0,d0
	move.l #92,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	bsr updateeditscreen
	rts


choosegad1:
	dc.l choosegad2
	dc.w 10,200,90,30,5,$1,1
	dc.l image_90_30
	dc.l 0,txtchoose1,0,0,0,0
txtchoose1: dc.b 11,0,0,0
	dc.w 14,12,0,0
	dc.l txt_bye,0
	dc.l g_bye
txt_bye: dc.b "Chosen",0
	even

choosegad2:
	dc.l choosegad3
	dc.w 110,14,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txtchoose2,0,0,0,0
txtchoose2: dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_plus,0
	dc.l g_blockchooseplus
choosegad3:
	dc.l choosegad4
	dc.w 176,14,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txtchoose3,0,0,0,0
txtchoose3: dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_minus,0
	dc.l g_blockchooseminus
choosegad4:
	dc.l choosegad5
	dc.w 50,80,199,27,5,$1,1
	dc.l image_199_27
	dc.l 0,txtchoose4,0,0,0,0
txtchoose4: dc.b 11,0,0,0
	dc.w 34,8,0,0
	dc.l txt_insertname,0
	dc.l g_insertname
txt_insertname: dc.b "Insert a Name",0
	even
choosegad5:
	dc.l 0
	dc.w 50,140,199,27,5,$1,1
	dc.l image_199_27
	dc.l 0,txtchoose5,0,0,0,0
txtchoose5: dc.b 11,0,0,0
	dc.w 34,8,0,0
	dc.l txt_changename,0
	dc.l g_editname
txt_changename: dc.b "Edit this Name",0
	even



palgad1:
	dc.l palgad2
	dc.w 10,200,90,30,5,$1,1
	dc.l image_90_30
	dc.l 0,txtpg1,0,0,0,0
txtpg1:	dc.b 11,0,0,0
	dc.w 8,12,0,0
	dc.l txt_main,0
	dc.l g_back
txt_main: dc.b "Main menu",0
	even
maingad3:
	dc.l maingad4
	dc.w 54,204,199,27,5,$1,1
	dc.l image_199_27
	dc.l 0,txtmg3,0,0,0,0
txtmg3:	dc.b 11,0,0,0
	dc.w 12,10,0,0
	dc.l txt_editpal,0,g_editpal
txt_editpal: dc.b "Change level pallete.",0
	even
maingad4:
	dc.l 0
	dc.w 54,150,199,27,5,$1,1
	dc.l image_199_27
	dc.l 0,txtmg4,0,0,0,0
txtmg4:	dc.b 11,0,0,0
	dc.w 12,10,0,0
	dc.l txt_editblock,0,g_editblock
txt_editblock: dc.b "Block control screen",0
	even
editgad1:
	dc.l editgad2
	dc.w 4,220,90,30,5,$1,1
	dc.l image_90_30
	dc.l 0,txted1,0,0,0,0
txted1:	dc.b 11,0,0,0
	dc.w 8,12,0,0
	dc.l txt_main,0
	dc.l g_back

editgad2:
	dc.l editgad3
	dc.w 124,7,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted2,0,0,0,0
txted2:	dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_plus,0
	dc.l g_blockplus
txt_plus dc.b "+",0
	even
editgad3:
	dc.l editgad4
	dc.w 58,7,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted3,0,0,0,0
txted3:	dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_minus,0
	dc.l g_blockminus
txt_minus dc.b "-",0
	even
editgad4:

	dc.l editgad5
	dc.w 220,30,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted4,0,0,0,0
txted4:	dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_plus,0
	dc.l g_blocknameplus
editgad5:
	dc.l editgad6
	dc.w 156,30,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted5,0,0,0,0
txted5:	dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_minus,0
	dc.l g_blocknameminus
editgad6:
	dc.l editgad7
	dc.w 240,30,74,18,5,$1,1
	dc.l image_74_18
	dc.l 0,txted6,0,0,0,0
txted6:	dc.b 11,0,0,0
	dc.w 10,4,0,0
	dc.l txt_nchoose,0
	dc.l g_namechoose
txt_nchoose: dc.b "Change",0
	even

editgad7:
	dc.l editgad8
	dc.w 150,6,74,18,5,$1,1
	dc.l image_74_18
	dc.l 0,txted7,0,0,0,0
txted7:	dc.b 11,0,0,0
	dc.w 10,4,0,0
	dc.l txt_bchoose,0
	dc.l g_blockchoose
txt_bchoose: dc.b "Choose",0
	even
editgad8:
	dc.l editgad9
	dc.w 220,50,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted8,0,0,0,0
txted8:	dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_plus,0
	dc.l g_blockspec1plus
editgad9:
	dc.l editgad10
	dc.w 156,50,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted9,0,0,0,0
txted9: dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_minus,0
	dc.l g_blockspec1minus
editgad10:
	dc.l editgad11
	dc.w 240,50,74,18,5,$1,1
	dc.l image_74_18
	dc.l 0,txted10,0,0,0,0
txted10: dc.b 11,0,0,0
	dc.w 10,4,0,0
	dc.l txt_nchoose,0
	dc.l g_spec1choose
editgad11:
	dc.l editgad12
	dc.w 90,70,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted11,0,0,0,0
txted11: dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_plus,0
	dc.l g_blockspec2plus
editgad12:
	dc.l editgad14
	dc.w 48,70,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted12,0,0,0,0
txted12: dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_minus,0
	dc.l g_blockspec2minus

editgad14:
	dc.l editgad15
	dc.w 90,90,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted14,0,0,0,0
txted14: dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_plus,0
	dc.l g_blockspec3plus
editgad15:
	dc.l editgad16
	dc.w 48,90,16,16,5,$1,1
	dc.l image_16_16
	dc.l 0,txted15,0,0,0,0
txted15: dc.b 11,0,0,0
	dc.w 4,4,0,0
	dc.l txt_minus,0
	dc.l g_blockspec3minus
editgad16:
	dc.l editgad17
	dc.w 120,74,90,30,5,$1,1
	dc.l image_90_30
	dc.l 0,txted16,0,0,0,0
txted16: dc.b 11,0,0,0
	dc.w 10,10,0,0
	dc.l txt_loadiffblock,0
	dc.l g_loadiffblock
txt_loadiffblock: dc.b "Load IFF",0
	even

updateeditscreen:

	move.l #78,d0
	move.l #11,d1
	bsr writecbn

	move.l #46,d0
	move.l #32,d1
	bsr writecbnname

	move.l #176,d0
	move.l #34,d1
	bsr writecbnnamen

	move.l #176,d0
	move.l #54,d1
	bsr writecbnspec1

	move.l #65,d0
	move.l #74,d1
	bsr writecbnspec2

	move.l #65,d0
	move.l #94,d1
	bsr writecbnspec3

	move.l #46,d0
	move.l #54,d1
	bsr writecbnspec


	rts
writecbnname:

	move.l d0,d6
	move.l d1,d7
	move.l currentblockadr(a5),a0
	move.w block_name(a0),d0
	move.w #0,d1
	bsr getnameadr
	move.l a0,cbnstra

	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts

writecbnspec:

	move.l d0,d6
	move.l d1,d7
	move.l currentblockadr(a5),a0
	move.w block_special(a0),d0
	move.w #1,d1
	bsr getnameadr
	move.l a0,cbnstra

	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts

cbnstr: dc.b 15,0,1,0
	dc.w 0,0,0,0
cbnstra	dc.l 0,0

getnameadr:
	move.l textbase(a5),a0
	move.w d0,d2
.loop	move.b (a0)+,d0
	cmp.b #-1,d0
	beq .error
	cmp.b d0,d1
	beq .found
.zip	move.b (a0)+,d0
	cmp.b #$ff,d0
	beq .error
	cmp.b #" ",d0
	bhs .zip
	sub.l #1,a0
	bra .loop
.found	
	tst.w d2
	beq .got
	sub.w #1,d2
	bra .zip
.got
	lea tempspace(a5),a1
	move.l a1,a2
.copy	move.b (a0)+,d0
	cmp.b #" ",d0
	blo .done
	cmp.b #-1,d0
	beq .done
	move.b d0,(a1)+
	bra .copy
.done   move.b #0,(a1)
	move.l a2,a0
	
	move.w #13,d0
.ploop	tst.b (a2)+
	beq .fda
	dbra d0,.ploop
	bra .frrt
.fda	sub.l #1,a2
.zzzz	move.b #" ",(a2)+
	dbra d0,.zzzz
	move.b #0,(a2)

.frrt	
	
	true
.error	
	lea aer,a0
	false
warning: moveq #-1,d0
.loop	move.w $dff006,$dff180
	dbra d0,.loop
	rts
aer:	dc.b "ERROR - NO NAME",0
	even
	

writecbn:

	move.l d0,d6
	move.l d1,d7
	moveq #0,d0
	move.w currentblock(a5),d0
	lea tempspace(a5),a0
	bsr worddecconv
	move.b #0,(a0)
	lea tempspace(a5),a0
	move.l a0,cbnstra

	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts

writecbnnamen

	move.l d0,d6
	move.l d1,d7
	moveq #0,d0
	move.l currentblockadr(a5),a0
	move.w block_name(a0),d0
	lea tempspace(a5),a0
	bsr worddecconv
	move.b #0,(a0)
	lea tempspace(a5),a0
	move.l a0,cbnstra
	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts

writecbnspec1

	move.l d0,d6
	move.l d1,d7
	moveq #0,d0
	move.l currentblockadr(a5),a0
	move.w block_special(a0),d0
	lea tempspace(a5),a0
	bsr worddecconv
	move.b #0,(a0)
	lea tempspace(a5),a0
	move.l a0,cbnstra
	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts


writecbnspec2

	move.l d0,d6
	move.l d1,d7
	moveq #0,d0
	move.l currentblockadr(a5),a0
	move.b block_special+2(a0),d0
	lea tempspace(a5),a0
	bsr worddecconv
	move.b #0,(a0)
	lea tempspace+2(a5),a0
	move.l a0,cbnstra
	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts


writecbnspec3

	move.l d0,d6
	move.l d1,d7
	moveq #0,d0
	move.l currentblockadr(a5),a0
	move.b block_special+3(a0),d0
	lea tempspace(a5),a0
	bsr worddecconv
	move.b #0,(a0)
	lea tempspace+2(a5),a0
	move.l a0,cbnstra
	move.l intbase(a5),a6
	lea cbnstr,a1
	move.l d6,d0
	move.l d7,d1
	move.l rastport(a5),a0
	jsr -216(a6)

	rts


worddecconv:
decconv_type set 5
decconv_type2 set 10000
	decconv
	rts

prmbs:	dc.b 12,0,0,0
	dc.w 0,0,0,0
	dc.l prmb,0
prmb	dc.b "(RMB=EDIT)",0
	even
bnst:	dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l bns,0
bns	dc.b "Number:",0
	even
bnnam:	dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l bnnams,0
bnnams	dc.b "Name:",0
	even
bnspec1: dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l bnspec1s,0
bnspec1s dc.b "Any1:",0
	even
bnspec2: dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l bnspec2s,0
bnspec2s dc.b "Any2:",0
	even
bnspec3: dc.b 14,0,0,0
	dc.w 0,0,0,0
	dc.l bnspec3s,0
bnspec3s dc.b "Any3:",0

	even

g_blockplus:
	move.w numberofblocks(a5),d0
	move.l currentblockadr(a5),a0
	move.w block_number(a0),d2
	add.w #1,d2
	cmp.w d0,d2
	beq .equal
	move.w d2,currentblock(a5)
	move.l (a0),currentblockadr(a5)
	bsr updateeditscreen
.equal	rts



g_blockminus:
	tst.w currentblock(a5)
	beq .endoflist
	sub.w #1,currentblock(a5)
	move.l currentblockadr(a5),a0
	move.l block_prev(a0),currentblockadr(a5)
	bsr updateeditscreen
.endoflist rts

g_blocknameplus
	move.l currentblockadr(a5),a0
	move.w block_name(a0),d0
	add.w #1,d0
	beq .error
	move.w #0,d1
	bsr getnameadr
	bne .error
	move.l currentblockadr(a5),a0
	add.w #1,block_name(a0)
	bsr updateeditscreen
.error	rts

g_blocknameminus
	move.l currentblockadr(a5),a0
	move.w block_name(a0),d0
	beq .error
	sub.w #1,d0
	move.w #0,d1
	bsr getnameadr
	bne .error
	move.l currentblockadr(a5),a0
	sub.w #1,block_name(a0)
	bsr updateeditscreen
.error	rts

screen_defs:
	dc.w 0,0,320,256
scr_devs_bp dc.w 0
	dc.b 0,0
	dc.w 2,15,0,0
	dc.l scr_devs_name
	dc.l 0,0,0,0

pallete:
	dc.w $0000,$0F00,$00f0,$000f,$0ff0,$00ff,$09B9,$0f0f
	dc.w $0337,$07ff,$0f7f,$0ff7,$077f,$0f77,$07f7,$0777,$048f,$084f,$0f84,$0f48
	dc.w $08f4,$04f8,$026a,$062a,$0a62,$0a26,$06a2,$02a6,$022f,$0f22,$0f2f,$0f2f
	dcb.w 32,$00f

palgad2:
	dc.l 0
	dc.w 100,202,199,27,5,$1,1
	dc.l image_199_27
	dc.l 0,txtpg2,0,0,0,0
txtpg2:	dc.b 11,0,0,0
	dc.w 8,10,0,0
	dc.l txt_paliff,0
	dc.l g_palloadiff
txt_paliff: dc.b "Load IFF file Pallete",0
	even

maingad1:
	dc.l maingad2
	dc.w 10,50,52,31,5,$1,1
	dc.l image_52_31
	dc.l 0,txtmg1,0,0,0,0
txtmg1:	dc.b 11,0,0,0
	dc.w 8,12,0,0
	dc.l txt_save,0
jump:	dc.l g_save
txt_save: dc.b "Save",0
	even
maingad2:
	dc.l maingad3
	dc.w 240,50,52,31,5,$1,1
	dc.l image_52_31
	dc.l 0,txtmg2,0,0,0,0
txtmg2:	dc.b 11,0,0,0
	dc.w 8,12,0,0
	dc.l txt_exit,0,g_exit
txt_exit: dc.b "Exit",0
	even

image_90_30
	dc.w 0,0
	dc.w 90,30
	dc.w 4
	dc.l gad_90_30
	dc.b %1111,0
	dc.l 0

image_199_27
	dc.w 0,0
	dc.w 199,27
	dc.w 4
	dc.l gad_199_27	
	dc.b %1111,0
	dc.l 0

image_74_18
	dc.w 0,0
	dc.w 74,18
	dc.w 4
	dc.l gad_74_18	
	dc.b %1111,0
	dc.l 0

image_52_31
	dc.w 0,0
	dc.w 64,31
	dc.w 4
	dc.l gad_52_31	
	dc.b %1111,0
	dc.l 0

image_16_16
	dc.w 0,0
	dc.w 16,16
	dc.w 4
	dc.l gad_16_16
	dc.b %1111,0
	dc.l 0
image_up
	dc.w 0,0
	dc.w 16,16
	dc.w 4
	dc.l gad_up
	dc.b %1111,0
	dc.l 0

windowdef:
	dc.w 0,2,320,256-2
	dc.b 0,0
idcmp:	dc.l $0,$1000+$800+$10000
windgad: dc.l 0,0
	dc.l 0 ;windname
windowdef_screenhd dc.l 0
	dc.l 0
	dc.w 0,2,320,256-2
	dc.w 15
	dc.l 0,0

windname: dc.w 0

redtext: dc.b 11,0,0,0
	dc.w 4,5,0,0
	dc.l text_redtext,0
text_redtext: dc.b "Red",0
	even

greentext: dc.b 15,0,0,0
	dc.w 4,5,0,0
	dc.l text_greentext,0
text_greentext: dc.b "Green",0
	even

bluetext: dc.b 13,0,0,0
	dc.w 4,5,0,0
	dc.l text_bluetext,0
text_bluetext: dc.b "Blue",0
	even

hextext: dc.b 11,0,0,0
	dc.w 4,5,0,0
	dc.l text_hextext,0
text_hextext: dc.b "0 1 2 3 4 5 6 7 8 9 A B C D E F",0
	even

normalpallete:

	dc.w $0000,$08A8,$09B9,$0ABA,$0BCB,$0CDC
	dc.w $0EEE,$0FFF,$0797,$0565,$0232,$0F00,$0FF0,$000F,$0F0F,$00F0
	dc.w $068C,$0f00,$00f0,$000f
	dc.w $0ff0,$00ff,$0f0f,$0777,$0888,$0999,$0AAA,$0BBB,$0CCC,$0DDD,$0EEE,$0FFF


closewindow:
	move.l windowhd(a5),d0
	beq .dont
	move.l d0,a0
	move.l intbase(a5),a6
	jsr -72(a6)
	clr.l windowhd(a5)
.dont
	rts
g_save
	bsr savedata
	rts
g_exit
	move.w #1,exitbit(a5)
	rts
g_editpal
	move.w #1,windowtype(a5)
	bsr updatewindow
	bne nowindow
	move.l #0,d6			;bp strt
	move.l #70,d7			;stop
	bsr plotpal

	bsr plotrgb


	rts

plotpal:
	move.w bitplanes(a5),d0
	bsr power
	move.w d1,palcolours(a5)
	move.w #320,d0
	divu d1,d0
	move.w d0,palspace(a5) 

	move.l gfxbase(a5),a6
	move.l rastport(a5),a4


	moveq.l #0,d5
	moveq.l #0,d4
.loop
	move.l d5,d0
	move.l a4,a1
	jsr -342(a6)

	move.l a4,a1

	move.w d4,d0			;x1	;left distance
	move.l d6,d1			;y1	;bp start
	move.l d0,d2
	add.w palspace(a5),d2		;x2
	move.l d7,d3			;y2	;bp stop
	jsr -306(a6)
	
	add.w #1,d5
	add.w palspace(a5),d4
	cmp.w palcolours(a5),d5
	bls .loop

	rts

power:			;2^(d0)
	moveq.l #1,d1
	tst.b d0
	beq .got
	sub.b #1,d0
.loop	add.l d1,d1
	dbra d0,.loop
.got	rts

g_palloadiff:
	bsr scrback
	move.b #1,d0
	bsr palchange_rgbarrow

	lea txtchoosepal,a0
	bsr plotmess
	bsr choosefile
	tst.b (a0)
	beq .exit
	move.l a0,d1
	bsr openoldfile
	bne .exit2
	lea tempspace(a5),a0
	move.l #12,d0
	bsr loadtofile
	bne .exit2
	cmp.l #"FORM",tempspace(a5)
	bne .notiff
	cmp.l #"ILBM",8+tempspace(a5)
	bne .notiff

	lea tempspace(a5),a0
	move.l #"BMHD",d0
	bsr findhunk
	bne .notiff
	moveq.l #0,d0
	move.b 8(a0),d0
	cmp.b #5,d0
	bls .twa
	move.b #5,d0
.twa	bsr power
	move.w d1,-(sp)	;num of bitplanes
	bsr unloadhunk

	lea tempspace(a5),a0
	move.l #"CMAP",d0
	bsr findhunk
	beq .popa
	move.w (sp)+,d7
	bra .notiff
.popa
	move.w (sp)+,d7
	sub.w #1,d7
	lea pallete,a4
.dc	move.l #0,d0
	move.b (a0)+,d0
	move.b (a0)+,d1
	move.b (a0)+,d2
	lsl.w #4,d0
	lsr.b #4,d2
	and.w #$f00,d0
	and.w #$0f0,d1
	and.w #$f,d2
	or.b d1,d0
	or.b d2,d0
	move.w d0,(a4)+
	dbra d7,.dc

	bsr unloadhunk
	bsr closefile

	lea tpalloaded,a0
	bsr plotmess
	bsr readkey
	lea pallete,a1
	bsr setrgb

.exit
	move.b #0,d0
	bsr palchange_rgbarrow

	bsr scrfront
	rts
.exit2	lea tcantp,a0
	bsr plotmess
	bsr readkey
	bra .exit
.notiff	lea tnotiff,a0
	bsr plotmess
	bsr readkey
	bra .exit

choosefile:

	lea taskfile,a0
	bsr plotmess
	lea dirname(a5),a0
	move.l a0,a2
	move.l #120,d3
	bsr readmessage
	move.l a2,a0
.loop	move.b (a0)+,d0
	beq .got
	cmp.b #10,d0
	bne .loop
	move.b #0,-1(a0)
.got	move.l a2,a0
	
	rts

nowindow:
	bsr scrback
	lea windowerror,a0
	bsr plotmess
	bsr readkey
	move.l bye(a5),a7
	rts

findhunk:	;tempspace = a0, hunkname = d0
		;hunk in a0, kill hunk with unloadhunk
	move.l a0,a3
	move.l d0,d6
.next
	move.l #8,d0
	move.l a3,a0
	bsr loadtofile
	beq .lod
	false
.lod
	move.l (a3),d0
	cmp.l d6,d0
	beq .foundhunk

	move.l 4(a3),d2
	add.l #1,d2
	and.l #-2,d2
	move.l #0,d3
;	move.l d2,d4
	move.l diskopt_currantfile(a5),d1
	move.l dosbase(a5),a6
	jsr -66(a6)
;	cmp.l d4,d0

;	beq .next
	not.l d0
	bne .next
	lea tbackseek,a0
	bsr plotmess
	bsr closefile
	false
.foundhunk
	move.l 4(a3),d3
	add.l #1,d3
	and.l #-2,d3

	move.l d3,hunksize(a5)
	move.l d3,d0
	moveq.l #0,d1
	move.l exec(a5),a6
	jsr -198(a6)
	move.l d0,hunkbase(a5)
	bne .memok
	bsr closefile
	lea nomemhunk,a0
	bsr plotmess
	false
.memok

	move.l d0,a0
	move.l hunksize(a5),d0
	bsr loadtofile
	beq .ok
	bsr unloadhunk
	false
.ok	move.l hunkbase(a5),a0
	true

unloadhunk:
	move.l hunksize(a5),d0
	move.l hunkbase(a5),a1
	move.l exec(a5),a6
	jsr -210(a6)
	rts

palmouse:

	moveq.l #0,d6
	move.l d6,d7
	move.w mousex(a5),d6
	move.w mousey(a5),d7

	moveq.l #0,d0
	moveq.l #0,d1
	cmp.w #70,d7
	bhi .splot
	move.w d6,d0
	move.w palspace(a5),d1
	divu d1,d0
	move.w palcolours(a5),d2
	cmp.w d2,d0
	bhi .nope

	move.l d0,-(sp)
	move.b #1,d0
	move.l #73,d2
	bsr palcol_arrow
	move.b #1,d0
	bsr palchange_rgbarrow

	move.l (sp)+,d0
	move.w d0,currentcolour(a5)
	move.b #0,d0
	move.l #73,d2
	bsr palcol_arrow
	move.b #0,d0
	bsr palchange_rgbarrow


.nope
	rts
.splot
	moveq.l #0,d6
	move.l d6,d7
	move.w mousex(a5),d6
	move.w mousey(a5),d7
	lsr.w #4,d6
	cmp.w #$f,d6
	bhi .lower
	move.w #100,d4
	move.w #116,d5

	cas3 d4,d5,d7
	blo .lower
	bhi .rhigher

	move.w #8,d3
	move.w #$ff,d4
	bsr pal_swap
	bra .lower
.rhigher add.w #32,d4
	add.w #32,d5
	cas3 d4,d5,d7
	blo .lower
	bhi .ghigher
	move.w #4,d3
	move.w #$f0f,d4
	bsr pal_swap
	bra .lower
.ghigher add.w #32,d4
	add.w #32,d5
	cas3 d4,d5,d7
	bne .lower
	move.w #0,d3
	move.w #$ff0,d4
	bsr pal_swap
.lower
	rts
pal_swap:
	lea pallete,a0
	moveq.l #0,d0
	move.w currentcolour(a5),d0
	add.l d0,d0
	lea (a0,d0.l),a0
	move.w (a0),d7
	lsl.w d3,d6
	and.w d4,d7
	or.w d6,d7
	move.w d7,-(sp)
	move.l a0,-(sp)
	move.b #1,d0
	bsr palchange_rgbarrow
	move.l (sp)+,a0
	move.w (sp)+,d7
	move.w d7,(a0)
	move.b #0,d0
	bsr palchange_rgbarrow
	lea pallete,a1
	bsr setrgb
	rts
plotrgb:
	move.l intbase(a5),a6
	
	lea image_16_16,a4
	move.l rastport(a5),a3
	move.l #100,d7
	move.w #2,d3
.loop2
	move.l #0,d6
	move.w #16,d5
	move.w #15,d4
.loop
	move.l a3,a0
	move.l a4,a1
	move.l d6,d0
	move.l d7,d1
	jsr -114(a6)
	add.w d5,d6
	dbra d4,.loop
	move.l a3,a0
	lea hextext,a1
	clr.l d0
	move.l d7,d1
	jsr -216(a6)
	add.w #32,d7
	dbra d3,.loop2

	lea redtext,a1
	move.l #260,d0
	move.l #100,d1
	move.l a3,a0
	jsr -216(a6)

	lea greentext,a1
	move.l #260,d0
	move.l #132,d1
	move.l a3,a0
	jsr -216(a6)

	lea bluetext,a1
	move.l #260,d0
	move.l #164,d1
	move.l a3,a0
	jsr -216(a6)

	move.b #0,d0
	bsr palchange_rgbarrow
	move.b #0,d0
	move.l #73,d2
	bsr palcol_arrow

	rts

palcol_arrow:	;d0 = 0 (plot) d0 = 1 (erase) d2 = ypos

	move.b d0,d4
	moveq.l #0,d0
	moveq.l #0,d1
	
	move.w currentcolour(a5),d0	;colour
	move.w palspace(a5),d1		;space between colours
	mulu d1,d0			;get start of colour
	sub.l #7,d0
	lsr.w #1,d1			;half space
	add.l d1,d0			;add half space to it

	tst.b d4
	bne .clear

	move.l intbase(a5),a6
	move.l rastport(a5),a0
	lea image_up,a1

	move.l d2,d1
	jsr -114(a6)
	rts

.clear	
	mulu #screenwidth,d2
	move.l d2,d1
	lsr.l #3,d0
	bsr clear16

	rts

palchange_rgbarrow:		;d0 = 0 (plot) d0 = 1 (erase)
	move.b d0,d1
	lea pallete,a0
	moveq.l #0,d0
	move.w currentcolour(a5),d0
	add.l d0,d0
	lea (a0,d0.l),a0
	clr.l d5
	clr.l d6	
	clr.l d7
	move.w (a0),d7
	move.w d7,d6
	lsr.w #4,d6
	move.w d6,d5
	lsr.w #4,d5
	and.w #$f,d7	;blue
	and.w #$f,d6	;green
	and.w #$f,d5	;red

	tst.b d1
	bne .clearit
	lsl.w #4,d5
	lsl.w #4,d6
	lsl.w #4,d7

	move.l intbase(a5),a6
	move.l rastport(a5),a4
	lea image_up,a3

	move.l a3,a1
	move.l a4,a0
	move.l #117,d1
	move.l d5,d0
	jsr -114(a6)
	move.l a3,a1
	move.l a4,a0
	move.l #149,d1
	move.l d6,d0
	jsr -114(a6)
	move.l a3,a1
	move.l a4,a0
	move.l #181,d1
	move.l d7,d0
	jsr -114(a6)
	rts
.clearit
	add.l d5,d5
	add.l d6,d6
	add.l d7,d7
	
	move.l #(117+2)*screenwidth,d1
	move.l d5,d0
	bsr clear16
	move.l #(149+2)*screenwidth,d1
	move.l d6,d0
	bsr clear16
	move.l #(181+2)*screenwidth,d1
	move.l d7,d0
	bsr clear16

	rts
clear16	;d1 = y pos height*screenwidth ;d0 = x pos (bytes)
	move.l bp1(a5),a0
	move.w #15,d3
	move.w d3,d4
	lea (a0,d1.l),a0
	lea (a0,d0.l),a0
.b1	clr.b (a0)
	clr.b 1(a0)
	clr.b 2(a0)
	lea screenwidth(a0),a0
	dbra d4,.b1
	move.l bp2(a5),a0
	move.w d3,d4
	lea (a0,d1.l),a0
	lea (a0,d0.l),a0
.b2	clr.b (a0)
	clr.b 1(a0)
	clr.b 2(a0)
	lea screenwidth(a0),a0
	dbra d4,.b2
	move.l bp3(a5),a0
	move.w d3,d4
	lea (a0,d1.l),a0
	lea (a0,d0.l),a0
.b3	clr.b (a0)
	clr.b 1(a0)
	clr.b 2(a0)
	lea screenwidth(a0),a0
	dbra d4,.b3
	move.l bp4(a5),a0
	move.w d3,d4
	lea (a0,d1.l),a0
	lea (a0,d0.l),a0
.b4	clr.b (a0)
	clr.b 1(a0)
	clr.b 2(a0)
	lea screenwidth(a0),a0
	dbra d4,.b4
	rts



openscreen:
	tst.l screenhd(a5)
	bne .isopen
	move.w bitplanes(a5),d0
	cmp.w #4,d0
	bhs .bitok
	move.w #4,d0
.bitok	move.w d0,actualbp(a5)
	
	move.w d0,scr_devs_bp
	move.l intbase(a5),a6	
	lea screen_defs,a0
	jsr -198(a6)
	move.l d0,screenhd(a5)
	beq .error
	move.l d0,a0
	move.l $c0(a0),bp1(a5)
	move.l $c4(a0),bp2(a5)
	move.l $c8(a0),bp3(a5)
	move.l $cc(a0),bp4(a5)
	move.l $d0(a0),bp5(a5)
	lea 44(a0),a0
	move.l a0,viewport(a5)
	lea pallete,a1
	bsr setrgb

	move.l exec(a5),a6
	move.l #$10000,d1
	move.l #12,d0
	jsr -198(a6)
	move.l d0,ucprlist(a5)
	beq .error2

	lea normalpallete,a3
	move.l gfxbase(a5),a6
	move.l ucprlist(a5),a1
	move.l #$200,d0
	clr.l d1
	jsr -378(a6)			;cwait
	move.l ucprlist(a5),a1
	jsr -366(a6)			;cbump

	move.l #$180,d7
	move.w #32-1,d6
.copycop
	move.l d7,d0
	clr.l d1
	move.w (a3)+,d1
	move.l ucprlist(a5),a1		;move
	jsr -372(a6)
	move.l ucprlist(a5),a1		;bump
	jsr -366(a6)
	add.l #2,d7
	dbra d6,.copycop

	move.l ucprlist(a5),a1
	move.l #10000,d0
	move.l #256,d1
	jsr -378(a6)	
	move.l viewport(a5),a0
	move.l ucprlist(a5),20(a0)
	move.l intbase(a5),a6
	jsr -390(a6)

.isopen	true
.error	false
.error2	bsr closescreen
	bra .error
setrgb:	
	move.l viewport(a5),a0
	move.w #32,d0
	move.l gfxbase(a5),a6
	jsr -192(a6)
	rts

closescreen:
	bsr closewindow

	move.l screenhd(a5),d0
	beq .done
	move.l d0,a0
	move.l intbase(a5),a6
	jsr -66(a6)
	clr.l screenhd(a5)
.done	rts

allocateall:
	
	bsr allocatelevel
	bne .error
	bsr allocateblocks
	bne .error
	bsr allocatetext
	bne .error
	bsr allocblockchip
	bne .error
	true
.error	false

releaseall:
	bsr releaselevel
	bsr releaseblocks
	bsr releasetext
	bsr releaseblockchip
	bsr closescreen
	rts
allocblockchip:				;allocates the level memory
	moveq.l #0,d0
	move.w blockwidth(a5),d0
	add.w #1,d0
	bclr #0,d0
	mulu.w blockheight(a5),d0
	mulu.w bitplanes(a5),d0
	move.l d0,chipblockbasesize(a5)
	move.l #$10002,d1
	move.l exec(a5),a6
	jsr -198(a6)
	move.l d0,chipblockbase(a5)
	beq .error
	
	lea chipimage+4,a0
	move.w blockwidth(a5),d0
	add.w d0,d0	;*2
	add.w d0,d0	;*4
	add.w d0,d0	;*8
	move.w d0,blockpixelwidth(a5)
	move.w d0,(a0)+
	move.w blockheight(a5),(a0)+
	move.w bitplanes(a5),d0
	move.w d0,(a0)+
	move.l chipblockbase(a5),(a0)+
	bsr power
	sub.w #1,d1
	move.b d1,(a0)
	
	
	
	true
.error	false

releaseblockchip:
	move.l chipblockbasesize(a5),d0
	move.l chipblockbase(a5),a1
	move.l a1,d1
	beq .done
	move.l exec(a5),a6
	jsr -210(a6)
	clr.l chipblockbase(a5)
.done
	rts



allocatelevel:				;allocates the level memory
	move.l lvlsize(a5),d0
	move.l #$10000,d1
	move.l exec(a5),a6
	jsr -198(a6)
	move.l d0,lvlbase(a5)
	beq .error
	true
.error	false

releaselevel:
	move.l lvlsize(a5),d0
	move.l lvlbase(a5),a1
	move.l a1,d1
	beq .done
	move.l exec(a5),a6
	jsr -210(a6)
	clr.l lvlbase(a5)
.done
	rts

allocateblocks:	;allocates the blocks
	move.l exec(a5),a6
	move.l #$10000,d5
	move.w numberofblocks(a5),d7
	clr.l d6
	move.w blocksize(a5),d6

	move.l d5,d1
	move.l d6,d0
	jsr -198(a6)				;mainblock
	move.l d0,blockbase(a5)
	beq .error
	sub.w #1,d7
	beq .done
	move.w #1,d4
	move.l d0,a4
.loop	move.l d5,d1
	move.l d6,d0
	jsr -198(a6)
	beq .error
	move.l d0,(a4)
	move.l d0,a3
	move.l a4,block_prev(a3)
	move.l a3,a4
	move.w d4,block_number(a4)
	add.w #1,d4
	sub.w #1,d7
	bne .loop
.done
	true
.error	false
releaseblocks:
	move.l blockbase(a5),d0
	beq .done
	clr.l blockbase(a5)
	move.l d0,a4
.go	move.l (a4),a3
	clr.l d6
	move.w blocksize(a5),d6
	move.l exec(a5),a6
	move.l d6,d0
	move.l a4,a1
	jsr -210(a6)
	move.l a3,a4	
	move.l a3,d0
	bne .go
.done	rts

allocatetext:
	move.l textsize(a5),d0
	move.l #$10000,d1
	move.l exec(a5),a6
	jsr -198(a6)
	move.l d0,textbase(a5)
	beq .error
	true
.error	false
releasetext:
	move.l textsize(a5),d0
	move.l textbase(a5),a1
	move.l a1,d1
	beq .done
	move.l exec(a5),a6
	jsr -210(a6)
	clr.l textbase(a5)
.done
	rts

cddirectory:			;a0 = directory
	move.l a0,a4
	move.l a4,d1
	move.l #-2,d2
	move.l dosbase(a5),a6
	jsr -84(a6)
	move.l d0,lock(a5)
	beq .error
	move.l d0,d1
	jsr -126(a6)
	move.l d0,olddir(a5)
	true
.error	false
unlock:
	move.l dosbase(a5),a6

	move.l olddir(a5),d1
	jsr -126(a6)
	move.l lock(a5),d1
	beq .dont
	jsr -90(a6)
	clr.l lock(a5)
.dont	rts


readnumber:			;reads a number from con (d0 = answer)

	lea wantnumber,a0
	bsr plotmess

	lea tempspace(a5),a0
	move.l a0,a2
	move.l #50,d3
	bsr readmessage
	move.l a2,a0
	decback
	rts
getmessage:		;a0 = storage, d3 = size
	move.w d3,-(sp)
	move.l a0,-(sp)
	lea wantmessage,a0
	bsr plotmess
	move.l (sp)+,a0
	move.w (sp)+,d3
	bra readmessage

readmessage:			;reads a message (a0 = storage d3 = size)
	move.l conbase(a5),d1
	move.l a0,d2
	move.l dosbase(a5),a6
	jsr -42(a6)
	rts

askyn:				;asks for Y (set) or N (clear)
	lea askforyn,a0
	bsr plotmess
	bsr readkey
	uppercase
	cmp.b #"N",d0
	beq .no
	cmp.b #"Y",d0
	beq .yes
	lea wrongkey,a0
	bsr plotmess
	bra askyn
.yes	true
.no	false

readkey: readkey
	rts
	
plotmess: 				;A0 = message, ending in a 0
	plotmess
	rts

updatewindow:
	lea windowpallete,a0
	clr.l d0
	move.w windowtype(a5),d0
	mulu #20,d0	

	lea (a0,d0.l),a0
	move.l (a0)+,a1
	move.l (a0)+,currentgad(a5)
	move.l (a0)+,idcmp
	move.l (a0)+,d0
	beq .nosetup
	movem.l d0-d7/a0-a6,-(sp)
	move.l d0,a4
	jsr (a4)
	movem.l (sp)+,d0-d7/a0-a6
.nosetup
	move.l a0,a4
	bsr setrgb
	move.l (a4),d0
	bne .cop
	move.l ucprlist(a5),a0
	move.l 4(a0),a0
	move.l 12(a0),a0
	move.w #$200,2(a0)
	bra .nocop
.cop	move.l ucprlist(a5),a0
	move.l 4(a0),a0
	move.l 12(a0),a0
	move.w d0,2(a0)

.nocop	
	tst.l windowhd(a5)
	beq .nowind
	bsr closewindow
.nowind

	move.l currentgad(a5),windgad

	lea windowdef,a0
	move.l screenhd(a5),windowdef_screenhd
	move.l intbase(a5),a6
	jsr -204(a6)
	move.l d0,windowhd(a5)
	beq .error
	move.l d0,a0
	move.l 50(a0),rastport(a5)
	move.l 86(a0),userport(a5)

	move.l intbase(a5),a6
	jsr -390(a6)


	true
.error	false

loaddata:
	bsr scrback

	lea loadmess,a0
	bsr plotmess

.errorx
	bsr askdir

	bsr cddirectory
	beq .found
	lea nodir,a0
	bsr plotmess
	bsr askyn
	beq .errorx
	move.l bye(a5),a7
	rts
.found
	
	
	lea map_prefs,a0
	bsr openoldfile
	bne .error


	lea loading,a0
	bsr plotmess

	lea theprefs(a5),a0
	move.l #92,d0
	bsr loadtofile
	bne .error
	
	lea theprefs(a5),a0
	move.l (a0)+,textsize(a5)
	move.l (a0)+,lvlsize(a5)
	move.w (a0)+,lvlwidth(a5)
	move.w (a0)+,lvlheight(a5)
	move.w (a0)+,blocksize(a5)
	move.w (a0)+,blockmask(a5)
	move.w (a0)+,numberofblocks(a5)
	move.w (a0)+,blockwidth(a5)
	move.w (a0)+,blockheight(a5)
	move.w (a0)+,bitplanes(a5)
	move.w (a0)+,numberoflevels(a5)
	move.w (a0)+,currentlevel(a5)
	move.w #63,d0
	lea pallete,a1
.copy	move.b (a0)+,(a1)+
	dbra d0,.copy

	bsr closefile

	bsr allocateall
	bne .memerror

	lea map_text,a0
	bsr openoldfile
	bne .error

	lea loading,a0
	bsr plotmess

	move.l textbase(a5),a0
	move.l textsize(a5),d0
	bsr loadtofile
	bne .error
	bsr closefile

	
	lea map_gfx,a0
	bsr openoldfile
	bne .error

	lea loading,a0
	bsr plotmess

	move.l blockbase(a5),a4
	
	moveq.l #0,d6
	move.w blocksize(a5),d6
	sub.w #8,d6
.loadblock
	move.l a4,a0
	move.l d6,d0
	move.l a4,-(sp)
	add.l #8,a0
	move.l d6,-(sp)
	bsr loadtofile
	beq .e2
	move.l (sp)+,d6
	move.l (sp)+,a4
	bra .error
.e2	move.l (sp)+,d6
	move.l (sp)+,a4

	move.l (a4),a4
	move.l a4,d0
	bne .loadblock

	bsr closefile

	lea map_level,a4
	lea 9(a4),a0
	clr.l d0
	move.w currentlevel(a5),d0
decconv_type set 3
decconv_type2 set 100
	decconv
	
	lea map_level,a0
	bsr openoldfile
	bne .error

	lea loading,a0
	bsr plotmess

	move.l lvlbase(a5),a0
	move.l lvlsize(a5),d0
	bsr loadtofile
	bne .error
	bsr closefile

.dontload
	bsr scrfront
	true

.error	
	lea errorload,a0
	bsr plotmess
	bsr scrfront
	false
.memerror
	lea nomem,a0
	bsr plotmess
	bsr readkey
	bra .error

savedata:
	bsr scrback

	lea savemess,a0
	bsr plotmess
	bsr askyn
	bne .dontsave

	lea map_prefs,a0
	bsr opennewfile
	bne .error

	lea saving,a0
	bsr plotmess
	lea theprefs(a5),a4
	lea textsize(a5),a0
	move.l (a0),(a4)+
	bsr savelong	
	bne .error
	lea lvlsize(a5),a0
	move.l (a0),(a4)+
	bsr savelong
	bne .error
	lea lvlwidth(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea lvlheight(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea blocksize(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea blockmask(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea numberofblocks(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea blockwidth(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea blockheight(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea bitplanes(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea numberoflevels(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea currentlevel(a5),a0
	move.w (a0),(a4)+
	bsr saveword
	bne .error
	lea pallete,a0
	move.l #64,d0
	bsr savetofile
	bne .error
	lea pallete,a0
	move.w #63,d0
.bip	move.b (a0)+,(a4)+
	dbra d0,.bip
	
	bsr closefile


	lea map_text,a0
	bsr opennewfile
	bne .error

	lea saving,a0
	bsr plotmess

	move.l textbase(a5),a0
	move.l textsize(a5),d0
	bsr savetofile
	bne .error
	bsr closefile

	
	lea map_gfx,a0
	bsr opennewfile
	bne .error

	lea saving,a0
	bsr plotmess

	move.l blockbase(a5),a4
	
	moveq.l #0,d6
	move.w blocksize(a5),d6
	sub.w #8,d6
.saveblock
	move.l a4,a0
	move.l d6,d0
	move.l a4,-(sp)
	add.l #8,a0
	move.l d6,-(sp)
	bsr savetofile
	beq .e2
	move.l (sp)+,d6
	move.l (sp)+,a4
	bra .error
.e2	move.l (sp)+,d6
	move.l (sp)+,a4

	move.l (a4),a4
	move.l a4,d0
	bne .saveblock

	bsr closefile

	lea map_level,a4
	lea 9(a4),a0
	clr.l d0
	move.w currentlevel(a5),d0
decconv_type set 3
decconv_type2 set 100
	decconv
	
	lea map_level,a0
	bsr opennewfile
	bne .error

	lea saving,a0
	bsr plotmess

	move.l lvlbase(a5),a0
	move.l lvlsize(a5),d0
	bsr savetofile
	bne .error
	bsr closefile


.dontsave
	bsr scrfront
	rts
.error
	lea mnosave,a0
	bsr plotmess
	bsr readkey
	bra .dontsave

loadlong:
	move.l #4,d0
	bra loadtofile
loadword:
	move.l #2,d0
	bra loadtofile

savelong:
	move.l #4,d0
	bra savetofile
saveword:
	move.l #2,d0
	bra savetofile

savetofile:
	move.l d0,d7
	writefile
	cmp.l d7,d0
	bne .error
	lea dot,a0
	bsr plotmess
	true
.error	lea error,a0
	bsr plotmess
	bsr closefile
	move.l currentopen(a5),a0
	bsr deletefile
	false

loadtofile:
	move.l d0,d7
	readfile
	cmp.l d7,d0
	bne .error
	lea dot,a0
	bsr plotmess
	true
.error	lea error,a0
	bsr plotmess
	bsr closefile
	false

closefile:	
	move.l currentopen(a5),a4
	lea closetext,a0
	bsr plotmess
	move.l a4,a0
	bsr plotmess
	closefile
	lea eol,a0
	bsr plotmess
	rts
deletefile:
	move.l currentopen(a5),a4
	lea deletetext,a0
	bsr plotmess
	move.l a4,a0
	bsr plotmess
	deletefile
	lea eol,a0
	bsr plotmess
	rts

opennewfile:

	move.l a0,a4
	lea openmess,a0
	bsr plotmess
	move.l a4,a0
	bsr plotmess
	move.l a4,a0

	newopenfile

	tst.l filebase(a5)
	beq .openerror
	move.l a4,currentopen(a5)
	lea eol,a0
	bsr plotmess
	
	true
.openerror
	lea error,a0
	bsr plotmess
	false
openoldfile:
	move.l a0,a4
	lea openmess,a0
	bsr plotmess
	move.l a4,a0
	bsr plotmess
	move.l a4,a0
	oldopenfile

	tst.l filebase(a5)
	beq .openerror
	move.l a4,currentopen(a5)
	lea eol,a0
	bsr plotmess
	
	true
.openerror
	lea error,a0
	bsr plotmess
	false

newproject:
	lea askblockwidth,a0
	bsr plotmess
	bsr readnumber	
	move.w d0,blockwidth(a5)
	lea askblockheight,a0
	bsr plotmess
	bsr readnumber
	move.w d0,blockheight(a5)
.cols	lea askcolours,a0
	bsr plotmess
	bsr readnumber
	range d0,1,8
	tst.w d0
	bne .okay
	lea wrongcols,a0
	bsr plotmess
	bra .cols
.okay	move.w d0,bitplanes(a5)

	lea asklvlwidth,a0
	bsr plotmess
	bsr readnumber
	move.w d0,lvlwidth(a5)
	lea asklvlheight,a0
	bsr plotmess
	bsr readnumber
	move.w d0,lvlheight(a5)

.error
	bsr askdir

	bsr cddirectory
	beq .found
	lea nodir,a0
	bsr plotmess
	bsr askyn
	beq .error
	move.l bye(a5),a7
	rts
.found
	move.w #1,numberofblocks(a5)
	clr.l d0
	move.w blockwidth(a5),d0
	mulu.w blockheight(a5),d0
	move.w bitplanes(a5),d1
	add.w #1,d1
	mulu.w d1,d0
	add.w #block_gfx,d0
	move.w d0,blocksize(a5)
	move.w blockwidth(a5),d0
	mulu.w blockheight(a5),d0
	move.w bitplanes(a5),d1
	mulu.w d1,d0
	add.w #block_gfx,d0
	move.w d0,blockmask(a5)
	clr.l d0
	move.w lvlwidth(a5),d0
	mulu.w lvlheight(a5),d0
	move.l d0,d1
	add.l d0,d0
	move.l d0,lvlsp1(a5)
	add.l d1,d0
	move.l d0,lvlsp2(a5)
	add.l d1,d0
	move.l d0,lvlsize(a5)

	move.l #defualttexte-defualttext,textsize(a5)
	
	move.w #0,currentlevel(a5)
	move.w #1,numberoflevels(a5)

	bsr allocateall
	beq .alocgood
	bsr releaseall
	lea nomem,a0
	bsr plotmess
	bsr readkey
.exitit
	move.l bye(a5),a7
	rts
.alocgood
	lea defualttext,a0
	move.l textbase(a5),a1
.copytext:
	move.b (a0)+,d0
	move.b d0,(a1)+
	cmp.b #-1,d0
	bne .copytext
	
	lea txt_gonnasave,a0
	bsr  plotmess
	bsr savedata
	bne .errorsav
	
	rts
.errorsav
	lea txt_nosaveg,a0
	bsr plotmess
	bsr readkey
	bra .exitit

txt_gonnasave:
	dc.b "This verson of this map designer requires you to save the data",10
	dc.b "now. So i'm saving!...",10,0
txt_nosaveg: 
	dc.b "Uh oh! An error - Bye bye!. Press return please [Return]",0
	even

askdir:
.error	lea mdirname,a0
	bsr plotmess
	lea dirname(a5),a0
	move.l #128,d3
	bsr getmessage
	cmp.b #10,dirname(a5)
	beq .error
	lea mgonnafinddir,a0
	bsr plotmess 
	bsr readkey
	lea dirname(a5),a0
	move.l a0,a1
.fix	move.b (a1)+,d0
	cmp.b #10,d0
	bne .fix
	sub.l #1,a1
	move.b -1(a1),d0
	cmp.b #":",d0
	beq .ok
	cmp.b #"/",d0
	beq .ok
	move.b #"/",(a1)+
.ok	move.b #0,(a1)
	rts



scrfront:
	move.l screenhd(a5),d0
	beq .noscr
	move.l intbase(a5),a6
	move.l d0,a0
	jsr -252(a6)
.noscr	rts

scrback:
	move.l screenhd(a5),d0
	beq .noscr
	move.l intbase(a5),a6
	move.l d0,a0
	jsr -246(a6)
.noscr	rts
