ScriptMonsterTideService.java 3.77 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);
    }

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    public class OnMonsterDead implements ScriptMonsterListener{
        @Override
        public void onNotify(EntityMonster sceneMonster) {
            if(monsterSceneLimit <= 0){
                return;
            }
            if(monsterAlive.decrementAndGet() >= monsterSceneLimit) {
                // maybe not happen
                return;
            }
            monsterKillCount.incrementAndGet();
            if(monsterTideCount.get() > 0){
                // 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
        }
82
83
84
85
86
    }

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