diff --git a/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java b/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java index 809e052..a703f25 100644 --- a/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java +++ b/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java @@ -21,7 +21,6 @@ public class PlotConstructor2 extends ElementConstructor { JSONArray plots = null; HashMap> adjList = new HashMap>(); - HashMap> revAdjList = new HashMap>(); private HashMap plotIndex = new HashMap(); HashMap visited = new HashMap(); @@ -103,7 +102,7 @@ public class PlotConstructor2 extends ElementConstructor { /** - * 根据id构造邻接表 + * 根据id构造邻接表,将剧情数据变成树状结构 * @param plots */ public void constructAdjList(JSONArray plots) { @@ -121,6 +120,8 @@ public class PlotConstructor2 extends ElementConstructor { adjList.put(id, new ArrayList<>()); } + + //如果npc是离线的,就过滤掉剧情文本中的变量槽 varSlotFilt(plot, keywordMap); //添加branch中的下一个plot @@ -129,6 +130,7 @@ public class PlotConstructor2 extends ElementConstructor { for (int j = 0; j < branchSize; j++) { nextPlotId = (String) branch.getJSONObject(j).get("nextPlotId"); + //过滤特殊字符 branch.getJSONObject(j).put("curPremise", premiseTextFilt(branch.getJSONObject(j).getJSONArray("curPremise"))); branch.getJSONObject(j).put("hisPremise", premiseTextFilt(branch.getJSONObject(j).getJSONArray("hisPremise"))); @@ -136,14 +138,12 @@ public class PlotConstructor2 extends ElementConstructor { //将剧情和相应文本相对应 //plotTextIndex.put(nextPlotId, nextPlotText); - if (revAdjList.get(nextPlotId) == null) { - revAdjList.put(nextPlotId, new ArrayList<>()); - } - + //在邻接表中放入当前剧情的下一步剧情 adjList.get(id).add(nextPlotId); - revAdjList.get(nextPlotId).add(id); } + + //剧情文本特殊字符的过滤 plot.put("plotText", textReplace(plot.getString("plotText"), replaceMap)); plotIndex.put(id, plot); } @@ -195,6 +195,12 @@ public class PlotConstructor2 extends ElementConstructor { /** + * + * 剧情生成中包括3个剧情标识: + * TaskLine 某地点剧情进度 + * TaskCondition 某地点剧情进度用到的condition + * TaskReply 某地点剧情用到的文本 + * 剧情(id)的状态:null标识未激活,1、 2表示激活 * 将所有剧情根据地点分为多条剧情线,每一条剧情线都有一个计数器,记录当前地点的剧情到了第几步。 * 加上剧情计数器, null时未激活,大于等于1时表示已激活 * @param root @@ -214,29 +220,40 @@ public class PlotConstructor2 extends ElementConstructor { List curPremiseList, historyPremiseList; + + //设置初始化unknown文本 root.put("nextUnknownText", varInitialMap.get("unknownText")); root.put("parentId", "null"); + + //当前剧情是否自动跳转的标识 root.put("autoJump", false); + //上一步剧情是否自动跳转的标识 root.put("prevAutoJump", false); jsonObject.put("plotStartText", ((String)jsonObject.get("plotStartText")).replace("\n", "").replace(",", ",")); if(root.get("conditionName") != null && !root.get("conditionName").equals("")) conditionName = (String)root.get("conditionName"); else{ + + //如果剧情是无条件剧情,就设置默认condition conditionName = DEFAULT_CONDITION; root.put("conditionName", conditionName); } - String startPlace = ((String)root.get("place")).split("\\+")[0]; + //根据地点生成变量名,标志地点的任务进度,condition和回答 + String startPlace = root.getString("place"); String startTaskLine = String.format(TASK_LINE, startPlace); String startTaskCondition = String.format(TASK_CONDITION, startPlace); String startTaskReply = String.format(TASK_REPLY, startPlace); + + //在配置文件中设置初始值 varInitialMap.put("site", startPlace); varInitialMap.put(startTaskLine, String.format(GAME_STEP_FORMAT, botAccount, rootId)); varInitialMap.put(startTaskCondition, ""); varInitialMap.put(startTaskReply, ""); + //添加变量的描述 conditionDescription.put(startTaskLine, startTaskLine); conditionDescription.put(startTaskCondition,startTaskCondition); conditionDescription.put(startTaskReply, startTaskReply); @@ -246,17 +263,23 @@ public class PlotConstructor2 extends ElementConstructor { //generateStartGame(root, jsonObject, startPlace, conditionName); Queue queue = new LinkedList(); + queue.add(root); //System.out.println(root.toJSONString()); while (!queue.isEmpty()) { node = queue.remove(); + //剧情id id = (String) node.get("id"); location = ((String) node.get("place")).split("\\+")[0]; conditionName = (String) node.get("conditionName"); + + //当前剧情是否自动跳转 curAutoJump = node.getBoolean("autoJump"); prevAutoJump = node.getBoolean("prevAutoJump"); curUnknownText = (String) node.get("unknownText"); + + //剧情状态的名字,任务线xxx的进度 taskLineName = String.format(TASK_LINE, location); conditionDescription.put(id, String.format(PLOT_STATUS, id)); varInitialMap.put(id ,""); @@ -270,7 +293,7 @@ public class PlotConstructor2 extends ElementConstructor { branchSize = branch.size(); - + //遍历剧情分支 for (int i = 0; i < branchSize; i++) { skipText.setLength(0); @@ -282,19 +305,26 @@ public class PlotConstructor2 extends ElementConstructor { nextId = branchNode.getString("nextPlotId"); - //自动寻路 + + //是否将玩家移动到下个剧情所在地点 moveWithPlot = branchNode.getBoolean("moveWithPlot"); + + //如果下一部剧情不存在,就新建一个终止结点,id名为当前id+"_end",终止节点剧情文本为当前剧情的unknown文本 if(nextId == null || nextId.equals("") || !plotIndex.containsKey(nextId)){ nextId = id + "_end"; createEndNode(node, id, nextId, curUnknownText); } + + //如果选了合并剧情/自动跳转,那么一定会把玩家地点切换到下个剧情的地点 if(moveWithPlot == null || curAutoJump) moveWithPlot = true; + //下一步剧情的unknown文本 nextUnknownText = plotIndex.get(nextId).getString("unknownText"); + //下一步剧情的自动跳转 nextAutoJump = plotIndex.get(nextId).getBoolean("autoJump"); reply = plotIndex.get(nextId).getString("plotText"); @@ -305,25 +335,23 @@ public class PlotConstructor2 extends ElementConstructor { - //如果存在下一剧情,就将下一步剧情的condition设为下一个激活的conditin,把地点更新为下一步剧情的地点,并更新任务线进度 + //如果存在下一剧情,就将下一步剧情的condition设为要激活的conditin,把地点更新为下一步剧情的地点,并更新任务线进度 nextCondition = plotIndex.get(nextId).getString("conditionName"); if(nextCondition.equals("")) + //condition为空,则设置默认condition nextCondition = DEFAULT_CONDITION; nextLocation = plotIndex.get(nextId).getString("place"); - //任务线 + //需要更新 下个剧情发生地点 任务线进度 nTaskLineName = String.format(TASK_LINE, nextLocation); - //任务线最后一个condition + ////需要更新 下个剧情发生地点最后一个condition lastTaskCondition = String.format(TASK_CONDITION, nextLocation); lastTaskReply = String.format(TASK_REPLY, nextLocation); -// if(nextId.endsWith("_end")) -// nextGameStep = id; -// else -// nextGameStep = nextId; + //剧情点qa标识 gameStep = String.format(GAME_STEP_FORMAT, botAccount, id); nextGameStep = String.format(GAME_STEP_FORMAT, botAccount,nextId); @@ -337,24 +365,38 @@ public class PlotConstructor2 extends ElementConstructor { */ //现在暂时放开下一步剧情未发生过的限制条件 - //historyPremise - //currentPremise //extraHP = taskLineName + "::match-Include=" + id + ",site::match-Include=" + location + "," + id + "::number-BiggerOrEqual=" + 1 + "," + nextId + "::match-Include=null"; + /** + * 剧情分支触发的系统前置条件 + * 该地点的任务线进度=玩家剧情进度 + * 玩家处于剧情地点 + * 剧情已被激活 + */ appendPremise(extraHP, taskLineName, "match", "Include", gameStep); appendPremise(extraHP, "site", "match", "Include", location); appendPremise(extraHP, id, "match", "Include", "1"); + /** + * 剧情分支触发后的变量更新操作 + * 下个剧情发生地点 任务线进度 + * 下个剧情发生地点最后一个condition + * 当前剧情状态加一 + * 下一步剧情设为1, 激活 + * 无匹配次数清零 + * + */ + appendKnowledge(extraUK, nTaskLineName, nextGameStep); appendKnowledge(extraUK, lastTaskCondition, nextCondition); appendKnowledge(extraUK, lastTaskReply, reply); - appendKnowledge(extraUK, id, "+1"); //当前剧情状态加一 - appendKnowledge(extraUK, nextId, "1"); //下一步设为1 - appendKnowledge(extraUK, nTaskLineName, nextGameStep); + appendKnowledge(extraUK, id, "+1"); + appendKnowledge(extraUK, nextId, "1"); appendKnowledge(extraUK, UNKNOWN_COUNTER_NAME, "0"); + //如果剧情结束了,那么qa还是留在最后一部剧情 //如果下一步剧情不存在,那么下一步剧情id为当前id加后缀_end,但是用户提交qa的game_step应该还是当前id,没有加上_end if(nextId.endsWith("_end")) { nextGameStep = String.format(GAME_STEP_FORMAT, botAccount,id); @@ -364,7 +406,8 @@ public class PlotConstructor2 extends ElementConstructor { //System.out.println(id); appendFeature(extraFT, "match"); - //下一步为自动跳转且地点相同,则将回答放在plot_jump_text + //如果下一步剧情是自动跳转,并且(当前剧情和下一部剧情地点相同或者用户选中了自动切换地点), + // 先把本剧情的文本保留到plot_jump_text, 然后系统会在这个配置文件处理完成后,激活下一个(自动跳转)剧情的配置文件 if(doActivatePlot(nextAutoJump, location, nextLocation, moveWithPlot) ){ appendKnowledge(extraUK, "plot_jump_text", "str+" + reply); } @@ -403,13 +446,15 @@ public class PlotConstructor2 extends ElementConstructor { */ + //如果用户在分支里选了提前激活 if(branchNode.containsKey("activateExtraPlot")) { //激活除了下一步剧情以外的剧情 extraActivatedPlot(extraUK, nextLocation, branchNode.getJSONObject("activateExtraPlot")); } - //如果主角的位置随着剧情移动或者下一步剧情地点和当前剧情相同,主角地点和game_step要做相应更新 + //如果主角的位置随着剧情移动(切换地点)或者下一步剧情地点和当前剧情相同,即如果玩家可以直接到达下一个剧情所在地点,主角地点和game_step要做相应更新, + if(moveWithPlot || location.equals(nextLocation)) { appendKnowledge(extraUK, "site", nextLocation); appendKnowledge(extraUK, "game_step", nextGameStep); @@ -422,16 +467,8 @@ public class PlotConstructor2 extends ElementConstructor { } else { - //任务线最后一个condition - - - /** - * 注意,比较的是当前地点和跳转前的地点 - * 地点更新为跳转后的地点 - */ - //可能因为不存在自动跳转或者地点不同,所以执行现在的代码,如果在剧情中存在自动跳转的情况,那么应该比较自动跳转完成后的地点喝下一步剧情的地点是否相同 if(!location.equals(nextLocation)){ //System.out.println(id + "报异常" + curUnknownText); if(curUnknownText.equals("")) { @@ -447,28 +484,27 @@ public class PlotConstructor2 extends ElementConstructor { - //下一部剧情在不同地点时,为了防止到达下个地点的剧情重复执行,需要加一个标志表示下一步剧情是否已经发生过 + //下一部剧情在不同地点时,为了防止到配置文件重复执行,需要加一个标志表示下一步剧情是否已经发生过 + // activate_by 下一个地点的剧情是由当前剧情激活的 + //_activate_ready 下一个地点的剧情等待被激活 appendKnowledge(extraUK, nextLocation + "_activate_by", id); appendKnowledge(extraUK, nextLocation + "_activate_ready", "1"); + // 如果不能直接到达所在地点,说明剧情1-1和1-2直接存在一个中间结点,这些更新操作会放在中间结点执行(动态生成,方法名:arriveActivatePlot2 这个中间结点会被当做一个监听剧情来处理) arriveActivatePlot2(jsonObject, id, nextId, nextLocation); - //如果下一步剧情不在同一地点,那么即使有跳转剧情,也是在下一个地点才说 + //回答改为提示文本 reply = hint; - - - //必须在上一个jConfig添加完之后 - //extraUK = extraUK.replace("-<-" + nextId + ":" + "+1", ""); - } } //System.out.println(branchNode); + //取出作者设定的剧情条件 curPremiseList = JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), Premise.class); historyPremiseList = JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), Premise.class); @@ -479,16 +515,22 @@ public class PlotConstructor2 extends ElementConstructor { appendFeature(extraFT, "includeCurInfo"); } + + //如果分支中存在额外操作,在将这些额外操作(提交表单、激活、存档、读档等)添加到配置文件 if(branchNode.get("extraFeature") != null){ appendFeature(extraFT, branchNode.getString("extraFeature")); } + //如果符合激活下一个剧情配置文件的操作 if(doActivatePlot(nextAutoJump, location, nextLocation, moveWithPlot) ){ + //在配置文件中添加激活的操作 appendFeature(extraFT, "activate:after"); + //激活下一步剧情id nextCondition = "plot_" + nextId; } + //生成配置文件 configTermText = new ConfigTermText( curPremiseList, historyPremiseList, @@ -499,13 +541,16 @@ public class PlotConstructor2 extends ElementConstructor { configTermText.setConfigName("plot_" + id, false); - //当剧情没有条件限制时的操作 需要顺便激活qa、地图、物品的查询配置文件, 额外要求当前剧情不是自动跳转 + //当剧情没有条件限制时的操作(条件剧情) 需要顺便激活qa、地图、物品的查询配置文件, 额外要求当前剧情不是自动跳转(当前剧情不是合并剧情) if (curPremiseList.size() == 0 && historyPremiseList.size() == 0 && !curAutoJump){ Boolean activateFlag = false; + + //有activate意味着下一步剧情是合并剧情 if(extraFT.indexOf("activate") != -1) activateFlag = true; unconditionalPlotProcess(configTermText, nextCondition, activateFlag); } + if (branchNode.getBoolean("higherPriority") != null && branchNode.getBoolean("higherPriority")) configTermText.setAuxiliaryPriority("3"); configTermTextList.add(configTermText); @@ -550,41 +595,17 @@ public class PlotConstructor2 extends ElementConstructor { unknownTypeConfigTermGenerate("sleeping", "queryskip1","qa_status::match-Include=OK,qa_score::number-Smaller=90", "match", "{临时变量reply-2}", "process-nextStep:var={临时变量condition-2}"); -// -// configTermText = new ConfigTermText( -// " ", "qa_status::match-Include=OK,qa_score::number-Smaller=90", "match", -// "{临时变量reply-2}", " ", "process-nextStep:var={临时变量condition-2}" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("queryskip1",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); + unknownTypeConfigTermGenerate("sleeping", "queryskip2","qa_status::match-Include=OK,qa_score::number-BiggerOrEqual=90", "match", "{临时变量reply-2}", "process-nextStep:var={临时变量condition-2}"); -// -// configTermText = new ConfigTermText( -// " ", "qa_status::match-Include=OK,qa_score::number-BiggerOrEqual=90", "match", -// "{临时变量reply-2}", " ", "process-nextStep:var={临时变量condition-2}" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("queryskip2",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); + unknownTypeConfigTermGenerate("sleeping", "queryskip3","qa_status::match-Include=not_found", "match", "{临时变量reply-2}", "process-nextStep:var={临时变量condition-2}"); -// -// configTermText = new ConfigTermText( -// " ", "qa_status::match-Include=not_found", "match", -// "{临时变量reply-2}", " ", "process-nextStep:var={临时变量condition-2}" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("queryskip3",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); + @@ -619,67 +640,6 @@ public class PlotConstructor2 extends ElementConstructor { unknownTypeConfigTermGenerate("sleeping", "common_answer","qa_status::match-Include=OK,qa_score::number-BiggerOrEqual=90", "match,return-0", "", ""); - // configTermText = new ConfigTermText(" ", " ", "activate,post::gameQA(" + QA_URL + ")", -// "", "unknown4,unknown3,unknown2,unknown1,common_answer", ""); -// configTermText.setNextStepStatus("active"); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); - -// configTermText = new ConfigTermText( -// " ", "qa_status::match-Include=not_found", "match,return-0", -// "{unknownText-2}", " ", "" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("unknown1",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); - - - - // configTermText = new ConfigTermText( -// " ", "qa_status::match-Include=not_found,unknownText::match-Include=null", "match,return-0", -// UNKNOWN_TEXT, -// " ", "" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("unknown2",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); - - - - // configTermText = new ConfigTermText( -// " ", "qa_status::match-Include=OK,qa_score::number-Smaller=90,unknownText::match-Include=null", "match,return-0", -// UNKNOWN_TEXT, -// " ", "" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("unknown3",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); - - - - // configTermText = new ConfigTermText( -// " ", "qa_status::match-Include=OK,qa_score::number-Smaller=90", "match,return-0", -// "{unknownText-2}", " ", "" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("unknown4",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); - - - - -// configTermText = new ConfigTermText( -// " ", , "match,return-0", -// "" ," ", "" -// ); -// configTermText.setStatus("sleeping"); -// configTermText.setConfigName("common_answer",false); -// configTermText.setAuxiliaryPriority("0"); -// configTermTextList.add(configTermText); //--------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------- @@ -799,8 +759,17 @@ public class PlotConstructor2 extends ElementConstructor { - //如果是无条件剧情,那么需要跟着一个激活操作 + + + /** + * //如果是无条件剧情,那么需要跟着一个激活操作,激活查询qa、地图、物品、npc的配置文件 + * *号是一个通配符,只要配置文件前缀相同就会被激活 + * @param configTermText + * @param nextCondition + * @param activateFlag + */ public void unconditionalPlotProcess(ConfigTermText configTermText, String nextCondition, Boolean activateFlag){ + configTermText.setFeature(configTermText.getFeature() + ",activate,post::gameQA(" + QA_URL + ")"); // @@ -811,7 +780,9 @@ public class PlotConstructor2 extends ElementConstructor { configTermText.setNextStep("queryskip*,map*,item*,npc*"); } + //虽然激活了查询相关的配置文件,但返回给引擎的condition还是应该用剧情里的,所以 //将本配置条目中nextStep的值放在“临时变量condition”中,将本配置条目中的回答放在“临时变量reply”中,激活查询配置后,最后返回的condition应该是剧情指定的下一个condition + //查询完了之后应该 把这两个临时变量复制到被激活的配置文件中,作为回答返回。 configTermText.setUpdateKnowledge(configTermText.getUpdateKnowledge() + "-<-临时变量condition:"+ nextCondition +"-<-临时变量reply:var={reply-3}"); configTermText.setNextStepStatus("active"); } @@ -859,6 +830,12 @@ public class PlotConstructor2 extends ElementConstructor { /** * 除了激活下一步剧情外,还可以同时激活其他地点的剧情,可以在剧情一开始就把ABC三个地点的剧情都激活,这样就不需要先玩完A,才能玩B + * 本方法的主要功能是修改剧情的状态,已知后台会存储剧情一些关于剧情的状态 + * * TaskLine 某地点剧情进度 + * * TaskCondition 某地点剧情进度用到的condition + * * TaskReply 某地点剧情用到的文本 + * * 剧情(id)的状态:null标识未激活,1、 2表示激活 + * 如果激活1-1,那么就是将剧情1-1的状态变为1,剧情所在地点的剧情进度更新为1-1,condition更新为1-1用到的condition * @param updateKnowledge * @param nextPlotLocation * @param activateExtraPlot 激活id列表 @@ -914,7 +891,9 @@ public class PlotConstructor2 extends ElementConstructor { /** - * 当前剧情和下一步剧情不在同一个地点,需要到达地点后才能激活剧情的情况,将这块处理当做listening模块的一个剧情处理 + * 如果剧情1-1的地点是在沙滩,剧情1-2的地点实在营地,作者的游戏编辑平台里没有选中切换地点,那么就需要玩家一步一步地从沙滩走到营地(具体方法说“到xx,到营地”),然后才能开始1-2的剧情 + * 这个时候就需要一个中间结点,当玩家说到营地的时候,才会执行剧情状态的更新,并且回答1-2的剧情文本 + * 这块处理会被当做监听模块的一个剧情处理 */ void arriveActivatePlot2(JSONObject gameElements, String plotId, String nextId, String nextLocation){ @@ -935,6 +914,7 @@ public class PlotConstructor2 extends ElementConstructor { hisPremiseList.add(new Premise("isNeighbour:" + GOTO_CONDITION, "match", "Include", "1+3+4")); + //无匹配次数清零 updateKnowledge.put(UNKNOWN_COUNTER_NAME, "0"); @@ -943,15 +923,25 @@ public class PlotConstructor2 extends ElementConstructor { listeningConfig.put("hisPremise", hisPremiseList); listeningConfig.put("updateKnowledge", updateKnowledge); listeningConfig.put("nextPlotId", nextId); - listeningConfig.put("priority", "1"); + listeningConfig.put("priority", "1"); //剧情优先级默认为1 if(gameElements.getJSONArray("listeningActions") == null) gameElements.put("listeningActions", new JSONArray()); - gameElements.getJSONArray("listeningActions").add(listeningConfig); + gameElements.getJSONArray("listeningActions").add(listeningConfig); //放到监听剧情列表 } + + /** + * 判断是否满足激活下一部剧情的条件 + * 如果下一步剧情是自动跳转,并且(当前剧情和下一部剧情地点相同或者用户选中了自动切换地点),就为真 + * @param nextAutoJump + * @param location + * @param nextLocation + * @param moveWithPlot + * @return + */ Boolean doActivatePlot(Boolean nextAutoJump, String location, String nextLocation, Boolean moveWithPlot) { return nextAutoJump && (location.equals(nextLocation) || moveWithPlot);