ScriptMonsterTideService.java 3.78 KB
Newer Older
1
2
3
4
5
6
7
8
package emu.grasscutter.scripts.service;

import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.scripts.SceneScriptManager;
import emu.grasscutter.scripts.constants.EventType;
import emu.grasscutter.scripts.data.SceneGroup;
import emu.grasscutter.scripts.data.SceneMonster;
import emu.grasscutter.scripts.data.ScriptArgs;
9
import emu.grasscutter.scripts.listener.ScriptMonsterListener;
10
11
12
13
14
15
16
17
18
19
20
21
22

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class ScriptMonsterTideService {
    private final SceneScriptManager sceneScriptManager;
    private final SceneGroup currentGroup;
    private final AtomicInteger monsterAlive;
    private final AtomicInteger monsterTideCount;
    private final AtomicInteger monsterKillCount;
    private final int monsterSceneLimit;
    private final ConcurrentLinkedQueue<Integer> monsterConfigOrders;
23
24
    private final OnMonsterCreated onMonsterCreated= new OnMonsterCreated();
    private final OnMonsterDead onMonsterDead= new OnMonsterDead();
25
26
27
28
29
30
31
32
33
34
35

    public ScriptMonsterTideService(SceneScriptManager sceneScriptManager,
                     SceneGroup group, int tideCount, int monsterSceneLimit, Integer[] ordersConfigId){
        this.sceneScriptManager = sceneScriptManager;
        this.currentGroup = group;
        this.monsterSceneLimit = monsterSceneLimit;
        this.monsterTideCount = new AtomicInteger(tideCount);
        this.monsterKillCount = new AtomicInteger(0);
        this.monsterAlive = new AtomicInteger(0);
        this.monsterConfigOrders = new ConcurrentLinkedQueue<>(List.of(ordersConfigId));

36
37
        this.sceneScriptManager.getScriptMonsterSpawnService().addMonsterCreatedListener(onMonsterCreated);
        this.sceneScriptManager.getScriptMonsterSpawnService().addMonsterDeadListener(onMonsterDead);
38
39
40
41
42
43
        // spawn the first turn
        for (int i = 0; i < this.monsterSceneLimit; i++) {
            this.sceneScriptManager.getScriptMonsterSpawnService().spawnMonster(group.id, getNextMonster());
        }
    }

44
45
46
47
48
49
50
    public class OnMonsterCreated implements ScriptMonsterListener{
        @Override
        public void onNotify(EntityMonster sceneMonster) {
            if(monsterSceneLimit > 0){
                monsterAlive.incrementAndGet();
                monsterTideCount.decrementAndGet();
            }
51
52
53
54
55
56
57
58
59
60
61
62
        }
    }

    public SceneMonster getNextMonster(){
        var nextId = this.monsterConfigOrders.poll();
        if(currentGroup.monsters.containsKey(nextId)){
            return currentGroup.monsters.get(nextId);
        }
        // TODO some monster config_id do not exist in groups, so temporarily set it to the first
        return currentGroup.monsters.values().stream().findFirst().orElse(null);
    }

Akka's avatar
Akka committed
63
    public class OnMonsterDead implements ScriptMonsterListener {
64
65
        @Override
        public void onNotify(EntityMonster sceneMonster) {
Akka's avatar
Akka committed
66
            if (monsterSceneLimit <= 0) {
67
68
                return;
            }
Akka's avatar
Akka committed
69
            if (monsterAlive.decrementAndGet() >= monsterSceneLimit) {
70
71
72
73
                // maybe not happen
                return;
            }
            monsterKillCount.incrementAndGet();
Akka's avatar
Akka committed
74
            if (monsterTideCount.get() > 0) {
75
76
77
78
79
80
                // add more
                sceneScriptManager.getScriptMonsterSpawnService().spawnMonster(currentGroup.id, getNextMonster());
            }
            // spawn the last turn of monsters
            // fix the 5-2
            sceneScriptManager.callEvent(EventType.EVENT_MONSTER_TIDE_DIE, new ScriptArgs(monsterKillCount.get()));
81
        }
Akka's avatar
Akka committed
82

83
84
85
86
87
    }

    public void unload(){
        this.sceneScriptManager.getScriptMonsterSpawnService().removeMonsterCreatedListener(onMonsterCreated);
        this.sceneScriptManager.getScriptMonsterSpawnService().removeMonsterDeadListener(onMonsterDead);
88
89
    }
}