Search in sources :

Example 21 with Tile

use of objects.Tile in project ultimate-java by pantinor.

the class Utils method setMapTiles.

/**
 * load the tile indexes from the ULT file
 */
public static void setMapTiles(BaseMap map, TileSet ts) throws Exception {
    String fname = map.getFname().toLowerCase();
    if (fname == null || fname.isEmpty() || fname.endsWith(".tmx")) {
        return;
    }
    InputStream is = new FileInputStream("assets/data/" + fname);
    byte[] bytes = IOUtils.toByteArray(is);
    Tile[] tiles = new Tile[map.getWidth() * map.getHeight()];
    if (map.getType() == MapType.world || map.getType() == MapType.city) {
        int pos = 0;
        for (int ych = 0; ych < map.getHeight() / 32; ych++) {
            for (int xch = 0; xch < map.getWidth() / 32; xch++) {
                for (int y = 0; y < 32; y++) {
                    for (int x = 0; x < 32; x++) {
                        int index = bytes[pos] & 0xff;
                        pos++;
                        Tile tile = ts.getTileByIndex(index);
                        if (tile == null) {
                            System.out.println("Tile index cannot be found: " + index + " using index 127 for black space.");
                            tile = ts.getTileByIndex(127);
                        }
                        tiles[x + (y * map.getWidth()) + (xch * 32) + (ych * 32 * map.getWidth())] = tile;
                    }
                }
            }
        }
    } else if (map.getType() == MapType.combat) {
        int pos = 0x40;
        for (int y = 0; y < map.getHeight(); y++) {
            for (int x = 0; x < map.getWidth(); x++) {
                int index = bytes[pos] & 0xff;
                pos++;
                Tile tile = ts.getTileByIndex(index);
                if (tile == null) {
                    System.out.println("Tile index cannot be found: " + index + " using index 127 for black space.");
                    tile = ts.getTileByIndex(127);
                }
                tiles[x + y * map.getWidth()] = tile;
            }
        }
    } else if (map.getType() == MapType.shrine) {
        int pos = 0;
        for (int y = 0; y < map.getHeight(); y++) {
            for (int x = 0; x < map.getWidth(); x++) {
                int index = bytes[pos] & 0xff;
                pos++;
                Tile tile = ts.getTileByIndex(index);
                if (tile == null) {
                    System.out.println("Tile index cannot be found: " + index + " using index 127 for black space.");
                    tile = ts.getTileByIndex(127);
                }
                if (tile.getIndex() == 31) {
                    // avatar position
                    tile = ts.getTileByIndex(4);
                }
                tiles[x + y * map.getWidth()] = tile;
            }
        }
    }
    map.setTiles(tiles);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DungeonTile(ultima.Constants.DungeonTile) Tile(objects.Tile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) FileInputStream(java.io.FileInputStream)

Example 22 with Tile

use of objects.Tile in project ultimate-java by pantinor.

the class Utils method peerGem.

// used for view gem on the world map only
public static Texture peerGem(BaseMap worldMap, int avatarX, int avatarY, TextureAtlas atlas) throws Exception {
    FileTextureData d = (FileTextureData) (atlas.getRegions().first().getTexture().getTextureData());
    BufferedImage sheet = ImageIO.read(d.getFileHandle().file());
    BufferedImage canvas = new BufferedImage(32 * 64, 32 * 64, BufferedImage.TYPE_INT_ARGB);
    int startX = avatarX - 32;
    int startY = avatarY - 32;
    int endX = avatarX + 32;
    int endY = avatarY + 32;
    int indexX = 0;
    int indexY = 0;
    for (int y = startY; y < endY; y++) {
        for (int x = startX; x < endX; x++) {
            int cx = x;
            if (x < 0) {
                cx = 256 + x;
            } else if (x >= 256) {
                cx = x - 256;
            }
            int cy = y;
            if (y < 0) {
                cy = 256 + y;
            } else if (y >= 256) {
                cy = y - 256;
            }
            Tile ct = worldMap.getTile(cx, cy);
            AtlasRegion ar = (AtlasRegion) atlas.findRegion(ct.getName());
            BufferedImage sub = sheet.getSubimage(ar.getRegionX(), ar.getRegionY(), 32, 32);
            canvas.getGraphics().drawImage(sub, indexX * 32, indexY * 32, 32, 32, null);
            Creature cr = worldMap.getCreatureAt(cx, cy);
            if (cr != null) {
                canvas.getGraphics().fillRect(indexX * 32, indexY * 32, 32, 32);
            }
            indexX++;
        }
        indexX = 0;
        indexY++;
    }
    // add avatar in the middle
    canvas.getGraphics().fillRect((32 * 64) / 2, (32 * 64) / 2, 32, 32);
    java.awt.Image tmp = canvas.getScaledInstance(20 * 32, 20 * 32, Image.SCALE_AREA_AVERAGING);
    BufferedImage scaledCanvas = new BufferedImage(20 * 32, 20 * 32, BufferedImage.TYPE_INT_ARGB);
    scaledCanvas.getGraphics().drawImage(tmp, 0, 0, null);
    Pixmap p = createPixmap(Ultima4.SCREEN_WIDTH, Ultima4.SCREEN_HEIGHT, scaledCanvas, (Ultima4.SCREEN_WIDTH - scaledCanvas.getWidth()) / 2, (Ultima4.SCREEN_HEIGHT - scaledCanvas.getHeight()) / 2);
    Texture t = new Texture(p);
    p.dispose();
    return t;
}
Also used : Creature(objects.Creature) Image(java.awt.Image) DungeonTile(ultima.Constants.DungeonTile) Tile(objects.Tile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) FileTextureData(com.badlogic.gdx.graphics.glutils.FileTextureData) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion) Texture(com.badlogic.gdx.graphics.Texture) BufferedImage(java.awt.image.BufferedImage) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 23 with Tile

use of objects.Tile in project ultimate-java by pantinor.

the class Utils method setTMXMap.

public static void setTMXMap(BaseMap map, Maps id, String tmxFile, TileSet ts) {
    List<Person> people = new ArrayList<>();
    Tile[] tiles = new Tile[map.getWidth() * map.getHeight()];
    TmxMapLoader loader = new TmxMapLoader();
    TiledMap tm = loader.load("assets/tilemaps/" + tmxFile);
    TiledMapTileLayer ml = (TiledMapTileLayer) tm.getLayers().get(map.getId() + "-map");
    if (ml != null) {
        FileHandle f = new FileHandle("assets/tilemaps/latest-atlas.txt");
        TextureAtlas.TextureAtlasData atlas = new TextureAtlas.TextureAtlasData(f, f.parent(), false);
        int png_grid_width = 24;
        Tile[] mapTileIds = new Tile[png_grid_width * Constants.tilePixelWidth + 1];
        for (Region r : atlas.getRegions()) {
            int x = r.left / r.width;
            int y = r.top / r.height;
            int i = x + (y * png_grid_width) + 1;
            mapTileIds[i] = ts.getTileByName(r.name);
        }
        for (int y = 0; y < map.getHeight(); y++) {
            for (int x = 0; x < map.getWidth(); x++) {
                StaticTiledMapTile tr = (StaticTiledMapTile) ml.getCell(x, map.getWidth() - 1 - y).getTile();
                Tile tile = mapTileIds[tr.getId()];
                if (tile == null) {
                    tile = ts.getTileByIndex(127);
                }
                tiles[x + (y * map.getWidth())] = tile;
            }
        }
    }
    MapLayer objectsLayer = tm.getLayers().get(map.getId() + "-people");
    if (objectsLayer != null) {
        Iterator<MapObject> iter = objectsLayer.getObjects().iterator();
        while (iter.hasNext()) {
            MapObject obj = iter.next();
            Person p = new Person();
            Conversation conv = new Conversation();
            conv.setName(obj.getName());
            conv.setMap(id);
            p.setConversation(conv);
            people.add(p);
            Iterator<String> keys = obj.getProperties().getKeys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = obj.getProperties().get(key).toString();
                if (key.equals("tileType")) {
                    Tile tile = ts.getTileByName(value);
                    p.setTileIndex(tile.getIndex());
                    p.setTile(tile);
                } else if (key.equals("startX")) {
                    p.setStart_x(new Float(value).intValue());
                } else if (key.equals("startY")) {
                    p.setStart_y(new Float(value).intValue());
                } else if (key.equals("role")) {
                    PersonRole role = new PersonRole();
                    role.setInventoryType(InventoryType.valueOf(value));
                    p.setRole(role);
                } else if (key.equals("movement")) {
                    p.setMovement(ObjectMovementBehavior.valueOf(value));
                } else if (key.startsWith("description")) {
                    conv.setDescription(value);
                    conv.getTopics().add(conv.new Topic(standardQuery[2], value, null, null, null));
                } else if (key.startsWith("topic")) {
                    String[] splits = value.split(",");
                    if (splits.length == 5) {
                        conv.getTopics().add(conv.new Topic(splits[0], splits[1], splits[2], splits[3], splits[4]));
                    } else {
                        conv.getTopics().add(conv.new Topic(splits[0], splits[1], null, null, null));
                    }
                }
            }
        }
    }
    map.getCity().setPeople(people);
    map.setTiles(tiles);
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) MapLayer(com.badlogic.gdx.maps.MapLayer) ArrayList(java.util.ArrayList) Conversation(objects.Conversation) HawkwindConversation(objects.HawkwindConversation) LordBritishConversation(objects.LordBritishConversation) PersonRole(objects.PersonRole) TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) MapObject(com.badlogic.gdx.maps.MapObject) TmxMapLoader(com.badlogic.gdx.maps.tiled.TmxMapLoader) DungeonTile(ultima.Constants.DungeonTile) Tile(objects.Tile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) TiledMap(com.badlogic.gdx.maps.tiled.TiledMap) Person(objects.Person)

Example 24 with Tile

use of objects.Tile in project ultimate-java by pantinor.

the class Utils method getPeople.

/**
 * Read the ULT file and parse the people
 *
 * @param fname
 * @return
 */
public static List<Person> getPeople(String fname, Maps map, TileSet ts) {
    byte[] bytes;
    try {
        InputStream is = new FileInputStream("assets/data/" + fname);
        bytes = IOUtils.toByteArray(is);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Person[] people = new Person[32];
    int MAX_PEOPLE = 32;
    int MAP_WIDTH = 32;
    int startOffset = MAP_WIDTH * MAP_WIDTH;
    int end = startOffset + MAX_PEOPLE;
    int count = 0;
    for (int i = startOffset; i < end; i++) {
        int index = bytes[i] & 0xff;
        if (index == 0) {
            count++;
            continue;
        }
        Person p = new Person();
        p.setId(count);
        p.setTileIndex(index);
        if (ts != null) {
            Tile t = ts.getTileByIndex(index);
            if (t == null) {
                System.err.printf("tile index %s could not be found.\n", index);
            }
            p.setTile(t);
        }
        people[count] = p;
        count++;
    }
    startOffset = MAP_WIDTH * MAP_WIDTH + MAX_PEOPLE * 1;
    end = startOffset + MAX_PEOPLE;
    count = 0;
    for (int i = startOffset; i < end; i++) {
        int start_x = bytes[i] & 0xff;
        Person p = people[count];
        if (p == null) {
            count++;
            continue;
        }
        p.setStart_x(start_x);
        count++;
    }
    startOffset = MAP_WIDTH * MAP_WIDTH + MAX_PEOPLE * 2;
    end = startOffset + MAX_PEOPLE;
    count = 0;
    for (int i = startOffset; i < end; i++) {
        int start_y = bytes[i] & 0xff;
        Person p = people[count];
        if (p == null) {
            count++;
            continue;
        }
        p.setStart_y(start_y);
        count++;
    }
    startOffset = MAP_WIDTH * MAP_WIDTH + MAX_PEOPLE * 6;
    end = startOffset + MAX_PEOPLE;
    count = 0;
    for (int i = startOffset; i < end; i++) {
        int m = bytes[i] & 0xff;
        Person p = people[count];
        if (p == null) {
            count++;
            continue;
        }
        if (m == 0) {
            p.setMovement(ObjectMovementBehavior.FIXED);
        } else if (m == 1) {
            p.setMovement(ObjectMovementBehavior.WANDER);
        } else if (m == 0x80) {
            p.setMovement(ObjectMovementBehavior.FOLLOW_AVATAR);
        } else if (m == 0xFF) {
            p.setMovement(ObjectMovementBehavior.ATTACK_AVATAR);
        }
        count++;
    }
    startOffset = MAP_WIDTH * MAP_WIDTH + MAX_PEOPLE * 7;
    end = startOffset + MAX_PEOPLE;
    count = 0;
    for (int i = startOffset; i < end; i++) {
        int id = bytes[i] & 0xff;
        Person p = people[count];
        if (p == null) {
            count++;
            continue;
        }
        p.setDialogId(id);
        count++;
    }
    List<Person> peeps = new ArrayList<>();
    for (int i = 0; i < people.length; i++) {
        if (people[i] != null) {
            peeps.add(people[i]);
            if (map == Maps.CASTLE_OF_LORD_BRITISH_2 && people[i].getId() == 31) {
                people[i].setConversation(new LordBritishConversation());
            }
            if (map == Maps.CASTLE_OF_LORD_BRITISH_1 && people[i].getId() == 29) {
                people[i].setConversation(new HawkwindConversation());
            }
            if (map == Maps.SKARABRAE && people[i].getId() == 10) {
                // isaac
                people[i].setRemovedFromMap(true);
            }
        }
    }
    return peeps;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) LordBritishConversation(objects.LordBritishConversation) ArrayList(java.util.ArrayList) DungeonTile(ultima.Constants.DungeonTile) Tile(objects.Tile) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) HawkwindConversation(objects.HawkwindConversation) Person(objects.Person) FileInputStream(java.io.FileInputStream)

Example 25 with Tile

use of objects.Tile in project ultimate-java by pantinor.

the class AndiusMapTmxConvert method create.

@Override
public void create() {
    try {
        File file2 = new File("assets/xml/tileset-base.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(TileSet.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        TileSet ts = (TileSet) jaxbUnmarshaller.unmarshal(file2);
        ts.setMaps();
        File file3 = new File("assets/xml/maps.xml");
        jaxbContext = JAXBContext.newInstance(MapSet.class);
        jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        MapSet ms = (MapSet) jaxbUnmarshaller.unmarshal(file3);
        ms.init(ts);
        // load the atlas and determine the tile indexes per tilemap position
        FileHandle f = new FileHandle("assets/tilemaps/latest-atlas.txt");
        TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
        int png_grid_width = 24;
        Tile[] mapTileIds = new Tile[png_grid_width * Constants.tilePixelWidth + 1];
        for (Region r : atlas.getRegions()) {
            int x = r.left / r.width;
            int y = r.top / r.height;
            int i = x + (y * png_grid_width) + 1;
            mapTileIds[i] = ts.getTileByName(r.name);
        }
        BaseMap world = Constants.Maps.WORLD.getMap();
        Utils.setMapTiles(world, ts);
        Tile[] tiles = world.getTiles();
        String tmxFName = "tmx/andius/world.tmx";
        WorldFormatter c = new WorldFormatter(world.getWidth(), world.getHeight(), tiles, world.getPortals(), world.getMoongates());
        FileUtils.writeStringToFile(new File(tmxFName), c.toString());
        System.out.printf("Wrote: %s\n", tmxFName);
        for (BaseMap map : ms.getMaps()) {
            if (!map.getFname().endsWith("ult")) {
                continue;
            }
            FileInputStream is = new FileInputStream("assets/data/" + map.getFname());
            byte[] bytes = IOUtils.toByteArray(is);
            tiles = new Tile[map.getWidth() * map.getHeight()];
            int pos = 0;
            for (int y = 0; y < map.getHeight(); y++) {
                for (int x = 0; x < map.getWidth(); x++) {
                    // convert a byte to an unsigned int value
                    int index = bytes[pos] & 0xff;
                    pos++;
                    Tile tile = ts.getTileByIndex(index);
                    if (tile == null) {
                        System.out.println("Tile index cannot be found: " + index + " using index 129 for black space.");
                        tile = ts.getTileByIndex(129);
                    }
                    tiles[x + y * map.getWidth()] = tile;
                }
            }
            tmxFName = String.format("tmx/andius/%s.tmx", map.getCity().getName().replace(" ", "").toLowerCase());
            if (map.getFname().equals("lcb_2.ult")) {
                tmxFName = "tmx/andius/britannia2.tmx";
            }
            Formatter fmtter = new Formatter(map.getWidth(), map.getHeight(), tiles);
            FileUtils.writeStringToFile(new File(tmxFName), fmtter.toString());
            System.out.printf("Wrote: %s\n", tmxFName);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("DONE");
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) Tile(objects.Tile) JAXBContext(javax.xml.bind.JAXBContext) TileSet(objects.TileSet) BaseMap(objects.BaseMap) FileInputStream(java.io.FileInputStream) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) MapSet(objects.MapSet) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Aggregations

Tile (objects.Tile)39 StaticTiledMapTile (com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile)22 TiledMapTile (com.badlogic.gdx.maps.tiled.TiledMapTile)16 Creature (objects.Creature)12 Drawable (objects.Drawable)10 File (java.io.File)8 JAXBContext (javax.xml.bind.JAXBContext)8 DungeonTile (ultima.Constants.DungeonTile)8 Unmarshaller (javax.xml.bind.Unmarshaller)7 BaseMap (objects.BaseMap)7 TileSet (objects.TileSet)7 FileHandle (com.badlogic.gdx.files.FileHandle)6 Region (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region)6 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)5 TiledMap (com.badlogic.gdx.maps.tiled.TiledMap)5 Vector3 (com.badlogic.gdx.math.Vector3)5 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 MapSet (objects.MapSet)5 TiledMapTileLayer (com.badlogic.gdx.maps.tiled.TiledMapTileLayer)4