
;--------------------------------------------------------------------------
;Map Designer ©1994 Rombust/Nebula
;
;This source is public domain. It is part of my coding tutorial
;You can do what you want with it (within reason)
;It is only a preview version
;
;Contact me at: Mark, 50 victoria avenue, sleaford, lincs. NG34 7LN
;
;Sorry the source is untidy, but who cares...
;
;--------------------------------------------------------------------------

	rsreset
filebase:	rs.l 1

	opt o+,w-
	opt d+

	section code,code

	include map.join
diskopt_currantfile set filebase
block_next equ 0
block_prev equ 4
block_number equ 8
block_name equ 10
block_anim equ 12
block_animspecial equ 14
block_special equ 18
block_gfx equ 22
screenwidth equ 40

Start:

	lea variables,a5
	lea _evar(a5),a0
	move.l a5,a1
.clear	clr.b (a1)+
	cmp.l a0,a1
	bne .clear

	move.l 4.w,exec(a5)

	opendos
	opengfx
	openint
	getconoutput

	bsr main
	
	bsr releaseall
	bsr unlock
	closeint
	closegfx
	closedos

	rts

main:
	move.l a7,bye(a5)

	*lea extramess,a0
	*bsr plotmess
	*bsr readkey

	lea startmess,a0
	bsr plotmess
	bsr askyn
	beq .loadit
	bsr newproject
.bat	bsr openscreen
	beq .opened
	lea noscreen,a0
	bsr plotmess
	bsr readkey
	rts
.opened
	move.l blockbase(a5),currentblockadr(a5)
	move.w #0,currentblock(a5)
	bsr programstart
	
.exit	rts

.loadit	bsr loaddata
	beq .bat
	rts

extramess:
	dc.b "Preview of the map maker",10,10,10
	dc.b "This Version is PD Demo version",10,10
	dc.b "DO NOT SPREAD",10
	dc.b "DO NOT SPREAD",10
	dc.b "DO NOT SPREAD",10
	dc.b "DO NOT SPREAD",10
	dc.b "DO NOT SPREAD",10
	dc.b "DO NOT SPREAD",10
	dc.b "DO NOT SPREAD",10
	dc.b "Press that big key called Return (or hit Enter!) :",0
	even
programstart:
	move.w #0,windowtype(a5)
	bsr updatewindow
	beq adit
	bsr scrback
	lea windowerror,a0
	bsr plotmess
	bsr readkey
	rts

adit
.mess
	move.l userport(a5),a0
	move.l windowhd(a5),a4
	move.l exec(a5),a6
	jsr -384(a6)
	move.l 12(a4),d7
	move.l userport(a5),a0
	jsr -372(a6)
	tst.l d0
	beq .mess
	
	move.l d0,a1
	move.l 20(a1),d6
	move.l $1c(a1),a4

	jsr -378(a6)

	cmp.l #$8,d6
	bne .nomouse
	
	move.w d7,mousex(a5)
	swap d7
	move.w d7,mousey(a5)
	lea windowmouse,a0
	moveq.l #0,d0
	move.w windowtype(a5),d0
	lsl.l #3,d0

	btst #6,$bfe001
	bne .nomousea

	move.l (a0,d0.l),d0
	beq .cont
	move.l d0,a4
	jsr (a4)
	bra .cont
.nomousea

	BTST	#$2,$DFF016	;$a
	BNE.S	.nomouse
	lea 4(a0),a0
	move.l (a0,d0.l),d0
	beq .cont
	move.l d0,a4
	jsr (a4)

	bra .cont

.nomouse
	cmp.l #$40,d6
	bne .mess

	move.l $42(a4),a4
	jsr (a4)
.cont
	tst.w exitbit(a5)
	beq .mess	
	rts

windowpallete:
	;	pallete		gadgets		idcmp	setup	pal2_y_pos
	dc.l normalpallete,maingad1,$40,0,0		;window 0
	dc.l pallete,palgad1,$40+8,0,73			;window 1
	dc.l normalpallete,editgad1,$40+8,0,0		;window 2
	dc.l pallete,drawgad1,$40+8,0,208		;window 3
	dc.l normalpallete,choosegad1,$40,0,0		;window 4


windowmouse: 
	dc.l 0,0
	dc.l palmouse,0
	dc.l 0,editactual
	dc.l editmouse,endeditactual
	

g_deleteblock:
	rts

g_saveiffblock:
	rts

g_loadiffblock:
	bsr scrback
	lea txtloadiff,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.w bitplanes(a5),d0
	bne .wrongbp
	move.w (a0),d0		;width
	move.w 2(a0),d1		;height
	move.b 10(a0),d2	;compression
	move.w d0,d3
	lsr.w #3,d3
	cmp.w blockwidth(a5),d3
	blo .wrongdims
	cmp.w blockheight(a5),d1
	blo .wrongdims
	
	
	
	movem.l d0-d2,-(sp)
	bsr unloadhunk

	lea tempspace(a5),a0
	move.l #"BODY",d0
	bsr findhunk
	beq .popa
	movem.l (sp)+,d0-d2
	bra .notiff
.popa
	movem.l (sp)+,d0-d2
	
	;A0 = Body, D0 = Width, D1 = Height D2 = Compression type.
	
	move.w d0,d3
	lsr.w #3,d3 ;(/8)
	add.w #1,d3
	move.w d3,d4
	ext.l d4
	mulu d1,d4
	move.l d4,d7
	move.l d0,d5
	mulu bitplanes(a5),d7
	move.l exec(a5),a6
	move.l d7,d0
	move.l d7,temppicsize(a5)
	move.w d2,d7
	move.l d1,d6
	moveq.l #0,d1
	move.l a0,a4
	jsr -198(a6)
	tst.l d0
	beq .nomemscr
	move.l d0,temppicadr(a5)
	
	move.l a4,a0	;source
	move.l d0,a1	;dest
	move.l d5,d0	;width (pixels)
	move.l d6,d1	;height (lines)
	move.b d7,d2	;type of compression (byte)
	move.w bitplanes(a5),d3	;bitplanes
	move.l d5,-(sp)
	move.l d6,-(sp)
	bsr convilbm

	
	bsr unloadhunk
	bsr closefile

	move.l (sp)+,d6			;height source
	move.l (sp)+,d5			;width source
	lsr.w #3,d5
	move.l d6,d7
	mulu d5,d7			;planesize
	move.l temppicadr(a5),a0	;source
	move.w blockwidth(a5),d3	;width
	move.w blockheight(a5),d4	;height

	move.l currentblockadr(a5),a1
	lea block_gfx(a1),a1		;*** Change block start here ***
	move.l a0,a2
	sub.w d3,d5			;offset
;	subq #1,d5
	sub.w #1,d4
	move.w bitplanes(a5),d2
	subq #1,d2
	subq #1,d3
.cb1	move.w d4,d1		;bitplanes

.cb2	move.w d3,d6		;width
.cb3	move.b (a0)+,(a1)+	
	dbra d6,.cb3
	lea (a0,d5.w),a0	;next line
	dbra d1,.cb2
	lea (a2,d7.w),a2
	move.l a2,a0
	dbra d2,.cb1

	move.l temppicadr(a5),a1
	move.l temppicsize(a5),d0	;recover screen mem.
	move.l exec(a5),a6
	jsr -210(a6)

	lea tblockloaded,a0
	bsr plotmess
	bsr readkey
.exit
	bsr scrfront
	rts
.exit2	lea tcantlb,a0
	bsr plotmess
	bsr readkey
	bra .exit
.notiff	lea tnotiffb,a0
	bsr plotmess
	bsr readkey
	bra .exit
.wrongbp lea txtnebp,a0
	bsr plotmess
	bsr readkey
	bsr unloadhunk
	bsr closefile
	bra .exit
.wrongdims lea txtwrongdims,a0
.same	bsr plotmess
	bsr readkey
	bsr unloadhunk
	bsr closefile
	bra .exit
	rts
.nomemscr lea txtnomemscr,a0
	bra .same
g_blockchoose
	rts

convilbm: ;convert ilbm to normal. D0 = width (pixels), d1 = height
	  ;d2 = compression type. d3 = bitplanes. A0 = source A1 = dest.

	tst.b d2
	bne .compressionon
	lea $dff180,a4
	sub.w #1,d0
	
	lsr.w #3,d0
	add.w #1,d0
	moveq.l #0,d6
	move.w d0,d6
	mulu d1,d6	;planesize
	move.l d6,d7
	mulu d3,d7	;total size
	sub.w #1,d7
	subq #1,d3	;bitplanes
	subq #1,d1	;height
.lc	move.w d3,d2	;bitplanes
.lb	move.w d0,d4	;width
	subq #1,d4
.la	moveq.l #0,d5
	move.w d2,d5
	mulu d6,d5	;planesize * bitplanes
	move.l d6,a2
	moveq.l #0,d6
	move.w d1,d6	;height
	mulu d0,d6	;width * height
	add.l d6,d5	;planesize*bitplanes+width*height
	add.l d4,d5	;above + width
	move.l d7,d6
	sub.l d5,d6
	move.w d6,(a4)
	move.b (a0)+,(a1,d6.l)
	move.l a2,d6
	dbra d4,.la
	
	move.l a0,d5
	add.l #1,d5
	and.l #-2,d5
	move.l d5,a0
	
	dbra d2,.lb
	dbra d1,.lc
	rts
.compressionon

	lsr.w #3,d0	;/8
	moveq.l #0,d7
	move.w d0,d7
	mulu d1,d7	;plane size
	subq #1,d3	;bitplanes
	subq #1,d1	;height
	subq #1,d0	;width
.rerunloop
	move.w d3,d2
	move.l a1,a3
.bitplanes

	move.l a3,a4
	move.w d0,d6
	bsr loopreadline
	lea (a3,d7.l),a3
	dbra d2,.bitplanes
	lea 1(a1,d0.w),a1
	dbra d1,.rerunloop
	rts
loopreadline: *Used for Iff Extraction. D6 = Width. A0 = Source A4 = Dest

		clr.l	d5
		move.b	(a0)+,d5
		bpl.s	literalcopy

duplicatenext	cmp.b	#128,d5
		bne.s	normduplicate

skipbyte		clr.b	(a4)+
		subq.b	#1,d6
		bpl.s	loopreadline
		rts

normduplicate	neg.b	d5
		move.b	(a0)+,d4
loopduplicate	move.b	d4,(a4)+
		subq.b	#1,d6
		dbmi	d5,loopduplicate
		tst.b	d6
		bpl.s	loopreadline
		rts

literalcopy	sub.b	d5,d6
		subq.b	#1,d6
loopliteralcopy	move.b	(a0)+,(a4)+
		dbf	d5,loopliteralcopy
		tst.b	d6
		bpl.s	loopreadline
		rts


	include map.sub



	include map.text


	even

	section chipdata,data_c

gad_52_31:	incbin gadgets_52*31.bm
gad_199_27:	incbin gadget_199*27.bm
gad_74_18:	incbin gadget_74*18.bm
gad_90_30:	incbin gadget_90*30.bm
gad_16_16:	incbin gadget_16*16.bm
gad_up:		incbin gadget_up.bm
gad_down:	incbin gadget_down.bm

	section vars,bss


exec: 		rs.l 1
keybuff:	rs.l 1
bye:		rs.l 1
lvlsp1:		rs.l 1	;offset to special 1 (.b)
lvlsp2:		rs.l 1	;offset to special 2 (.b)
lvlsize:	rs.l 1	;level size
blockbase:	rs.l 1
actualbp:	rs.w 1
screenhd:	rs.l 1
currentopen:	rs.l 1
lock:		rs.l 1
viewport:	rs.l 1
bp1:		rs.l 1
bp2:		rs.l 1
bp3:		rs.l 1	;must be kept together
bp4:		rs.l 1
bp5:		rs.l 1
textsize:	rs.l 1
exitbit:	rs.w 1
userport:	rs.l 1
olddir:		rs.l 1
mousex:		rs.w 1
palspace:	rs.w 1
palcolours:	rs.w 1
mousey:		rs.w 1
rastport:	rs.l 1
textbase:	rs.l 1
currentcolour:	rs.w 1
currentblock:	rs.w 1
currentblockadr: rs.l 1
currentgad:	rs.l 1
lvlbase:	rs.l 1
currentlevel:	rs.w 1
windowtype:	rs.w 1
chipblockbase:	rs.l 1
eb_dx: rs.w 1
eb_dy: rs.w 1
blockpixelwidth: rs.w 1
chipblockbasesize: rs.l 1
numberoflevels: rs.w 1
hunksize:	rs.l 1
numberofeorlongs: rs.w 1
eorstrta: rs.l 1
eorstrtb: rs.l 1
eormida: rs.l 1
eormidb: rs.l 1
eorenda: rs.l 1
eorendb: rs.l 1
temppicadr: rs.l 1
temppicsize: rs.l 1
hunkbase:	rs.l 1
choose_type:	rs.w 1
choose_name:	rs.l 1
choose_num:	rs.w 1
blockeditactualx: rs.w 1
blockeditactualy: rs.w 1
windowhd:	rs.l 1
ucprlist:	rs.l 1
lvlwidth:	rs.w 1
blocksize:	rs.w 1  ;size of a block
blockmask:	rs.w 1	;offset to block mask
lvlheight:	rs.w 1
numberofblocks: rs.w 1
blockwidth:	rs.w 1
blockheight:	rs.w 1
bitplanes:	rs.w 1
theprefs:	rs.b 100
tempspace:	rs.b 50
dirname:	rs.b 128
_evar 		rs.w 1
variables: dcb.b (_evar),0

	
	end	

