Advertisement
Dotsarecool

KatAM Map Maker

Apr 12th, 2019
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.FileFilter;
  6. import java.io.FileInputStream;
  7. import java.io.FileWriter;
  8. import java.io.PrintWriter;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12.  
  13.  
  14. public class KatAMMap {
  15.     public static String path = "C:\\Users\\Alex\\Desktop\\KatAM\\";
  16.     public static String gba = "C:\\Users\\Alex\\Desktop\\katam.gba";
  17.     public static int ROOM_PROP_TABLE = 0x903338;
  18.     public static int OBJ_LIST_PTR_TABLE = 0xD2EBC0;
  19.     public static byte[] rom;
  20.    
  21.     public static boolean drawbackground = true;
  22.    
  23.     public static void main(String [] args) {
  24.         try (PrintWriter out = new PrintWriter(new FileWriter(new File("out.log")))) {
  25.             try (FileInputStream in = new FileInputStream(gba)) {
  26.                 rom = new byte[(int)in.getChannel().size()];
  27.                 in.read(rom, 0, rom.length);
  28.             } catch (Exception e) {
  29.                 e.printStackTrace();
  30.             }
  31.            
  32.             File objectImages = new File(path + "_OBJ\\");
  33.             File[] objImgs = objectImages.listFiles();
  34.             File root = new File(path);
  35.             File[] levels = root.listFiles(new FileFilter() {
  36.                 @Override
  37.                 public boolean accept(File file) {
  38.                     return file.isDirectory() && (file.getName().contains("level-912") || file.getName().contains("level-916"));
  39.                 }
  40.             });
  41.            
  42.             for (File lvl : levels) {
  43.                 if (lvl.getName().equals("_OBJ")) {
  44.                     continue;
  45.                 }
  46.                 int lvlNo = Integer.parseInt(lvl.getName().substring(lvl.getName().indexOf("-")+1,lvl.getName().indexOf("_")));
  47.                 int ptrIdx = get16(ROOM_PROP_TABLE + 0x28 * lvlNo + 0x20)+1;
  48.                 int objPtr = get24(OBJ_LIST_PTR_TABLE + 0x4 * ptrIdx);
  49.                
  50.                 File [] imgs = lvl.listFiles();
  51.                 int [][] pos = new int[imgs.length][2];
  52.                
  53.                 String foldername = lvl.getName();
  54.                 int w = Integer.parseInt(foldername.substring(foldername.indexOf("w-")+2, foldername.indexOf("_h")));
  55.                 int h = Integer.parseInt(foldername.substring(foldername.indexOf("h-")+2)) - 32;
  56.                 File background = null;
  57.                
  58.                 for (int i = 0; i < imgs.length; i++) {
  59.                     String filename = imgs[i].getName();
  60.                     if (filename.equals("b.png")) {
  61.                         background = imgs[i];
  62.                         pos[i][1] = -100000;
  63.                     } else {
  64.                         pos[i][1] = Integer.parseInt(filename.substring(2,filename.indexOf("_")));
  65.                         pos[i][0] = Integer.parseInt(filename.substring(filename.indexOf("x-")+2, filename.indexOf(".png")));
  66.                     }
  67.                 }
  68.                
  69.                 BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  70.                 Graphics2D g = (Graphics2D)bi.getGraphics();
  71.                 g.setBackground(Color.red);
  72.                 g.setColor(Color.black);
  73.                
  74.                 BufferedImage bg = null;
  75.                 if (background != null) {
  76.                     bg = ImageIO.read(background);
  77.                 }
  78.                 for (int i = 0; i < imgs.length; i++) {
  79.                     BufferedImage image = ImageIO.read(imgs[i]);
  80.                     BufferedImage imageWAlpha = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
  81.                     ((Graphics2D)(imageWAlpha.getGraphics())).drawImage(image, 0, 0, null);
  82.                     for (int y = 0; y < imageWAlpha.getHeight(); ++y) {
  83.                         for (int x = 0; x < imageWAlpha.getWidth(); ++x) {
  84.                              int argb = imageWAlpha.getRGB(x, y);
  85.                              if ((argb & 0x00FFFFFF) == 0x00FF00FF)
  86.                              {
  87.                                  imageWAlpha.setRGB(x, y, 0);
  88.                              }
  89.                         }
  90.                     }
  91.                     if (drawbackground && bg != null) {
  92.                         g.drawImage(bg, pos[i][0], pos[i][1], null);
  93.                     }
  94.                     g.drawImage(imageWAlpha, pos[i][0], pos[i][1], null);
  95.                 }
  96.                
  97.                 out.printf("%x%n", objPtr);
  98.                
  99.                 int m = get16(objPtr + 0x10);
  100.                 int o = 0;
  101.                 while (m == 0x2401) {
  102.                     int ox = get16(objPtr + o * 36 + 0x10 + 6);
  103.                     int oy = get16(objPtr + o * 36 + 0x10 + 8);
  104.                     int ot = get8(objPtr + o * 36 + 0x10 + 0xC);
  105.                     int oa = get8(objPtr + o * 36 + 0x10 + 0xE);
  106.  
  107.                     File oImg = null;
  108.                     for (File f : objImgs) {
  109.                         if (f.getName().indexOf(ot + "_") == 0) {
  110.                             if (
  111.                                     ot == 0x92 || ot == 0x93 || ot == 0x94 || ot == 0x95 || // ability trophy
  112.                                     ot == 0x97 || // fancy door
  113.                                     ot == 0x00 || // waddle dee
  114.                                     ot == 0x2D // batty
  115.                                 ) {
  116.                                 if (f.getName().indexOf("(" + oa + ")") != -1) {
  117.                                     oImg = f;
  118.                                     break;
  119.                                 }
  120.                             } else {
  121.                                 oImg = f;
  122.                                 break;
  123.                             }
  124.                         }
  125.                     }
  126.                     if (oImg == null) {
  127.                         g.clearRect(ox, oy, 16, 16);
  128.                         out.printf("Sprite %x (level %d) - ", ot, lvlNo);
  129.                         out.println();
  130.                     } else {
  131.                         BufferedImage objBi = ImageIO.read(oImg);
  132.                         int xOffset = Integer.parseInt(oImg.getName().substring(oImg.getName().indexOf("[")+1,oImg.getName().indexOf(",")));
  133.                         int yOffset = Integer.parseInt(oImg.getName().substring(oImg.getName().indexOf(",")+1,oImg.getName().indexOf("]")));
  134.                         g.drawImage(objBi, ox + xOffset, oy + yOffset, null);
  135.                     }
  136.  
  137.                     for (int i = 0; i < 36; i+=2) {
  138.                         out.printf("%4x|", get16(objPtr + o * 36 + 0x10 + i));
  139.                     }
  140.                     out.println();
  141.                     //g.drawString(String.format("T: %x", ot), ox, oy);
  142.  
  143.                     o++;
  144.                     m = get16(objPtr + o * 36 + 0x10);
  145.                 }
  146.                
  147.                 String newname = lvl.toString().substring(0,lvl.toString().indexOf("_w"));
  148.                 ImageIO.write(bi, "png", new File(newname + ".png"));
  149.             }
  150.         } catch (Exception e) {
  151.             e.printStackTrace();
  152.         }
  153.     }
  154.    
  155.     public static int get8(int i) {
  156.         int a = 0xff&rom[i];
  157.         return a;
  158.     }
  159.    
  160.     public static int get16(int i) {
  161.         int a = 0xff&rom[i], b = 0xff&rom[i + 1];
  162.         return b * 0x100 + a;
  163.     }
  164.    
  165.     public static int get24(int i) {
  166.         int a = 0xff&rom[i], b = 0xff&rom[i + 1], c = 0xff&rom[i + 2];
  167.         return c * 0x10000 + b * 0x100 + a;
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement