Skip to content

Instantly share code, notes, and snippets.

@valscion
Created January 4, 2012 20:48
Show Gist options
  • Save valscion/1562062 to your computer and use it in GitHub Desktop.
Save valscion/1562062 to your computer and use it in GitHub Desktop.
Til2JSON
'This is your first CoolBasic program!
Include "Resource.cb"
mapName$ = "Mictlan"
Global gMapCRC
map = LoadResourceMap(mapName)
If Not IsDirectory("converted") Then MakeDir "converted"
If Not IsDirectory("converted") Then MakeError "Unable to create directory 'converted'"
If Not IsDirectory("pretty") Then MakeDir "pretty"
If Not IsDirectory("pretty") Then MakeError "Unable to create directory 'pretty'"
w = MapWidth()
h = MapHeight()
tileW = ObjectSizeX(map) / w
tileH = ObjectSizeY(map) / h
author$ = GetMapAuthor(mapName)
Print "Converting map " + w + "x" + h
q$ = Chr(34)
f = OpenToWrite("converted\"+mapName+".json")
WriteLine f, "{"
WriteLine f, " "+q+"name"+q+": "+q+mapName+q+","
WriteLine f, " "+q+"author"+q+": "+q+author+q+","
WriteLine f, " "+q+"crc32"+q+": "+gMapCRC+","
WriteLine f, " "+q+"config"+q+": {"
WriteLine f, " "+q+"maxPlayers"+q+": 10,"
WriteLine f, " "+q+"botCount"+q+": 5,"
WriteLine f, " "+q+"botDepartLimit"+q+": 7,"
WriteLine f, " "+q+"botNames"+q+": ["+q+"Bot_1"+q+", "+q+"Bot_2"+q+", "+q+"Bot_3"+q+", "+q+"Bot_4"+q+", "+q+"Bot_5"+q+", "+q+"Bot_6"+q+", "+q+"Bot_7"+q+", "+q+"Bot_8"+q+", "+q+"Bot_9"+q+", "+q+"Bot_10"+q+"],"
WriteLine f, " "+q+"botWeapons"+q+": [1, 2, 3, 4, 5, 6],"
WriteLine f, " "+q+"healthItems"+q+": 5,"
WriteLine f, " "+q+"mgunItems"+q+": 5,"
WriteLine f, " "+q+"bazookaItems"+q+": 5,"
WriteLine f, " "+q+"shotgunItems"+q+": 5,"
WriteLine f, " "+q+"launcherItems"+q+": 5,"
WriteLine f, " "+q+"chainsawItems"+q+": 5"
WriteLine f, " },"
WriteLine f, " "+q+"tileWidth"+q+": "+tileW+","
WriteLine f, " "+q+"tileHeight"+q+": "+tileH+","
WriteLine f, " "+q+"width"+q+": "+w+","
WriteLine f, " "+q+"height"+q+": "+h+","
WriteLine f, " "+q+"data"+q+": ["
For y = 1 To h
l$ = ""
For x = 1 To w
If GetMap2(2, x, y) Then
l = l + "1"
Else
l = l + "0"
EndIf
If x <> w Then l = l + ","
Next x
l = " [" + l + "]"
If y <> h Then l = l + ","
WriteLine f, l
Next y
WriteLine f, " ]"
WriteLine f, "}"
// Prettyprint
f = OpenToWrite("pretty\"+mapName+".txt")
//WriteLine f, "["
For y = 1 To h
l$ = ""
For x = 1 To w
If GetMap2(2, x, y) Then
l = l + "##"
Else
l = l + " "
EndIf
//If x <> w Then l = l + ","
Next x
l = l//" [" + l + "]"
//If y <> h Then l = l + ","
WriteLine f, l
l = ""
Next y
//WriteLine f, "]"
Execute "converted\"+mapName+".json"
Function LoadResourceMap(_mapName$)
archive$ = _mapName + ".mpc"
gMapCRC = Crc32(archive)
d = ExtractData(archive, _mapName + ".map")
If d = 0 Then
MakeError "Resource map " + _mapName + " not found!"
EndIf
o = 0
w = PeekByte(d, o) : o + 1
h = PeekByte(d, o) : o + 1
tw = PeekByte(d, o) : o + 1
th = PeekByte(d, o) : o + 1
map = MakeMap(w, h, tw, th)
For y = 1 To h
For x = 1 To w
For l = 0 To 3
EditMap map, l, x, y, PeekByte(d, o) : o + 1
Next l
Next x
Next y
Return map
EndFunction
Function GetMapAuthor(_mapName$)
mVersionData = ExtractData(_mapName + ".mpc", "versioninfo.txt")
offset = 4
strLen = PeekInt(mVersionData, offset) : offset + 4
author$ = ""
For i = 1 To strLen
c = PeekByte(mVersionData, offset) : offset + 1
author$ = author$ + Chr(c)
Next i
Return author
EndFunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment