Commit eaf78424 by 李进峰

path search

parent eeacf968
...@@ -13,14 +13,18 @@ import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsInstance; ...@@ -13,14 +13,18 @@ import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsInstance;
import com.qiwu.chatbotbackend.GameEnvironment.bean.SuperClass; import com.qiwu.chatbotbackend.GameEnvironment.bean.SuperClass;
import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcInstance; import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcInstance;
import com.qiwu.chatbotbackend.GameEnvironment.bean.Place; import com.qiwu.chatbotbackend.GameEnvironment.bean.Place;
import com.qiwu.chatbotbackend.GameEnvironment.bean.PlaceLink;
public class GameEnv implements Serializable { public class GameEnv implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public Map<String, SuperClass> npcClassIndex; public Map<String, SuperClass> npcClassIndex;
public Map<String, NpcInstance> npcInstanceIndex; public Map<String, NpcInstance> npcInstanceIndex;
public Map<String, SuperClass> goodsClassIndex; public Map<String, SuperClass> goodsClassIndex;
public Map<String, GoodsInstance> goodsInstanceIndex; public Map<String, GoodsInstance> goodsInstanceIndex;
public Map<String, Place> placeIndex; public Map<String, Place> placeIndex;
public byte[][] mapGraph;
public Map<String, Integer>mapNames;
public String[] mapIndexName;
public GameEnv(String botName) throws Exception { public GameEnv(String botName) throws Exception {
// 通过文件来初始化游戏环境 // 通过文件来初始化游戏环境
...@@ -30,30 +34,70 @@ public class GameEnv implements Serializable { ...@@ -30,30 +34,70 @@ public class GameEnv implements Serializable {
// System.out.println(jsonString); // System.out.println(jsonString);
String jsonString = DBUtil.queryEnvJsonByBotName(botName); String jsonString = DBUtil.queryEnvJsonByBotName(botName);
Map<String, Object> gameProjectMap = JSON.parseObject(jsonString); Map<String, Object> gameProjectMap = JSON.parseObject(jsonString);
// System.out.println(gameProjectMap); System.out.println(gameProjectMap);
if(!gameProjectMap.isEmpty()) { if(!gameProjectMap.isEmpty()) {
// 地图 // 地图
Map<String, Object> map = (Map<String, Object>)gameProjectMap.get("map"); Map<String, Object> map = (Map<String, Object>)gameProjectMap.get("map");
this.placeIndex = new HashMap<String, Place>(); this.placeIndex = new HashMap<String, Place>();
Map<String, Object> placeMapTmp = new HashMap(); Map<String, Object> placeMapTmp = new HashMap();
this.mapGraph = new byte[map.size()][map.size()];
this.mapNames = new HashMap<String, Integer>();
if(!map.isEmpty()) { if(!map.isEmpty()) {
// 第一步先把除了 canGo 之外的信息创建一个对象
int i= 0;
for(String key:map.keySet()) { for(String key:map.keySet()) {
Map<String, Object> one = (Map<String, Object>)map.get(key); Map<String, Object> one = (Map<String, Object>)map.get(key);
placeMapTmp.put(key, (String)one.get("name")); placeMapTmp.put(key, (String)one.get("name"));
Place place = new Place(); Place place = new Place();
place.setIntro((String)one.get("intro")); place.setIntro((String)one.get("intro"));
place.setName((String)one.get("name")); place.setName((String)one.get("name"));
place.setNickname((List)one.get("nickname"));
mapNames.put((String)one.get("name"), i);
i++;
placeIndex.put((String)one.get("name"), place); placeIndex.put((String)one.get("name"), place);
} }
// 添加 canGo 指针关联
for(String tmp:placeMapTmp.keySet()) { for(String tmp:placeMapTmp.keySet()) {
List<Place> ableGos = new ArrayList<Place>(); List<Place> ableGos = new ArrayList<Place>();
List<Long> ableGoLongs = (List<Long>)((Map<String, Object>)map.get(tmp)).get("canGo"); List<Long> ableGoLongs = (List<Long>)((Map<String, Object>)map.get(tmp)).get("canGo");
for(int j=0; j<ableGoLongs.size();j++) { for(int j=0; j<ableGoLongs.size();j++) {
String idName = String.valueOf(ableGoLongs.get(j)); String idName = String.valueOf(ableGoLongs.get(j));
ableGos.add(placeIndex.get(placeMapTmp.get(idName))); ableGos.add(placeIndex.get(placeMapTmp.get(idName)));
// 添加图结构数据
int x = this.mapNames.get(placeMapTmp.get(tmp));
int y = this.mapNames.get(placeMapTmp.get(idName));
this.mapGraph[x][y] = 1;
} }
placeIndex.get(placeMapTmp.get(tmp)).setAbleGo(ableGos); placeIndex.get(placeMapTmp.get(tmp)).setAbleGo(ableGos);
} }
for(i=0; i<mapGraph.length; i++) {
for(String mapName:mapNames.keySet()) {
if(mapNames.get(mapName).equals(i)) {
System.out.print(mapName+"\t");
}
}
}
// 映射index -> map name
this.mapIndexName = new String[mapGraph.length];
for(i=0; i<mapGraph.length; i++) {
for(String mapName:mapNames.keySet()) {
if(mapNames.get(mapName).equals(i)) {
mapIndexName[i] = mapName;
}
}
}
// for(i=0; i<this.mapGraph.length; i++) {
// for(String mapName:mapNames.keySet()) {
// if(mapNames.get(mapName).equals(i)) {
// System.out.print(mapName+"\t");
// }
// }
// for(int j=0; j<this.mapGraph[i].length; j++) {
// System.out.print(this.mapGraph[i][j]+"\t");
// }
// System.out.print("\n\n");
// }
} }
// npc // npc
// npc class // npc class
...@@ -245,9 +289,66 @@ public class GameEnv implements Serializable { ...@@ -245,9 +289,66 @@ public class GameEnv implements Serializable {
} }
return ableGoString; return ableGoString;
} }
// 两点之间的路径
public List<String> queryPathBySite(String start, String destination){ public Integer isNeighbour(String placeA, String placeB) {
return null; if(placeA.equals(placeB)) {
return 0;
}else {
int x = this.mapNames.get(placeA);
int y = this.mapNames.get(placeB);
if(this.mapGraph[x][y] == 1) {
return 2;
}else {
return 1;
}
}
}
// 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。
public List<String> queryPathBySite(String placeA, String placeB){
List<String> paths = new ArrayList<String>();
Deque<PlaceLink> placeDeque = new ArrayDeque<PlaceLink>();
Integer start = this.mapNames.get(placeA);
PlaceLink placeLink = new PlaceLink();
placeLink.setIndex(start);
boolean[] marked = new boolean[this.mapNames.size()];
for(int j=0; j<marked.length; j++) {
marked[j] = false;
}
placeDeque.push(placeLink);
int flag = 0;
while(!placeDeque.isEmpty() && flag == 0) {
PlaceLink placeLinkNode = placeDeque.pop();
marked[placeLinkNode.getIndex()] = true;
for(int i=0; i<this.mapGraph[placeLinkNode.getIndex()].length; i++) {
if(marked[i]==false) {
if(this.mapGraph[placeLinkNode.getIndex()][i] == 1) {
marked[i] = true;
PlaceLink placeLinkNodeSecond = new PlaceLink();
placeLinkNodeSecond.setIndex(i);
placeLinkNodeSecond.setPlaceLink(placeLinkNode);
List<String> nickname = this.placeIndex.get(this.mapIndexName[i]).getNickname();
if(this.mapIndexName[i].equals(placeB) || (nickname!=null && nickname.indexOf(placeB)>0)) {
while(placeLinkNodeSecond.getPlaceLink() != null) {
paths.add(this.mapIndexName[placeLinkNodeSecond.getIndex()]);
placeLinkNodeSecond = placeLinkNodeSecond.getPlaceLink();
}
paths.add(this.mapIndexName[placeLinkNodeSecond.getIndex()]);
flag = 1;
break;
}
placeDeque.push(placeLinkNodeSecond);
}
}
}
}
Collections.reverse(paths);
List<String> path = new ArrayList<String>();
for(int i=0; i<paths.size(); i++) {
if(i!=0&&i!=(paths.size()-1)) {
path.add(paths.get(i));
}
}
return path;
} }
/******************************************通过npc获取物品列表**********************************************/ /******************************************通过npc获取物品列表**********************************************/
// 通过npcName获取所有物品名称列表 // 通过npcName获取所有物品名称列表
......
package com.qiwu.chatbotbackend.GameEnvironment.bean;
public class PlaceLink {
private Integer index;
private PlaceLink placeLink;
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public PlaceLink getPlaceLink() {
return placeLink;
}
public void setPlaceLink(PlaceLink placeLink) {
this.placeLink = placeLink;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment