Search in sources :

Example 16 with Tile

use of io.anuke.mindustry.world.Tile in project Mindustry by Anuken.

the class Save12 method write.

@Override
public void write(DataOutputStream stream) throws IOException {
    // --META--
    // version id
    stream.writeInt(version);
    // last saved
    stream.writeLong(TimeUtils.millis());
    // --GENERAL STATE--
    // gamemode
    stream.writeByte(state.mode.ordinal());
    // map ID
    stream.writeByte(world.getMap().id);
    // wave
    stream.writeInt(state.wave);
    // wave countdown
    stream.writeFloat(state.wavetime);
    // player x/y
    stream.writeFloat(Vars.player.x);
    stream.writeFloat(Vars.player.y);
    // player health
    stream.writeInt(Vars.player.health);
    // amount of weapons
    stream.writeByte(control.upgrades().getWeapons().size - 1);
    // start at 1, because the first weapon is always the starter - ignore that
    for (int i = 1; i < control.upgrades().getWeapons().size; i++) {
        // weapon ordinal
        stream.writeByte(control.upgrades().getWeapons().get(i).id);
    }
    // --INVENTORY--
    int l = state.inventory.getItems().length;
    int itemsize = 0;
    for (int i = 0; i < l; i++) {
        if (state.inventory.getItems()[i] > 0) {
            itemsize++;
        }
    }
    // amount of items
    stream.writeByte(itemsize);
    for (int i = 0; i < l; i++) {
        if (state.inventory.getItems()[i] > 0) {
            // item ID
            stream.writeByte(i);
            // item amount
            stream.writeInt(state.inventory.getItems()[i]);
        }
    }
    // --ENEMIES--
    EntityContainer<Enemy> enemies = enemyGroup.all();
    // enemy amount
    stream.writeInt(enemies.size());
    for (int i = 0; i < enemies.size(); i++) {
        Enemy enemy = enemies.get(i);
        // type
        stream.writeByte(enemy.type.id);
        // lane
        stream.writeByte(enemy.lane);
        // x
        stream.writeFloat(enemy.x);
        // y
        stream.writeFloat(enemy.y);
        // tier
        stream.writeByte(enemy.tier);
        // health
        stream.writeInt(enemy.health);
    }
    // --MAP DATA--
    // seed
    stream.writeInt(world.getSeed());
    int totalblocks = 0;
    for (int x = 0; x < world.width(); x++) {
        for (int y = 0; y < world.height(); y++) {
            Tile tile = world.tile(x, y);
            if (tile.breakable()) {
                totalblocks++;
            }
        }
    }
    // tile amount
    stream.writeInt(totalblocks);
    for (int x = 0; x < world.width(); x++) {
        for (int y = 0; y < world.height(); y++) {
            Tile tile = world.tile(x, y);
            if (tile.breakable()) {
                // tile pos
                stream.writeInt(x + y * world.width());
                stream.writeByte(tile.link);
                // whether it has a tile entity
                stream.writeBoolean(tile.entity != null);
                // block ID
                stream.writeInt(tile.block().id);
                if (tile.entity != null) {
                    // rotation
                    stream.writeByte(tile.getRotation());
                    // health
                    stream.writeInt((int) tile.entity.health);
                    int amount = 0;
                    for (int i = 0; i < tile.entity.items.length; i++) {
                        if (tile.entity.items[i] > 0)
                            amount++;
                    }
                    // amount of items
                    stream.writeByte(amount);
                    for (int i = 0; i < tile.entity.items.length; i++) {
                        if (tile.entity.items[i] > 0) {
                            // item ID
                            stream.writeByte(i);
                            // item amount
                            stream.writeInt(tile.entity.items[i]);
                        }
                    }
                    tile.entity.write(stream);
                }
            }
        }
    }
}
Also used : Enemy(io.anuke.mindustry.entities.enemies.Enemy) Tile(io.anuke.mindustry.world.Tile)

Example 17 with Tile

use of io.anuke.mindustry.world.Tile in project Mindustry by Anuken.

the class Save13 method read.

@Override
public void read(DataInputStream stream) throws IOException {
    /*long loadTime = */
    stream.readLong();
    // general state
    byte mode = stream.readByte();
    byte mapid = stream.readByte();
    int wave = stream.readInt();
    float wavetime = stream.readFloat();
    float playerx = stream.readFloat();
    float playery = stream.readFloat();
    int playerhealth = stream.readInt();
    Vars.player.x = playerx;
    Vars.player.y = playery;
    Vars.player.health = playerhealth;
    state.mode = GameMode.values()[mode];
    Core.camera.position.set(playerx, playery, 0);
    // weapons
    control.upgrades().getWeapons().clear();
    control.upgrades().getWeapons().add(Weapon.blaster);
    Vars.player.weaponLeft = Vars.player.weaponRight = Weapon.blaster;
    int weapons = stream.readByte();
    for (int i = 0; i < weapons; i++) {
        control.upgrades().addWeapon((Weapon) Upgrade.getByID(stream.readByte()));
    }
    ui.hudfrag.updateWeapons();
    // inventory
    int totalItems = stream.readByte();
    Arrays.fill(state.inventory.getItems(), 0);
    for (int i = 0; i < totalItems; i++) {
        Item item = Item.getByID(stream.readByte());
        int amount = stream.readInt();
        state.inventory.getItems()[item.id] = amount;
    }
    ui.hudfrag.updateItems();
    // enemies
    Entities.clear();
    int enemies = stream.readInt();
    Array<Enemy> enemiesToUpdate = new Array<>();
    for (int i = 0; i < enemies; i++) {
        byte type = stream.readByte();
        int lane = stream.readByte();
        float x = stream.readFloat();
        float y = stream.readFloat();
        byte tier = stream.readByte();
        int health = stream.readShort();
        try {
            Enemy enemy = new Enemy(EnemyType.getByID(type));
            enemy.lane = lane;
            enemy.health = health;
            enemy.x = x;
            enemy.y = y;
            enemy.tier = tier;
            enemy.add(enemyGroup);
            enemiesToUpdate.add(enemy);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    state.enemies = enemies;
    state.wave = wave;
    state.wavetime = wavetime;
    if (!android)
        Vars.player.add();
    // map
    int seed = stream.readInt();
    world.loadMap(world.maps().getMap(mapid), seed);
    renderer.clearTiles();
    for (Enemy enemy : enemiesToUpdate) {
        enemy.node = -2;
    }
    int rocks = stream.readInt();
    for (int x = 0; x < world.width(); x++) {
        for (int y = 0; y < world.height(); y++) {
            Tile tile = world.tile(x, y);
            // remove breakables like rocks
            if (tile.breakable()) {
                world.tile(x, y).setBlock(Blocks.air);
            }
        }
    }
    for (int i = 0; i < rocks; i++) {
        int pos = stream.readInt();
        Tile tile = world.tile(pos % world.width(), pos / world.width());
        Block result = WorldGenerator.rocks.get(tile.floor());
        if (result != null)
            tile.setBlock(result);
    }
    int tiles = stream.readInt();
    for (int i = 0; i < tiles; i++) {
        int pos = stream.readInt();
        int blockid = stream.readInt();
        Tile tile = world.tile(pos % world.width(), pos / world.width());
        tile.setBlock(BlockLoader.getByOldID(blockid));
        if (blockid == Blocks.blockpart.id) {
            tile.link = stream.readByte();
        }
        if (tile.entity != null) {
            byte rotation = stream.readByte();
            short health = stream.readShort();
            int items = stream.readByte();
            tile.entity.health = health;
            tile.setRotation(rotation);
            for (int j = 0; j < items; j++) {
                int itemid = stream.readByte();
                int itemamount = stream.readInt();
                tile.entity.items[itemid] = itemamount;
            }
            tile.entity.read(stream);
        }
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) Item(io.anuke.mindustry.resource.Item) Enemy(io.anuke.mindustry.entities.enemies.Enemy) Tile(io.anuke.mindustry.world.Tile) Block(io.anuke.mindustry.world.Block) IOException(java.io.IOException)

Example 18 with Tile

use of io.anuke.mindustry.world.Tile in project Mindustry by Anuken.

the class Save14 method read.

@Override
public void read(DataInputStream stream) throws IOException {
    /*long loadTime = */
    stream.readLong();
    // general state
    byte mode = stream.readByte();
    byte mapid = stream.readByte();
    int wave = stream.readInt();
    float wavetime = stream.readFloat();
    // block header
    int blocksize = stream.readInt();
    IntMap<Block> map = new IntMap<>();
    for (int i = 0; i < blocksize; i++) {
        String name = readString(stream);
        int id = stream.readShort();
        map.put(id, Block.getByName(name));
    }
    float playerx = stream.readFloat();
    float playery = stream.readFloat();
    int playerhealth = stream.readInt();
    Vars.player.x = playerx;
    Vars.player.y = playery;
    Vars.player.health = playerhealth;
    state.mode = GameMode.values()[mode];
    Core.camera.position.set(playerx, playery, 0);
    // weapons
    control.upgrades().getWeapons().clear();
    control.upgrades().getWeapons().add(Weapon.blaster);
    Vars.player.weaponLeft = Vars.player.weaponRight = Weapon.blaster;
    int weapons = stream.readByte();
    for (int i = 0; i < weapons; i++) {
        control.upgrades().addWeapon((Weapon) Upgrade.getByID(stream.readByte()));
    }
    ui.hudfrag.updateWeapons();
    // inventory
    int totalItems = stream.readByte();
    Arrays.fill(state.inventory.getItems(), 0);
    for (int i = 0; i < totalItems; i++) {
        Item item = Item.getByID(stream.readByte());
        int amount = stream.readInt();
        state.inventory.getItems()[item.id] = amount;
    }
    ui.hudfrag.updateItems();
    // enemies
    Entities.clear();
    int enemies = stream.readInt();
    Array<Enemy> enemiesToUpdate = new Array<>();
    for (int i = 0; i < enemies; i++) {
        byte type = stream.readByte();
        int lane = stream.readByte();
        float x = stream.readFloat();
        float y = stream.readFloat();
        byte tier = stream.readByte();
        int health = stream.readShort();
        try {
            Enemy enemy = new Enemy(EnemyType.getByID(type));
            enemy.lane = lane;
            enemy.health = health;
            enemy.x = x;
            enemy.y = y;
            enemy.tier = tier;
            enemy.add(enemyGroup);
            enemiesToUpdate.add(enemy);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    state.enemies = enemies;
    state.wave = wave;
    state.wavetime = wavetime;
    if (!android)
        Vars.player.add();
    // map
    int seed = stream.readInt();
    world.loadMap(world.maps().getMap(mapid), seed);
    renderer.clearTiles();
    for (Enemy enemy : enemiesToUpdate) {
        enemy.node = -2;
    }
    int rocks = stream.readInt();
    for (int x = 0; x < world.width(); x++) {
        for (int y = 0; y < world.height(); y++) {
            Tile tile = world.tile(x, y);
            // remove breakables like rocks
            if (tile.breakable()) {
                world.tile(x, y).setBlock(Blocks.air);
            }
        }
    }
    for (int i = 0; i < rocks; i++) {
        int pos = stream.readInt();
        Tile tile = world.tile(pos % world.width(), pos / world.width());
        if (tile == null)
            continue;
        Block result = WorldGenerator.rocks.get(tile.floor());
        if (result != null)
            tile.setBlock(result);
    }
    int tiles = stream.readInt();
    for (int i = 0; i < tiles; i++) {
        int pos = stream.readInt();
        int blockid = stream.readInt();
        Tile tile = world.tile(pos % world.width(), pos / world.width());
        tile.setBlock(map.get(blockid));
        if (blockid == Blocks.blockpart.id) {
            tile.link = stream.readByte();
        }
        if (tile.entity != null) {
            byte rotation = stream.readByte();
            short health = stream.readShort();
            int items = stream.readByte();
            tile.entity.health = health;
            tile.setRotation(rotation);
            for (int j = 0; j < items; j++) {
                int itemid = stream.readByte();
                int itemamount = stream.readInt();
                tile.entity.items[itemid] = itemamount;
            }
            tile.entity.read(stream);
        }
    }
}
Also used : Tile(io.anuke.mindustry.world.Tile) IOException(java.io.IOException) Array(com.badlogic.gdx.utils.Array) Item(io.anuke.mindustry.resource.Item) Enemy(io.anuke.mindustry.entities.enemies.Enemy) IntMap(com.badlogic.gdx.utils.IntMap) Block(io.anuke.mindustry.world.Block)

Example 19 with Tile

use of io.anuke.mindustry.world.Tile in project Mindustry by Anuken.

the class Save15 method read.

@Override
public void read(DataInputStream stream) throws IOException {
    /*long loadTime = */
    stream.readLong();
    // general state
    byte mode = stream.readByte();
    byte mapid = stream.readByte();
    int wave = stream.readInt();
    float wavetime = stream.readFloat();
    byte difficulty = stream.readByte();
    state.difficulty = Difficulty.values()[difficulty];
    // block header
    int blocksize = stream.readInt();
    IntMap<Block> map = new IntMap<>();
    for (int i = 0; i < blocksize; i++) {
        String name = readString(stream);
        int id = stream.readShort();
        map.put(id, Block.getByName(name));
    }
    float playerx = stream.readFloat();
    float playery = stream.readFloat();
    int playerhealth = stream.readInt();
    if (!headless) {
        player.x = playerx;
        player.y = playery;
        player.health = playerhealth;
        state.mode = GameMode.values()[mode];
        Core.camera.position.set(playerx, playery, 0);
        // weapons
        control.upgrades().getWeapons().clear();
        control.upgrades().getWeapons().add(Weapon.blaster);
        player.weaponLeft = player.weaponRight = Weapon.blaster;
        int weapons = stream.readByte();
        for (int i = 0; i < weapons; i++) {
            control.upgrades().addWeapon((Weapon) Upgrade.getByID(stream.readByte()));
        }
        ui.hudfrag.updateWeapons();
    } else {
        byte b = stream.readByte();
        for (int i = 0; i < b; i++) stream.readByte();
    }
    // inventory
    int totalItems = stream.readByte();
    Arrays.fill(state.inventory.getItems(), 0);
    for (int i = 0; i < totalItems; i++) {
        Item item = Item.getByID(stream.readByte());
        int amount = stream.readInt();
        state.inventory.getItems()[item.id] = amount;
    }
    if (!headless)
        ui.hudfrag.updateItems();
    // enemies
    int enemies = stream.readInt();
    Array<Enemy> enemiesToUpdate = new Array<>();
    for (int i = 0; i < enemies; i++) {
        byte type = stream.readByte();
        int lane = stream.readByte();
        float x = stream.readFloat();
        float y = stream.readFloat();
        byte tier = stream.readByte();
        int health = stream.readShort();
        try {
            Enemy enemy = new Enemy(EnemyType.getByID(type));
            enemy.lane = lane;
            enemy.health = health;
            enemy.x = x;
            enemy.y = y;
            enemy.tier = tier;
            enemy.add(enemyGroup);
            enemiesToUpdate.add(enemy);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    state.enemies = enemies;
    state.wave = wave;
    state.wavetime = wavetime;
    if (!android && !headless)
        player.add();
    // map
    int seed = stream.readInt();
    world.loadMap(world.maps().getMap(mapid), seed);
    if (!headless)
        renderer.clearTiles();
    for (Enemy enemy : enemiesToUpdate) {
        enemy.node = -2;
    }
    int rocks = stream.readInt();
    for (int x = 0; x < world.width(); x++) {
        for (int y = 0; y < world.height(); y++) {
            Tile tile = world.tile(x, y);
            // remove breakables like rocks
            if (tile.breakable()) {
                world.tile(x, y).setBlock(Blocks.air);
            }
        }
    }
    for (int i = 0; i < rocks; i++) {
        int pos = stream.readInt();
        Tile tile = world.tile(pos % world.width(), pos / world.width());
        if (tile == null)
            continue;
        Block result = WorldGenerator.rocks.get(tile.floor());
        if (result != null)
            tile.setBlock(result);
    }
    int tiles = stream.readInt();
    for (int i = 0; i < tiles; i++) {
        int pos = stream.readInt();
        int blockid = stream.readInt();
        Tile tile = world.tile(pos % world.width(), pos / world.width());
        tile.setBlock(map.get(blockid));
        if (blockid == Blocks.blockpart.id) {
            tile.link = stream.readByte();
        }
        if (tile.entity != null) {
            byte rotation = stream.readByte();
            short health = stream.readShort();
            int items = stream.readByte();
            tile.entity.health = health;
            tile.setRotation(rotation);
            for (int j = 0; j < items; j++) {
                int itemid = stream.readByte();
                int itemamount = stream.readInt();
                tile.entity.items[itemid] = itemamount;
            }
            tile.entity.read(stream);
        }
    }
}
Also used : Tile(io.anuke.mindustry.world.Tile) IOException(java.io.IOException) Array(com.badlogic.gdx.utils.Array) Item(io.anuke.mindustry.resource.Item) Enemy(io.anuke.mindustry.entities.enemies.Enemy) IntMap(com.badlogic.gdx.utils.IntMap) Block(io.anuke.mindustry.world.Block)

Example 20 with Tile

use of io.anuke.mindustry.world.Tile in project Mindustry by Anuken.

the class Bullet method update.

@Override
public void update() {
    super.update();
    if (collidesTiles()) {
        world.raycastEach(world.toTile(lastX), world.toTile(lastY), world.toTile(x), world.toTile(y), (x, y) -> {
            Tile tile = world.tile(x, y);
            if (tile == null)
                return false;
            tile = tile.target();
            if (tile.entity != null && tile.entity.collide(this) && !tile.entity.dead) {
                tile.entity.collision(this);
                remove();
                type.hit(this);
                return true;
            }
            return false;
        });
    }
}
Also used : Tile(io.anuke.mindustry.world.Tile)

Aggregations

Tile (io.anuke.mindustry.world.Tile)45 Enemy (io.anuke.mindustry.entities.enemies.Enemy)10 Item (io.anuke.mindustry.resource.Item)7 SpawnPoint (io.anuke.mindustry.game.SpawnPoint)6 Block (io.anuke.mindustry.world.Block)6 Vector2 (com.badlogic.gdx.math.Vector2)5 IOException (java.io.IOException)5 Array (com.badlogic.gdx.utils.Array)4 Player (io.anuke.mindustry.entities.Player)3 BlockPart (io.anuke.mindustry.world.blocks.types.BlockPart)3 PowerAcceptor (io.anuke.mindustry.world.blocks.types.PowerAcceptor)3 Rock (io.anuke.mindustry.world.blocks.types.Rock)3 IntMap (com.badlogic.gdx.utils.IntMap)2 LiquidBlock (io.anuke.mindustry.world.blocks.types.LiquidBlock)2 StaticBlock (io.anuke.mindustry.world.blocks.types.StaticBlock)2 GridPoint2 (com.badlogic.gdx.math.GridPoint2)1 Rectangle (com.badlogic.gdx.math.Rectangle)1 Vars (io.anuke.mindustry.Vars)1 TileEntity (io.anuke.mindustry.entities.TileEntity)1 Difficulty (io.anuke.mindustry.game.Difficulty)1