Dotsarecool

KatAM Mapmaker Script

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