#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG
    DIM a$, b$, c$, d$, i&, wtoput&, htoput&, buffer$, file2$, oline$,startx$,starty$,tile1$,temp3$
    IF COMMAND$ = "" THEN
    PRINT "VDP TILEMAP PADDER"
    PRINT "WRITTEN BY OERG866 OF MDEM"
    PRINT "FOR OUR MDEM PRODUCTION FOR REVISION 2011"
    PRINT ""
    PRINT "USAGE: mdmappad.exe <inputfile> <outputfile> <width in tiles>"
    PRINT "                    <height in tiles> <priority> <palette select>"
    PRINT "                    <starting x tile> <starting y tile>"
    PRINT "                    <first tile empty>"
    PRINT ""
    PRINT "Example: mdmappadd.exe tmap.bin tmap.asm 40 28 1 2 0 0 1"
    PRINT ""
    PRINT "Error: No arguments specified."
    PRINT ""
    INPUT "inputfile?", a$
    INPUT "outputfile?", b$
    INPUT "width in tiles?", c$
    INPUT "height in tiles?", d$
    DIM priority$
    INPUT "Priority (0/1)?", priority$
    DIM palette$
    DIM yourmom$
    INPUT "Palette Select (0/1/2/3)?", palette$
    INPUT "Starting X tile?", startx$
    INPUT "Starting Y tile?", starty$
    INPUT "Is first tile empty?", tile1$
    ELSE
       a$=COMMAND$(1)
       b$=COMMAND$(2)
       c$=COMMAND$(3)
       d$=COMMAND$(4)
       priority$=COMMAND$(5)
       palette$=COMMAND$(6)
       startx$=COMMAND$(7)
       starty$=COMMAND$(8)
       tile1$=COMMAND$(9)
    END IF
    OPEN a$ FOR BINARY AS #1
    DIM wtoputbegin&
    DIM htoputbegin&

    wtoputbegin& = VAL(startx$)-1
    wtoput& = 64 - wtoputbegin& - VAL(c$) +1
    htoputbegin& = VAL(STARTY$)-1
    htoput& = 32 - htoputbegin& - VAL(d$) +1
    OPEN b$ FOR BINARY  AS #2
    DIM roflmofl$
    FOR i& = 1 TO htoputbegin&
        roflmofl$ = STRING$(128, 0)
          temp3$ = temp3$ + roflmofl$
    NEXT i&
    DIM p&
    FOR i& = 1 TO VAL(c$)
          buffer$ = SPACE$(VAL(c$)*2)
          GET #1, , buffer$
          IF tile1$ = "1" THEN
          FOR p& = 1 TO LEN(buffer)
              MID$(Buffer$, (p& - 1)*2 + 1, 2) = CHR$(ASC(MID$(Buffer$, (p& - 1)*2 + 1, 1))) + CHR$(ASC(MID$(Buffer$, (p& - 1)*2 + 2, 1)) + 1)
          NEXT p&
          END IF
          roflmofl$ = STRING$(wtoputbegin&*2 , 0) + buffer$ + STRING$(wtoput&*2 , 0)
          file2$ = file2$ + roflmofl$
    NEXT i&
    file2$ = temp3$ + file2$
    FOR i& = 1 TO htoput
        roflmofl$ = STRING$(128, 0)
          file2$ = file2$ + roflmofl$
    NEXT i&
    DIM byte1&
    DIM byte2&
    DIM bytes&

    DIM h&
    FOR i& = 0 TO 31
        oline$ = ""
       FOR h& = 0 TO 63
        roflmofl$ = MID$(file2$, ((i& - 1)*128 + (h-1)*2)+1, 2)
        byte1& = ASC(MID$(roflmofl$, 1, 1))
        byte2& = ASC(MID$(roflmofl$, 2, 1))
        bytes& = byte1& * 256 + byte2&
        IF priority$ =  "1" THEN BIT SET bytes&, 15

        IF palette$ = "1" THEN
        BIT SET bytes&, 13
        END IF
        IF palette$ = "2" THEN
        BIT SET bytes&, 14
        END IF
        IF palette$ = "3" THEN
        BIT SET bytes&, 14
        BIT SET bytes&, 13
        END IF
  '  ;    oline$ = oline$ + STRING$(4-LEN(HEX$(bytes&)),"0") + HEX$(bytes&)
    yourmom$ = CHR$(INT(bytes&/256)) + CHR$(INT(bytes& MOD 256))
        PUT #2, , yourmom$



        NEXT h&
      '  ;oline$ = "        hex " + oline$
     '   ;PRINT #2, oline$
    '    for i = 1 to len(oline$)
    NEXT i&
    IF COMMAND$= "" THEN
    PRINT "all done! press any key to exit"
    WHILE INKEY$ = ""
    WEND
    ELSE
    PRINT "Done."
    END IF
    CLOSE #1
    CLOSE #2

END FUNCTION
