Commit e04fb7b2 by 李进峰

direct go add

parent 669b44ce
...@@ -24,9 +24,12 @@ public class GameEnv implements Serializable{ ...@@ -24,9 +24,12 @@ public class GameEnv implements Serializable{
public Map<String, GoodsInstance> goodsInstanceIndex; public Map<String, GoodsInstance> goodsInstanceIndex;
public Map<String, String> globalAttrs; public Map<String, String> globalAttrs;
public Map<String, Place> placeIndex; public Map<String, Place> placeIndex;
public Map<String, Place> placeLinkDirectGo;
public Map<String, Place> placeNoLinkDirectGo;
public byte[][] mapGraph; public byte[][] mapGraph;
public Map<String, Integer>mapNames; public Map<String, Integer>mapNames;
public String[] mapIndexName; public String[] mapIndexName;
@SuppressWarnings("unchecked")
public GameEnv(String botName) throws Exception { public GameEnv(String botName) throws Exception {
// 通过文件来初始化游戏环境 // 通过文件来初始化游戏环境
// String fullName = "test.json"; // String fullName = "test.json";
...@@ -37,11 +40,13 @@ public class GameEnv implements Serializable{ ...@@ -37,11 +40,13 @@ public class GameEnv implements Serializable{
Map<String, Object> gameProjectMap = JSON.parseObject(jsonString); Map<String, Object> gameProjectMap = JSON.parseObject(jsonString);
System.out.println(gameProjectMap); System.out.println(gameProjectMap);
this.followNpcIndex = new HashMap<String, NpcInstance>(); this.followNpcIndex = new HashMap<String, NpcInstance>();
this.placeLinkDirectGo = new HashMap<String, Place>();
this.placeNoLinkDirectGo = new HashMap<String, Place>();
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, String> placeMapTmp = new HashMap<String, String>();
this.mapGraph = new byte[map.size()][map.size()]; this.mapGraph = new byte[map.size()][map.size()];
this.mapNames = new HashMap<String, Integer>(); this.mapNames = new HashMap<String, Integer>();
if(!map.isEmpty()) { if(!map.isEmpty()) {
...@@ -53,7 +58,14 @@ public class GameEnv implements Serializable{ ...@@ -53,7 +58,14 @@ public class GameEnv implements Serializable{
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")); place.setGoFlag((Integer)one.get("goFlag"));
if(((Integer)one.get("goFlag")).equals(new Integer(2))) {
this.placeLinkDirectGo.put(place.getName(), place);
}else if(((Integer)one.get("goFlag")).equals(new Integer(3))) {
this.placeNoLinkDirectGo.put(place.getName(), place);
}
List<String> nickname = (List<String>)one.get("nickname");
place.setNickname(nickname);
mapNames.put((String)one.get("name"), i); mapNames.put((String)one.get("name"), i);
i++; i++;
placeIndex.put((String)one.get("name"), place); placeIndex.put((String)one.get("name"), place);
...@@ -136,7 +148,6 @@ public class GameEnv implements Serializable{ ...@@ -136,7 +148,6 @@ public class GameEnv implements Serializable{
// instance // instance
Map<String, Object> instances = (Map<String, Object>)one.get("instance"); Map<String, Object> instances = (Map<String, Object>)one.get("instance");
for(String instanceKey:instances.keySet()) { for(String instanceKey:instances.keySet()) {
Map<String, Object> instance = new HashMap<String, Object>();
NpcInstance npcInstance = new NpcInstance(); NpcInstance npcInstance = new NpcInstance();
Map<String, Object> instanceAttrMap = (Map<String, Object>)instances.get(instanceKey); Map<String, Object> instanceAttrMap = (Map<String, Object>)instances.get(instanceKey);
npcNames.put(instanceKey, (String)instanceAttrMap.get("name")); npcNames.put(instanceKey, (String)instanceAttrMap.get("name"));
...@@ -157,7 +168,8 @@ public class GameEnv implements Serializable{ ...@@ -157,7 +168,8 @@ public class GameEnv implements Serializable{
npcInstance.setAttrs(npcInstanceAttrs); npcInstance.setAttrs(npcInstanceAttrs);
Place place = null; Place place = null;
if(instanceAttrMap.get("placeId")!=null) { if(instanceAttrMap.get("placeId")!=null) {
place = placeIndex.get(((Map<String, Object>)map.get(String.valueOf(instanceAttrMap.get("placeId")))).get("name")); Map<String, Object> placeMap = (Map<String, Object>)map.get(String.valueOf(instanceAttrMap.get("placeId")));
place = placeIndex.get(placeMap.get("name"));
List<NpcInstance> npcInstances = place.getNpcInstances(); List<NpcInstance> npcInstances = place.getNpcInstances();
if(npcInstances == null) { if(npcInstances == null) {
npcInstances = new ArrayList<NpcInstance>(); npcInstances = new ArrayList<NpcInstance>();
...@@ -203,7 +215,6 @@ public class GameEnv implements Serializable{ ...@@ -203,7 +215,6 @@ public class GameEnv implements Serializable{
// instance // instance
Map<String, Object> instances = (Map<String, Object>)one.get("instance"); Map<String, Object> instances = (Map<String, Object>)one.get("instance");
for(String instanceKey:instances.keySet()) { for(String instanceKey:instances.keySet()) {
GoodsInstance goodsInstance = new GoodsInstance(); GoodsInstance goodsInstance = new GoodsInstance();
Map<String, Object> instanceAttrMap = (Map<String, Object>)instances.get(instanceKey); Map<String, Object> instanceAttrMap = (Map<String, Object>)instances.get(instanceKey);
...@@ -220,7 +231,8 @@ public class GameEnv implements Serializable{ ...@@ -220,7 +231,8 @@ public class GameEnv implements Serializable{
} }
goodsInstance.setAttrs(goodsInstanceAttrs); goodsInstance.setAttrs(goodsInstanceAttrs);
if(instanceAttrMap.get("placeId")!=null) { if(instanceAttrMap.get("placeId")!=null) {
Place place = placeIndex.get(((Map<String, Object>)map.get(String.valueOf(instanceAttrMap.get("placeId")))).get("name")); Map<String, Object> placeMap = (Map<String, Object>)map.get(String.valueOf(instanceAttrMap.get("placeId")));
Place place = placeIndex.get(placeMap.get("name"));
goodsInstance.setPlace(place); goodsInstance.setPlace(place);
List<GoodsInstance> placeGoodsInstances= place.getGoodsInstances(); List<GoodsInstance> placeGoodsInstances= place.getGoodsInstances();
if(placeGoodsInstances == null) { if(placeGoodsInstances == null) {
...@@ -255,7 +267,6 @@ public class GameEnv implements Serializable{ ...@@ -255,7 +267,6 @@ public class GameEnv implements Serializable{
this.globalAttrs = (Map<String,String>)gameProjectMap.get("globalAttr"); this.globalAttrs = (Map<String,String>)gameProjectMap.get("globalAttr");
} }
} }
/********************************************地点******************************************/ /********************************************地点******************************************/
// 周围有什么 // 周围有什么
public List<String> queryAroundAllBySite(String site){ public List<String> queryAroundAllBySite(String site){
...@@ -301,35 +312,43 @@ public class GameEnv implements Serializable{ ...@@ -301,35 +312,43 @@ public class GameEnv implements Serializable{
ableGoString.add(this.mapIndexName[i]); ableGoString.add(this.mapIndexName[i]);
} }
} }
for(String placeName:this.placeLinkDirectGo.keySet()) {
ableGoString.add(placeName);
}
for(String placeName:this.placeNoLinkDirectGo.keySet()) {
ableGoString.add(placeName);
}
return ableGoString; return ableGoString;
} }
// 是否相邻
public Integer isNeighbour(String placeA, String placeB) { public Integer isNeighbour(String placeA, String placeB) {
try { try {
int x = this.mapNames.get(placeA); int x = this.mapNames.get(placeA);
if(placeA.equals(placeB) || this.placeIndex.get(placeA).getNickname().indexOf(placeB)>=0) { if(placeA.equals(placeB) || this.placeIndex.get(placeA).getNickname().indexOf(placeB)>=0) {
return 0; return 0;
}else { }
int flag = 0; for(int j=0;j<this.mapGraph[x].length;j++) {
for(int j=0;j<this.mapGraph[x].length;j++) { if(this.mapGraph[x][j] ==1) {
if(this.mapGraph[x][j] ==1) { if(this.mapIndexName[j].equals(placeB) || this.placeIndex.get(this.mapIndexName[j]).getNickname().indexOf(placeB)>=0) {
if(this.mapIndexName[j].equals(placeB) || this.placeIndex.get(this.mapIndexName[j]).getNickname().indexOf(placeB)>=0) { return 1;
flag = 1;
break;
}
} }
} }
if(flag == 1) { }
return 1; for(String placeName:this.placeLinkDirectGo.keySet()) {
}else { if(placeName.equals(placeB) || this.placeLinkDirectGo.get(placeName).getNickname().indexOf(placeB)>=0) {
return 2; return 3;
} }
} }
for(String placeName:this.placeNoLinkDirectGo.keySet()) {
if(placeName.equals(placeB) || this.placeNoLinkDirectGo.get(placeName).getNickname().indexOf(placeB)>=0) {
return 4;
}
}
return 2;
}catch(Exception e) { }catch(Exception e) {
return 2; return 2;
} }
} }
// 通过别名查询当前地点最近的别名地点 // 通过别名查询当前地点最近的别名地点
public String getPlaceNameByNickname(String curPlaceName, String nickname) { public String getPlaceNameByNickname(String curPlaceName, String nickname) {
if(this.placeIndex.get(curPlaceName).getNickname().indexOf(nickname)>=0) { if(this.placeIndex.get(curPlaceName).getNickname().indexOf(nickname)>=0) {
...@@ -344,7 +363,7 @@ public class GameEnv implements Serializable{ ...@@ -344,7 +363,7 @@ public class GameEnv implements Serializable{
} }
} }
// 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。 // 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。只有状态为2的时候使用
public List<String> getAllPath(String placeA, String placeB){ public List<String> getAllPath(String placeA, String placeB){
List<String> paths = new ArrayList<String>(); List<String> paths = new ArrayList<String>();
Deque<PlaceLink> placeDeque = new ArrayDeque<PlaceLink>(); Deque<PlaceLink> placeDeque = new ArrayDeque<PlaceLink>();
...@@ -385,6 +404,7 @@ public class GameEnv implements Serializable{ ...@@ -385,6 +404,7 @@ public class GameEnv implements Serializable{
Collections.reverse(paths); Collections.reverse(paths);
return paths; return paths;
} }
// 两点之间的通过的地方
public List<String> queryPathBySite(String placeA, String placeB){ public List<String> queryPathBySite(String placeA, String placeB){
List<String> paths = this.getAllPath(placeA, placeB); List<String> paths = this.getAllPath(placeA, placeB);
System.out.println(paths); System.out.println(paths);
...@@ -497,7 +517,6 @@ public class GameEnv implements Serializable{ ...@@ -497,7 +517,6 @@ public class GameEnv implements Serializable{
List<String> npcNames = new ArrayList<String>(); List<String> npcNames = new ArrayList<String>();
List<NpcInstance> placeNpcInstances = this.placeIndex.get(site).getNpcInstances(); List<NpcInstance> placeNpcInstances = this.placeIndex.get(site).getNpcInstances();
List<NpcInstance> npcInstances = this.goodsClassIndex.get(className).getNpcInstances(); List<NpcInstance> npcInstances = this.goodsClassIndex.get(className).getNpcInstances();
NpcInstance npcInstance = new NpcInstance();
for(int i=0; i<placeNpcInstances.size(); i++) { for(int i=0; i<placeNpcInstances.size(); i++) {
for(int j=0; j<npcInstances.size(); j++) { for(int j=0; j<npcInstances.size(); j++) {
if(placeNpcInstances.get(i) == npcInstances.get(j)) { if(placeNpcInstances.get(i) == npcInstances.get(j)) {
...@@ -675,7 +694,6 @@ public class GameEnv implements Serializable{ ...@@ -675,7 +694,6 @@ public class GameEnv implements Serializable{
return false; return false;
} }
} }
// 更新npc的位置 // 更新npc的位置
public boolean updateFollowerLocation(String location) { public boolean updateFollowerLocation(String location) {
Place place = this.placeIndex.get(location); Place place = this.placeIndex.get(location);
......
...@@ -7,63 +7,81 @@ public class Place implements Serializable{ ...@@ -7,63 +7,81 @@ public class Place implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String name; private String name;
private String intro; private String intro;
private Integer goFlag;
private List<String> nickname; private List<String> nickname;
private List<Place> ableGo; private List<Place> ableGo;
private List<NpcInstance> npcInstances; private List<NpcInstance> npcInstances;
private List<GoodsInstance> goodsInstances; private List<GoodsInstance> goodsInstances;
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getIntro() { public String getIntro() {
return intro; return intro;
} }
public void setIntro(String intro) { public void setIntro(String intro) {
this.intro = intro; this.intro = intro;
} }
public Integer getGoFlag() {
return goFlag;
}
public void setGoFlag(Integer goFlag) {
this.goFlag = goFlag;
}
public List<String> getNickname() { public List<String> getNickname() {
return nickname; return nickname;
} }
public void setNickname(List<String> nickname) { public void setNickname(List<String> nickname) {
this.nickname = nickname; this.nickname = nickname;
} }
public List<Place> getAbleGo() { public List<Place> getAbleGo() {
return ableGo; return ableGo;
} }
public void setAbleGo(List<Place> ableGo) { public void setAbleGo(List<Place> ableGo) {
this.ableGo = ableGo; this.ableGo = ableGo;
} }
public List<NpcInstance> getNpcInstances() { public List<NpcInstance> getNpcInstances() {
return npcInstances; return npcInstances;
} }
public void setNpcInstances(List<NpcInstance> npcInstances) { public void setNpcInstances(List<NpcInstance> npcInstances) {
this.npcInstances = npcInstances; this.npcInstances = npcInstances;
} }
public List<GoodsInstance> getGoodsInstances() { public List<GoodsInstance> getGoodsInstances() {
return goodsInstances; return goodsInstances;
} }
public void setGoodsInstances(List<GoodsInstance> goodsInstances) { public void setGoodsInstances(List<GoodsInstance> goodsInstances) {
this.goodsInstances = goodsInstances; this.goodsInstances = goodsInstances;
} }
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override @Override
public String toString() { public String toString() {
int ableGoSize = 0; int ableGoSize = 0;
......
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