1
|
|
|
/* |
2
|
|
|
* This file is part of Araknemu. |
3
|
|
|
* |
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.game.exploration.map; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.dbal.repository.EntityNotFoundException; |
23
|
|
|
import fr.quatrevieux.araknemu.core.event.Dispatcher; |
24
|
|
|
import fr.quatrevieux.araknemu.core.event.EventsSubscriber; |
25
|
|
|
import fr.quatrevieux.araknemu.core.event.Listener; |
26
|
|
|
import fr.quatrevieux.araknemu.data.world.entity.environment.MapTemplate; |
27
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.environment.MapTemplateRepository; |
28
|
|
|
import fr.quatrevieux.araknemu.game.PreloadableService; |
29
|
|
|
import fr.quatrevieux.araknemu.game.exploration.area.AreaService; |
30
|
|
|
import fr.quatrevieux.araknemu.game.exploration.event.ExplorationPlayerCreated; |
31
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.cell.CellLoader; |
32
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.event.MapLoaded; |
33
|
|
|
import fr.quatrevieux.araknemu.game.fight.FightService; |
34
|
|
|
import fr.quatrevieux.araknemu.game.fight.event.FightCreated; |
35
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.SendCreatureMove; |
36
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.SendNewSprite; |
37
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.SendPlayerChangeCell; |
38
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.SendPlayerChangeOrientation; |
39
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.SendSpriteRemoved; |
40
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.fight.HideFightOnStart; |
41
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.fight.SendCancelledFight; |
42
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.fight.SendCreatedFight; |
43
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.fight.SendFightsCount; |
44
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.fight.SendTeamFighterAdded; |
45
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.fight.SendTeamFighterRemoved; |
46
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.fight.SendTeamOptionChanged; |
47
|
|
|
import fr.quatrevieux.araknemu.game.listener.player.SendMapData; |
48
|
|
|
import org.apache.logging.log4j.Logger; |
49
|
|
|
import org.checkerframework.checker.index.qual.NonNegative; |
50
|
|
|
|
51
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
52
|
|
|
import java.util.concurrent.ConcurrentMap; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Service for handle exploration maps |
56
|
|
|
*/ |
57
|
|
|
public final class ExplorationMapService implements PreloadableService, EventsSubscriber { |
58
|
|
|
private final MapTemplateRepository repository; |
59
|
|
|
private final FightService fightService; |
60
|
|
|
private final AreaService areaService; |
61
|
|
|
private final Dispatcher dispatcher; |
62
|
|
|
private final CellLoader loader; |
63
|
|
|
|
64
|
1 |
|
private final ConcurrentMap<@NonNegative Integer, ExplorationMap> maps = new ConcurrentHashMap<>(); |
65
|
|
|
|
66
|
1 |
|
public ExplorationMapService(MapTemplateRepository repository, FightService fightService, AreaService areaService, Dispatcher dispatcher, CellLoader loader) { |
67
|
1 |
|
this.repository = repository; |
68
|
1 |
|
this.fightService = fightService; |
69
|
1 |
|
this.areaService = areaService; |
70
|
1 |
|
this.dispatcher = dispatcher; |
71
|
1 |
|
this.loader = loader; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Load the exploration map |
76
|
|
|
*/ |
77
|
|
|
public ExplorationMap load(@NonNegative int mapId) throws EntityNotFoundException { |
78
|
1 |
|
final ExplorationMap loadedMap = maps.get(mapId); |
79
|
|
|
|
80
|
1 |
|
if (loadedMap == null) { |
81
|
1 |
|
return createMap(repository.get(mapId)); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return loadedMap; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
@Override |
88
|
|
|
public void preload(Logger logger) { |
89
|
1 |
|
logger.info("Loading maps..."); |
90
|
|
|
|
91
|
1 |
|
final long start = System.currentTimeMillis(); |
92
|
|
|
|
93
|
1 |
|
repository.all().forEach(this::createMap); |
94
|
|
|
|
95
|
1 |
|
final long time = System.currentTimeMillis() - start; |
96
|
|
|
|
97
|
1 |
|
logger.info("{} maps successfully loaded in {}ms", maps.size(), time); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
@Override |
101
|
|
|
public String name() { |
102
|
|
|
return "map"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
@Override |
106
|
|
|
public Listener[] listeners() { |
107
|
1 |
|
return new Listener[] { |
108
|
1 |
|
new Listener<ExplorationPlayerCreated>() { |
109
|
|
|
@Override |
110
|
|
|
public void on(ExplorationPlayerCreated event) { |
111
|
1 |
|
event.player().dispatcher().add(new SendMapData(event.player())); |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
@Override |
115
|
|
|
public Class<ExplorationPlayerCreated> event() { |
116
|
1 |
|
return ExplorationPlayerCreated.class; |
117
|
|
|
} |
118
|
|
|
}, |
119
|
|
|
|
120
|
|
|
new SendCreatedFight(this, fightService), |
121
|
1 |
|
new Listener<FightCreated>() { |
122
|
|
|
@Override |
123
|
|
|
public void on(FightCreated event) { |
124
|
1 |
|
final ExplorationMap map = load(event.fight().map().id()); |
125
|
|
|
|
126
|
1 |
|
event.fight().dispatcher().add(new HideFightOnStart(map)); |
127
|
1 |
|
event.fight().dispatcher().add(new SendFightsCount(map, fightService)); |
128
|
1 |
|
event.fight().dispatcher().add(new SendCancelledFight(map, fightService)); |
129
|
1 |
|
event.fight().dispatcher().add(new SendTeamFighterRemoved(map)); |
130
|
1 |
|
event.fight().dispatcher().add(new SendTeamFighterAdded(map)); |
131
|
1 |
|
event.fight().dispatcher().register(new SendTeamOptionChanged(map)); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
@Override |
135
|
|
|
public Class<FightCreated> event() { |
136
|
1 |
|
return FightCreated.class; |
137
|
|
|
} |
138
|
|
|
}, |
139
|
|
|
}; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Load the exploration map |
144
|
|
|
*/ |
145
|
|
|
public ExplorationMap load(MapTemplate template) { |
146
|
1 |
|
final ExplorationMap loadedMap = maps.get(template.id()); |
147
|
|
|
|
148
|
1 |
|
if (loadedMap == null) { |
149
|
1 |
|
return createMap(template); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return loadedMap; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private ExplorationMap createMap(MapTemplate template) { |
156
|
1 |
|
final ExplorationMap map = new ExplorationMap(template, loader, areaService.get(template.subAreaId())); |
157
|
|
|
|
158
|
1 |
|
maps.put(map.id(), map); |
159
|
|
|
|
160
|
1 |
|
map.dispatcher().add(new SendNewSprite(map)); |
161
|
1 |
|
map.dispatcher().add(new SendSpriteRemoved(map)); |
162
|
1 |
|
map.dispatcher().add(new SendPlayerChangeCell(map)); |
163
|
1 |
|
map.dispatcher().add(new SendPlayerChangeOrientation(map)); |
164
|
1 |
|
map.dispatcher().add(new SendCreatureMove(map)); |
165
|
|
|
|
166
|
1 |
|
dispatcher.dispatch(new MapLoaded(map)); |
167
|
|
|
|
168
|
1 |
|
return map; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|