Login Register
Frontpage Code library Pastebin

Kartan avaus- ja tallennusfunktiot

Author: aXu
Added: 27. kesäkuuta 2012 kello 20.58
Edited: 27. kesäkuuta 2012 kello 21.02
Category: Kartat

Description

Funktiot kartan avaamiseen ja tallentamiseen. Erona cb:n omaan avausfunktioon OpenMap tallentaa tilen animointitiedot TileAnim-taulukkoon. TileAnim-taulukkoa luetaan seuraavasti: TileAnim(TileNum, 0_jos_pituus/1_jos_hitaus). SaveMap ottaa tämän taulukon tietenkin huomioon. Toinen ero on siinä, että SaveMap voi tallentaa tilesetin tiedostopolun suoraan karttatiedostoon, jolloin OpenMapille ei tarvitse sitä ilmoittaa. Jos kuitenkin OpenMapille ilmoittaa tilesetin sijainnin, karttaan tallennettua sijaintia ei käytetä. Koodi on aika vanhaa ja kommentoimatonta. Jos kiinnostaa tarkemmin Tilesterin tiedostojen sisältö, kannattaa lukea Tilesterin manuaali, siellä on kerrottu tavu tavulta formaatin toiminta :)

Code

Select all
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Dim TileAnim(1,1)   //TileAnim -taulukko sisältää tilejen animoinnin(koska coolbasicissa ei voi tarkistaa sitä jälkikäteen), TileAnim(TILE, ANIMAATION_PITUUS = 0/ANIMAATION_HITAUS = 1
Function OpenMap(FileName$, Tileset$="")
	f = OpenToEdit(FileName)
		If Not(ReadByte(f) = 40 And ReadByte(f) = 192 And ReadByte(f) = 13 And ReadByte(f) =  139) Then MakeError "Can't load map "+FileName+". Map is not valid Tilester 1.3 file"
		If Tileset = "" Then
			SeekFile f, 564
			For i = 0 To 255
				b = ReadByte(f)
				If b = 0 Then Exit Else Tileset = Tileset + Chr(b)
			Next i
		EndIf
		SeekFile f, 820
		TileCount   = ReadInt(f)
		TileWidth   = ReadInt(f)
		TileHeight  = ReadInt(f)
		Map = MakeMap(ReadInt(f), ReadInt(f), TileWidth, TileHeight)
		TilesetImg  = LoadImage(Tileset)
		PaintObject Map, TilesetImg
		For i = 0 To 3
			If Not(ReadByte(f) = 254-i And ReadByte(f) = 45-i And ReadByte(f) = 12-i And ReadByte(f) = 166-i) Then MakeError "Can't load map "+FileName+". Map layer "+i+" was corrupted"
			For Y = 1 To MapHeight()
				For X = 1 To MapWidth()
					EditMap Map, i + (i = 1) - (i = 2), X, Y, ReadInt(f)
				Next X
			Next Y
		Next i
		If Not(ReadByte(f) = 250 And ReadByte(f) = 41 And ReadByte(f) = 8 And ReadByte(f) =  162) Then MakeError "Can't load map "+FileName+". Tile animation was corrupted"
		ReDim TileAnim(TileCount-1, 1)
		For i = 0 To TileCount-1
			TileAnim(i, 0) = ReadInt(f)
			TileAnim(i, 1) = ReadInt(f)
			SetTile i, TileAnim(i, 0), TileAnim(i, 1)
		Next i
		SeekFile f, 520
		MaskObject Map, ReadByte(f), ReadByte(f), ReadByte(f)
	CloseFile f
	
	Return Map
EndFunction

Function SaveMap(Map, FileName$, TilesetImg, Tileset$ = "", MaskRED = 0, MaskGREEN = 0, MaskBLUE = 0, Comment$ = "")
	f = OpenToWrite(FileName)
		WriteByte f, 40 : WriteByte f, 192 : WriteByte f, 13 : WriteByte f, 139
		WriteFloat f, 1.3
		For i = 1 To 512
			If i < Len(Comment) And i < 256 Then
				WriteByte f, Asc(Mid(Comment, i, 1))
			Else
				WriteByte f, 0
			EndIf
		Next i
		WriteByte f, MaskRED : WriteByte f, MaskGREEN : WriteByte f, MaskBLUE : WriteByte f, 0
		For i = 1 To 40
			WriteByte f, 0
		Next i
		For i = 1 To 256
			If i < Len(Tileset) Then
				WriteByte f, Asc(Mid(Tileset, i, 1))
			Else
				WriteByte f, 0
			EndIf
		Next i
		TileCount = (ImageWidth(TilesetImg)/(ObjectSizeX(Map) / MapWidth())) * (ImageHeight(TilesetImg)/(ObjectSizeY(Map) / MapHeight()))
		WriteInt f, TileCount
		WriteInt f, ObjectSizeX(Map) / MapWidth()
		WriteInt f, ObjectSizeY(Map) / MapHeight()
		WriteInt f, MapWidth()
		WriteInt f, MapHeight()
		For i = 0 To 3
			WriteByte f, 254-i : WriteByte f, 45-i : WriteByte f, 12-i : WriteByte f, 166-i
			For Y = 1 To MapHeight()
				For X = 1 To MapWidth()
					WriteInt f, GetMap2(i + (i = 1) - (i = 2), X, Y)
				Next X
			Next Y
		Next i
		WriteByte f, 250 : WriteByte f, 41 : WriteByte f, 8 : WriteByte f, 162
		For i = 0 To TileCount-1
			WriteInt f, TileAnim(i, 0)
			WriteInt f, TileAnim(i, 1)
		Next i
	CloseFile f
EndFunction

Comments

No comments. You can be first!

Leave a comment

You must be logged in to comment.