Jump to content

[Open Source] Castle Wars


Alek

Recommended Posts

IcAc4zP.png

 

Haven't seen a Castle Wars script on either here or a competitor bot. This is something I wrote up real quick; you could just AFK outside the spawn room but I added some basic logic to prevent some reports.

 

Features:

-Waits for game

-Grabs bandages, finds enemies, attacks people

-Leaves spawn zone, walks to high-traffic areas

 

This is an open-source script, written so its easy to understand for some of you newer guys. If you are a VIP+ member you get the convenience of the script being on the SDN.

 

Source:

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.util.GraphicUtilities;
import org.osbot.rs07.api.util.LocalPathFinder;
import org.osbot.rs07.event.InteractionEvent;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

import java.awt.*;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by Alek on 5/8/2016.
 */
@ScriptManifest(name = "Castle Wars", author = "Alek", version = 1.0, info = "Castle Wars Script", logo = "")
public class CastleWars extends Script {

    ScriptState scriptState = ScriptState.PENDING;
    private Team myTeam = null;
    private Player enemyPlayer = null;

    private final Area ZAMORAK_SPAWN_ROOM = new Area(2368, 3127, 2376, 3135).setPlane(1);
    private final Area SARADOMIN_SPAWN_ROOM = new Area(2423, 3072, 2431, 3080).setPlane(1);
    private final Area ZAMORAK_WAITING_ROOM = new Area(2403, 9510, 2437, 9543);
    private final Area SARADOMIN_WAITING_ROOM = new Area(2363, 9478, 2394, 9502);
    private final Area SARADOMIN_SPAWN_AREA = new Area(2420, 3072, 2431, 3083).setPlane(1);
    private final Area ZAMORAK_SPAWN_AREA = new Area(2368, 3124, 2379, 3135).setPlane(1);

    private final Position ZAMORAK_STAIRS_DOWN = new Position(2380, 3127, 1);
    private final Position SARADOMIN_STAIRS_DOWN = new Position(2419, 3080, 1);
    private final Position ZAMORAK_LADDER_DOWN = new Position(2378, 3134, 1);
    private final Position SARADOMIN_LADDER_DOWN = new Position(2421, 3073, 1);
    private final Position ZAMORAK_BARRIER = new Position(2377, 3131, 1);
    private final Position SARADOMIN_BARRIER = new Position(2422, 3076, 1);
    private final Position CENTER_ISLAND = new Position(2400, 3104, 0);
    private final Position ZAMORAK_CASTLE_FRONT = new Position(2387, 3115, 0);
    private final Position SARADOMIN_CASTLE_FRONT = new Position(2411, 3092, 0);


    private enum Team {
        SARADOMIN(1), ZAMORAK(2);
        int teamId;

        Team(int teamId) {
            this.teamId = teamId;
        }

        int getTeamId() {
            return teamId;
        }
    }

    private enum ScriptState {FIND_PORTAL, WAIT_FOR_GAME, GRAB_SUPPLIES, LEAVE_SPAWN, FIND_OPPONENT, FIGHT_OPPONENT, PENDING}

    @Override
    public void onPaint(Graphics2D g) {
        g.setColor(Color.WHITE);
        g.drawString(scriptState.name(), 10, 120);
    }

    @Override
    public int onLoop() {
        scriptState = getScriptState();
        switch (scriptState) {
            case FIND_PORTAL:
                myTeam = null;
                Optional<RS2Object> optionalPortal = getObjects().getAll().stream().filter(o -> o.getName().equals("Guthix Portal")).findFirst();
                if (optionalPortal.isPresent()) {
                    if (optionalPortal.get().interact("Enter")) {
                        new ConditionalSleep(5000, 500) {
                            @Override
                            public boolean condition() throws InterruptedException {
                                return SARADOMIN_WAITING_ROOM.contains(myPlayer()) || ZAMORAK_WAITING_ROOM.contains(myPlayer());
                            }
                        }.sleep();
                    }
                } else {
                    log("Please start the script in Castle Wars!");
                    stop(false);
                }
                break;
            case WAIT_FOR_GAME:
                if (getDialogues().isPendingOption()) {
                    getDialogues().selectOption("Yes please!");
                } else if (myPlayer().getArea(2).getRandomPosition().interact(getBot(), "Walk here")) {
                    getMouse().moveOutsideScreen();
                    new ConditionalSleep(MethodProvider.random(30000, 60000), 3000) {
                        @Override
                        public boolean condition() throws InterruptedException {
                            return getDialogues().isPendingOption() ||
                                    (SARADOMIN_SPAWN_ROOM.contains(myPosition()) || ZAMORAK_SPAWN_ROOM.contains(myPosition()));
                        }
                    }.sleep();
                }
                break;
            case GRAB_SUPPLIES:
                Optional<RS2Object> optionalTable = getObjects().getAll().stream().filter(o -> o.getName().equals("Table") &&
                        (ZAMORAK_SPAWN_ROOM.contains(o) || SARADOMIN_SPAWN_ROOM.contains(o))).findFirst();
                if (optionalTable.isPresent()) {
                    for (int i = 0; i < MethodProvider.random(1, 5); i++) {
                        if (optionalTable.get().interact("Take-5")) {
                            final int slots = getInventory().getEmptySlots();
                            new ConditionalSleep(5000, 500) {
                                @Override
                                public boolean condition() throws InterruptedException {
                                    return slots != getInventory().getEmptySlots();
                                }
                            }.sleep();
                        }
                    }
                }
                break;
            case LEAVE_SPAWN:
                boolean successfullyLeftSpawnArea = false;
                if (SARADOMIN_SPAWN_ROOM.contains(myPosition()) || ZAMORAK_SPAWN_ROOM.contains(myPosition())) {
                    Optional<RS2Object> optionalBarrier;
                    if (getMyTeam().equals(Team.ZAMORAK))
                        optionalBarrier = getObjects().getAll().stream().filter(o -> o.getPosition().equals(ZAMORAK_BARRIER)).findFirst();
                    else
                        optionalBarrier = getObjects().getAll().stream().filter(o -> o.getPosition().equals(SARADOMIN_BARRIER)).findFirst();

                    if (optionalBarrier.isPresent()) {
                        if (optionalBarrier.get().interact("Pass")) {
                            new ConditionalSleep(5000, 500) {
                                @Override
                                public boolean condition() throws InterruptedException {
                                    return !SARADOMIN_SPAWN_ROOM.contains(myPosition()) && !ZAMORAK_SPAWN_ROOM.contains(myPosition());
                                }
                            }.sleep();
                        }
                    }
                } else {
                    Optional<RS2Object> optionalStairs;
                    if (getMyTeam().equals(Team.ZAMORAK))
                        optionalStairs = getObjects().getAll().stream().filter(o -> o.getPosition().equals(ZAMORAK_STAIRS_DOWN)).findFirst();
                    else
                        optionalStairs = getObjects().getAll().stream().filter(o -> o.getPosition().equals(SARADOMIN_STAIRS_DOWN)).findFirst();
                    if (optionalStairs.isPresent()) {
                        if (!isPathBlocked(optionalStairs.get())) {
                            if (optionalStairs.get().interact("Climb")) {
                                successfullyLeftSpawnArea = true;
                                new ConditionalSleep(5000, 500) {
                                    @Override
                                    public boolean condition() throws InterruptedException {
                                        return myPosition().getZ() == 0;
                                    }
                                }.sleep();
                            }
                        } else {
                            Optional<RS2Object> optionalLadder;
                            if (getMyTeam().equals(Team.ZAMORAK))
                                optionalLadder = getObjects().getAll().stream().filter(o -> o.getPosition().equals(ZAMORAK_LADDER_DOWN)).findFirst();
                            else
                                optionalLadder = getObjects().getAll().stream().filter(o -> o.getPosition().equals(SARADOMIN_LADDER_DOWN)).findFirst();
                            if (optionalLadder.isPresent()) {
                                if (!isPathBlocked(optionalLadder.get())) {
                                    if (optionalLadder.get().interact("Climb-down")) {
                                        successfullyLeftSpawnArea = true;
                                        new ConditionalSleep(5000, 500) {
                                            @Override
                                            public boolean condition() throws InterruptedException {
                                                return myPosition().getZ() == 0;
                                            }
                                        }.sleep();
                                    }
                                } else {
                                    NPC barricade = getNpcs().closest("Barricade");
                                    if (barricade != null) {
                                        if (barricade.interact("Attack")) {
                                            new ConditionalSleep(5000, 500) {
                                                @Override
                                                public boolean condition() throws InterruptedException {
                                                    return !barricade.exists();
                                                }
                                            }.sleep();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (successfullyLeftSpawnArea) {
                        switch (MethodProvider.random(0, 2)) {
                            case 1:
                                getWalking().webWalk(CENTER_ISLAND);
                                break;
                            case 2:
                                if (getMyTeam().equals(Team.ZAMORAK))
                                    getWalking().webWalk(ZAMORAK_CASTLE_FRONT);
                                else
                                    getWalking().webWalk(SARADOMIN_CASTLE_FRONT);
                                break;
                        }
                    }
                }
                break;
            case FIND_OPPONENT:
                Collection<Player> enemies = getPlayers().getAll().stream().filter(p -> p.getTeam() != getMyTeam().getTeamId()).collect(Collectors.toList());
                if (!enemies.isEmpty()) {
                    for (Player enemy : enemies) {
                        Rectangle enemyBounds = GraphicUtilities.getModelBoundingBox(getBot(), enemy.getGridX(), enemy.getGridY(), enemy.getZ(), enemy.getModel());
                        if (getMap().canReach(enemy)) {
                            if (enemyBounds.intersects(GraphicUtilities.MAIN_SCREEN_CLIP)) {
                                InteractionEvent event = new InteractionEvent(enemy, "Attack").setWalkTo(false);
                                execute(event);
                                if (event.hasFinished()) {
                                    enemyPlayer = enemy;
                                    break;
                                }
                            } else if (MethodProvider.random(0, 1) == 1) {
                                getWalking().walk(enemy);
                                break;
                            }
                        }
                    }
                }
                break;
            case FIGHT_OPPONENT:
                if (enemyPlayer.exists()) {
                    if (myPlayer().isInteracting(enemyPlayer)) {
                        if (getInventory().contains("Bandages") && (getSkills().getStatic(Skill.HITPOINTS) - getSkills().getDynamic(Skill.HITPOINTS) > 10))
                            getInventory().getItem("Bandages").interact("Heal");
                        new ConditionalSleep(MethodProvider.random(2000, 5000), 500) {
                            @Override
                            public boolean condition() throws InterruptedException {
                                return enemyPlayer.exists();
                            }
                        }.sleep();
                    } else if (getMap().canReach(enemyPlayer)) {
                        enemyPlayer.interact("Attack");
                    } else {
                        enemyPlayer = null;
                    }
                } else {
                    enemyPlayer = null;
                }
                break;
        }
        return 1000;
    }

    private ScriptState getScriptState() {
        if (getConfigs().get(1021) == 0)
            return ScriptState.FIND_PORTAL;
        if (ZAMORAK_WAITING_ROOM.contains(myPlayer()) || SARADOMIN_WAITING_ROOM.contains(myPlayer()))
            return ScriptState.WAIT_FOR_GAME;
        if (SARADOMIN_SPAWN_AREA.contains(myPlayer()) || ZAMORAK_SPAWN_AREA.contains(myPlayer()))
            if (!getInventory().contains("Bandages"))
                return ScriptState.GRAB_SUPPLIES;
            else
                return ScriptState.LEAVE_SPAWN;
        if (enemyPlayer == null)
            return ScriptState.FIND_OPPONENT;
        return ScriptState.FIGHT_OPPONENT;
    }

    private Team getMyTeam() {
        if (myTeam == null)
            myTeam = myPlayer().getTeam() == 1 ? Team.SARADOMIN : Team.ZAMORAK;
        return myTeam;
    }

    private boolean isPathBlocked(Entity entity) {
        LocalPathFinder pf = new LocalPathFinder(bot);
        LinkedList<Position> path = pf.findPath(entity);
        if (pf.foundPath()) {
            Collection<NPC> barricades = getNpcs().getAll().stream().filter(o -> o.getName().equals("Barricade")).collect(Collectors.toList());
            for (Position position : path)
                for (NPC barricade : barricades)
                    if (barricade.getPosition().equals(position))
                        return true;
            return false;
        }
        return true;
    }
}

  • Like 16
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...