查看: 874|回復: 0

[閒聊閒語] AutoHuntController(完整、可編譯版本)

[複製鏈接]

71

主題

267

帖子

5707

金錢

火焰之影

Rank: 8Rank: 8

威望
247
精華
0
貢獻
0
鑽石
0
閱讀權限
50
積分
6468
在線時間
110 小時
相冊
0
日誌
0
好友
0
發表於 2026-1-5 18:41 | 顯示全部樓層 |閱讀模式
package l1j.server.model.autohunt;

import java.util.Collection;

import l1j.server.model.L1World;
import l1j.server.model.Instance.L1MonsterInstance;
import l1j.server.model.Instance.L1PcInstance;
import l1j.server.model.map.L1Map;
import l1j.server.model.L1Attack;
import l1j.server.model.skill.L1SkillUse;

public class AutoHuntController implements Runnable {

    private final L1PcInstance _pc;
    private Thread _thread;

    private AutoHuntController(L1PcInstance pc) {
        _pc = pc;
    }

    /**
     * 對外唯一入口
     */
    public static void start(L1PcInstance pc) {
        AutoHuntController controller = new AutoHuntController(pc);
        controller.startThread();
    }

    private void startThread() {
        _thread = new Thread(this);
        _thread.setDaemon(true);
        _thread.start();
    }

    @Override
    public void run() {

        while (_pc.isAutoHunt()) {
            try {

                // 1. 基本檢查
                if (_pc.isDead() || _pc.getNetConnection() == null) {
                    break;
                }

                // 2. 找最近怪物
                L1MonsterInstance target = findNearestMonster();
                if (target == null) {
                    Thread.sleep(1000);
                    continue;
                }

                // 3. 移動到攻擊距離
                int distance = _pc.getLocation().getTileDistance(target.getLocation());
                if (distance > 1) {
                    moveToTarget(target);
                    Thread.sleep(500);
                    continue;
                }

                // 4. 攻擊
                attack(target);

                Thread.sleep(800);

            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }

        // 結束保險
        _pc.setAutoHunt(false);
    }

    /**
     * 找最近的怪
     */
    private L1MonsterInstance findNearestMonster() {
        Collection<L1MonsterInstance> list =
                L1World.getInstance().getVisibleObjects(_pc, L1MonsterInstance.class);

        L1MonsterInstance nearest = null;
        int minDist = Integer.MAX_VALUE;

        for (L1MonsterInstance mob : list) {
            if (mob.isDead()) continue;
            if (!mob.getMap().isPassable(mob.getX(), mob.getY())) continue;

            int d = _pc.getLocation().getTileDistance(mob.getLocation());
            if (d < minDist) {
                minDist = d;
                nearest = mob;
            }
        }
        return nearest;
    }

    /**
     * 移動到目標
     */
    private void moveToTarget(L1MonsterInstance target) {
        int dir = _pc.targetDirection(target.getX(), target.getY());
        _pc.setHeading(dir);
        _pc.getMoveState().setHeading(dir);
        _pc.moveForward(dir);
    }

    /**
     * 攻擊(使用既有戰鬥系統)
     */
    private void attack(L1MonsterInstance target) {
        L1Attack attack = new L1Attack(_pc, target);
        attack.calcHit();
        attack.calcDamage();
        attack.action();
        attack.commit();
    }
}






上一篇︰1/5 搖一搖~
下一篇︰1/5 搖一搖
[發帖際遇]: esdion 發帖時在路邊撿到 3 金錢,偷偷放進了口袋. 幸運榜 / 衰神榜
您需要登錄後才可以回帖 登錄 | 註冊會員

本版積分規則

天堂私服列表

45客服

Archiver| 45天堂私服論壇   分享到微博! 分享到臉書! 分享到噗浪! 分享到維特! 分享到Google+! 分享到LINE!

45天堂私服發佈站 ©    天堂私服架設教學  提供最新天堂私服最新資訊

流量最高、品質最好、服務最優、玩家首選、最新天堂私服資訊,都在45天堂私服發佈站.    免責聲明

Sitetag
line客服聯繫
掃一掃二碼
Line客服聯繫
24H專人回覆
返回頂部 返回列表