You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

player4.cpp 61 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. #include <vector>
  2. #include <thread>
  3. #include <array>
  4. #include "AI.h"
  5. #include "constants.h"
  6. #include <queue>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <chrono>
  10. #include<sstream>
  11. #include<numeric>
  12. #include<strstream>
  13. using namespace std;
  14. // 全局变量区
  15. bool ifchecked = false;
  16. std::vector<std::pair<int32_t, int32_t>> Windows;
  17. std::vector<std::vector<THUAI6::PlaceType>> Map;//全局地图
  18. std::vector<std::pair<int32_t, int32_t>> Door3Position;// 3门的位置(x, y)
  19. std::vector<std::pair<int32_t, int32_t>> Door5Position; // 5门的位置
  20. std::vector<std::pair<int32_t, int32_t>> Door6Position; // 6门的位置
  21. std::vector<std::pair<int32_t, int32_t>> ClassroomPosition;//教室的位置
  22. std::vector<std::pair<int32_t, int32_t>> Chest;//箱子的位置
  23. std::vector<std::pair<int32_t, int32_t>> Gate;//校门
  24. std::vector<std::pair<int32_t, int32_t>> GrassPos;//草地
  25. std::vector<int32_t>LearningProcess;//学习进度,如果学到100LearningProcess变成1
  26. int Grass[50][50] = { 0 };
  27. int iffind = 1;
  28. vector<pair<int, int>>StuPosition(4,pair<int,int>(0,0));
  29. class TrickersInfo//记录捣蛋鬼位置/时间,以后改进算法用
  30. {
  31. public:
  32. TrickersInfo(int X = 0, int Y = 0, int Time = 0)
  33. {
  34. x = X;
  35. y = Y;
  36. time = Time;
  37. }
  38. void reset(int X = 0, int Y = 0, int Time = 0)
  39. {
  40. x = X;
  41. y = Y;
  42. time = Time;
  43. }
  44. int x;
  45. int y;
  46. int time;//帧数
  47. }TrickerInfo;
  48. struct Node
  49. {
  50. int x, y;
  51. int Gcost, Hcost, Fcost;//0 1 2 空地,openlist,closelist
  52. int flag;//初值为0,墙是2
  53. struct Node* Parent;
  54. };
  55. Node NodeMap[50][50] = { 0,0,0,0,0,0,nullptr};
  56. int Target[50][50] = { 0 };//需要进行初始化//后来可能用不到
  57. int obstacleMap[50][50] = { 0 };//障碍物,包括箱子、教室和墙
  58. int RunAwayCoolTime = 0;//冷却用的
  59. class MyFunction
  60. {
  61. public:
  62. //移动函数
  63. bool isLeftoutofcell(IStudentAPI& api, int32_t cellx, int32_t celly) {
  64. if (api.GetSelfInfo()->y - 400 < celly * 1000) return true;
  65. else return false;
  66. }//判断是否在目标格左边一定范围
  67. bool isRightoutofcell(IStudentAPI& api, int32_t cellx, int32_t celly) {
  68. if (api.GetSelfInfo()->y + 400 > (celly + 1) * 1000) return true;
  69. else return false;
  70. }//判断是否在目标格右边一定范围
  71. bool isUpoutofcell(IStudentAPI& api, int32_t cellx, int32_t celly) {
  72. if (api.GetSelfInfo()->x - 400 < cellx * 1000) return true;
  73. else return false;
  74. }//判断是否在目标格上边一定范围
  75. bool isDownoutofcell(IStudentAPI& api, int32_t cellx, int32_t celly) {
  76. if (api.GetSelfInfo()->x + 400 > (cellx + 1) * 1000) return true;
  77. else return false;
  78. }//判断是否在目标格下边一定范围
  79. bool isInsideofcellx(IStudentAPI& api, int32_t cellx) {
  80. if (!isUpoutofcell(api, cellx, -1) && !isDownoutofcell(api, cellx, -1)) return true;
  81. else return false;
  82. }
  83. //判断人是否整个在某行内
  84. bool isInsideofcelly(IStudentAPI& api, int32_t celly) {
  85. if (!isLeftoutofcell(api, -1, celly) && !isRightoutofcell(api, -1, celly)) return true;
  86. else return false;
  87. }
  88. //判断人是否整个在某列内
  89. bool isInsideofcell(IStudentAPI& api, int32_t cellx, int32_t celly) {
  90. if (isInsideofcellx(api, cellx) && isInsideofcelly(api, celly)) return true;
  91. else return false;
  92. }
  93. //判断人是否整个在某格子内
  94. void moveinto(IStudentAPI& api, int32_t cellx, int32_t celly) {
  95. if (!isInsideofcell(api, cellx, celly))
  96. {
  97. if (isLeftoutofcell(api, cellx, celly)) api.MoveRight(50);
  98. else if (isRightoutofcell(api, cellx, celly)) api.MoveLeft(50);
  99. if (isUpoutofcell(api, cellx, celly)) api.MoveDown(50);
  100. else if (isDownoutofcell(api, cellx, celly)) api.MoveUp(50);
  101. }
  102. /*auto self = api.GetSelfInfo();
  103. auto sx = self->x;
  104. auto sy = self->y;
  105. int destx = api.CellToGrid(cellx);
  106. int desty = api.CellToGrid(celly);
  107. api.Print(fmt::format("({},{})->({},{})", sx, sy, destx, desty));
  108. int delta_x = destx - sx;
  109. int delta_y = desty - sy;
  110. auto angle = atan2(delta_y, delta_x);
  111. api.Move(20, angle + std::rand() % 10 / 10);
  112. return;*/
  113. }
  114. //使人的碰撞箱进入一整格内,避免卡死等问题:只用于人在该格子附近的微调!
  115. //ArriveAt函数没用过
  116. bool ArriveAt(IStudentAPI& api, int32_t desX, int32_t desY)
  117. {
  118. auto self = api.GetSelfInfo();
  119. int32_t sx = self->x;
  120. int32_t sy = self->y;
  121. auto cellx = api.GridToCell(sx);
  122. auto celly = api.GridToCell(sy);
  123. if (cellx == desX && celly == desY) return true;
  124. else return false;
  125. }//到达grid
  126. //Astar算法
  127. class Compare
  128. {
  129. public:
  130. Compare() {};
  131. bool operator()(const Node& node1, const Node& node2) const
  132. {
  133. return node1.Fcost > node2.Fcost;
  134. }
  135. }compare_by_cost;
  136. std::vector<std::pair<int32_t, int32_t>>Astar(IStudentAPI& api, int destX, int destY, Node Nodemap[50][50])
  137. {
  138. Node nodemap[50][50];
  139. for (int i = 0; i < 50; i++)
  140. {
  141. for (int j = 0; j < 50; j++)
  142. {
  143. nodemap[i][j] = Nodemap[i][j];
  144. }
  145. }
  146. for (int indexnow = 0; indexnow < 4; indexnow++)
  147. {
  148. if (api.GetStudents()[indexnow]->playerID != api.GetSelfInfo()->playerID)
  149. {
  150. int ox = api.GetStudents()[indexnow]->x, oy = api.GetStudents()[indexnow]->y;
  151. int otherx = api.GridToCell(ox), othery=api.GridToCell(oy);
  152. /*nodemap[otherx][othery].flag = 2;*/
  153. /*nodemap[otherx + 1][othery].flag = 2;
  154. nodemap[otherx][othery+1].flag = 2;
  155. nodemap[otherx - 1][othery].flag = 2;
  156. nodemap[otherx][othery - 1].flag = 2;*/
  157. if (isInsideofcell(api, otherx, othery))
  158. {
  159. nodemap[otherx][othery].flag = 2;
  160. }
  161. else
  162. {
  163. if (ox > api.CellToGrid(otherx))
  164. {
  165. if (oy > api.CellToGrid(othery))
  166. {
  167. nodemap[otherx][othery].flag = 2;
  168. nodemap[otherx+1][othery].flag = 2;
  169. nodemap[otherx][othery+1].flag = 2;
  170. nodemap[otherx - 1][othery].flag = 2;
  171. nodemap[otherx][othery - 1].flag = 2;
  172. nodemap[otherx+1][othery+1].flag = 2;
  173. }
  174. else//oy <= api.CellToGrid(othery)
  175. {
  176. nodemap[otherx][othery].flag = 2;
  177. nodemap[otherx + 1][othery].flag = 2;
  178. nodemap[otherx][othery - 1].flag = 2;
  179. nodemap[otherx - 1][othery].flag = 2;
  180. nodemap[otherx][othery + 1].flag = 2;
  181. nodemap[otherx + 1][othery - 1].flag = 2;
  182. }
  183. }
  184. else
  185. {
  186. if (oy > api.CellToGrid(othery))
  187. {
  188. nodemap[otherx][othery].flag = 2;
  189. nodemap[otherx][othery + 1].flag = 2;
  190. nodemap[otherx - 1][othery].flag = 2;
  191. nodemap[otherx + 1][othery].flag = 2;
  192. nodemap[otherx][othery - 1].flag = 2;
  193. nodemap[otherx - 1][othery + 1].flag = 2;
  194. }
  195. else//oy <= api.CellToGrid(othery)
  196. {
  197. nodemap[otherx][othery].flag = 2;
  198. nodemap[otherx - 1][othery].flag = 2;
  199. nodemap[otherx][othery - 1].flag = 2;
  200. nodemap[otherx][othery+1].flag = 2;
  201. nodemap[otherx+1][othery].flag = 2;
  202. nodemap[otherx - 1][othery - 1].flag = 2;
  203. }
  204. }
  205. }
  206. }
  207. }
  208. auto trickers = api.GetTrickers();
  209. if (!trickers.empty())
  210. {
  211. auto tcellx = trickers[0]->x;
  212. auto tcelly = trickers[0]->y;
  213. nodemap[api.GridToCell(tcellx)][api.GridToCell(tcelly)].flag = 2;
  214. }
  215. api.Print("Astar被调用");
  216. std::priority_queue<Node, std::vector<Node>, Compare> openList;
  217. auto self = api.GetSelfInfo();
  218. nodemap[api.GridToCell(self->x)][api.GridToCell(self->y)].flag=0;
  219. if (self->x / 1000 == destX && self->y / 1000 == destY)
  220. {
  221. std::vector<std::pair<int, int>> path{ std::pair<int, int>(destX,destY) };
  222. return path;//返回自身坐标是已经到了
  223. }
  224. auto& startNode = nodemap[self->x / 1000][self->y / 1000];
  225. startNode.Gcost = 0;
  226. startNode.Hcost = GetManhattanDistance(self->x / 1000, self->y / 1000, destX, destY);
  227. startNode.Fcost = GetManhattanDistance(self->x / 1000, self->y / 1000, destX, destY);
  228. startNode.flag = 1;
  229. startNode.Parent = nullptr;
  230. auto& endNode = nodemap[destX][destY];
  231. endNode.Gcost = 0; endNode.Hcost = 0, endNode.Fcost = 0; endNode.flag = 0; endNode.Parent = nullptr;
  232. openList.push(startNode);
  233. Node* currentNode = &startNode;
  234. while (!openList.empty() && !(currentNode->x == destX && currentNode->y == destY))
  235. {
  236. currentNode->flag = 2;
  237. openList.pop();
  238. for (int i = -1; i <= 1; i++)
  239. {
  240. for (int j = -1; j <= 1; j++)
  241. {
  242. if (!(i * j) && !(i == 0 && j == 0))//代表不是梅花四格和中心格
  243. {
  244. if (isInMap(currentNode->x + i, currentNode->y + j))
  245. {
  246. Node* logicNode = &nodemap[currentNode->x + i][currentNode->y + j];
  247. if (logicNode->x == destX && logicNode->y == destY)
  248. {
  249. logicNode->flag = 2;
  250. logicNode->Parent = currentNode;
  251. currentNode = logicNode;//备注:
  252. goto here;
  253. }
  254. else if ( logicNode->flag == 0)
  255. {
  256. logicNode->Gcost = currentNode->Gcost + 1;
  257. logicNode->flag = 1;//flag=1:加入到openlist
  258. logicNode->Parent = currentNode;
  259. logicNode->Hcost = GetManhattanDistance(logicNode->x, logicNode->y, destX, destY);
  260. logicNode->Fcost = logicNode->Gcost + logicNode->Hcost;
  261. openList.push(*logicNode);
  262. }
  263. }
  264. else
  265. {
  266. continue;
  267. }
  268. }
  269. }
  270. }
  271. if (!openList.empty())
  272. {
  273. currentNode = &nodemap[openList.top().x][openList.top().y];
  274. }
  275. }
  276. here:
  277. if (currentNode->x == self->x / 1000 && currentNode->y == self->y / 1000)
  278. {
  279. std::vector<std::pair<int, int>> path;
  280. api.Print("到不了,返回空路径");
  281. return path;//返回空路径
  282. }
  283. auto path = GetPath(currentNode);
  284. api.Print(fmt::format("Astar返回值:path.size()={}", path.size()));
  285. return path;
  286. }
  287. int32_t GetManhattanDistance(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
  288. {
  289. return abs(x1 - x2) + abs(y1 - y2);
  290. }
  291. std::vector<std::pair<int, int>> GetPath(Node* endNode)
  292. {
  293. std::vector<std::pair<int, int>> path;
  294. Node* node = endNode;
  295. while (node->Parent)
  296. {
  297. path.push_back(std::pair<int, int>(node->x, node->y));
  298. node = node->Parent;
  299. }
  300. path.push_back(std::pair<int, int>(node->x, node->y)); // 添加起点
  301. std::reverse(path.begin(), path.end()); // 路径反转
  302. return path;
  303. }
  304. bool isWall(int x, int y)
  305. {
  306. return (Map.at(x).at(y) == THUAI6::PlaceType::Wall || Map.at(x).at(y) == THUAI6::PlaceType::ClassRoom);
  307. }
  308. bool isInMap(int x, int y)
  309. {
  310. return 0 <= x && x < 50 && 0 <= y && y < 50;
  311. }
  312. //前往该集合最近点,适用于点较少,2、3个左右
  313. //ToNear暂时没有用到
  314. bool ToNear(IStudentAPI& api, std::vector<std::pair<int32_t, int32_t>>set)
  315. {
  316. if (set.empty())return false;//空集或者去不了
  317. int size = set.size();
  318. int flag = -1;
  319. int path_len = 50 * 50;
  320. std::vector<std::pair<int32_t, int32_t>> path;
  321. auto self = api.GetSelfInfo();
  322. for (int i = 0; i < size; i++)
  323. {
  324. if (GetManhattanDistance(set[i].first, set[i].second,api.GridToCell(self->x),api.GridToCell(self->y)>10))continue;
  325. auto cur_path = Astar(api, set[i].first, set[i].second, NodeMap);
  326. if (cur_path.empty())
  327. {
  328. api.Print("当前路径为空?");
  329. continue;
  330. }
  331. else api.Print(fmt::format("{}", cur_path.size()));
  332. if (cur_path.size() < path_len && cur_path.size() != 0)
  333. {
  334. path_len = cur_path.size();
  335. path = cur_path;
  336. flag = i;
  337. }
  338. }
  339. if (flag == -1)
  340. {
  341. api.Print("无法到达");
  342. return false;
  343. }
  344. else
  345. {
  346. api.Print(fmt::format("{}", path.size()));
  347. if (path.size() > 1)
  348. {
  349. moveinto(api, path[1].first, path[1].second);
  350. api.Print("path>1");
  351. return true;
  352. }
  353. else
  354. {
  355. moveinto(api, path[0].first, path[0].second);
  356. api.Print("path=1");
  357. return true;
  358. }
  359. }
  360. }
  361. //焦神算法,寻找当前最近目标
  362. //寻找最近的目标点
  363. bool Adjacent(pair<int, int>point1, pair<int, int>point2)
  364. {
  365. int x = abs(point1.first - point2.first);
  366. int y = abs(point1.second - point2.second);
  367. return (x + y == 1);
  368. }//是否是四周的格
  369. //尚未考虑把学生当做障碍物,但是应该不影响后续操作
  370. pair<int, int> FindNearestPoint(int x, int y, int target[50][50])//实际还得一个参量:issTudent
  371. {
  372. int Accept[50][50] = { 0 };
  373. pair<int, int>StartPoint = { x,y };//举例子用
  374. //初始化PointList
  375. vector<vector<pair<int, int>>>PointList(400, vector<pair<int, int>>(400, { 99,99 }));
  376. int distance = 0;
  377. PointList[0][0] = StartPoint;
  378. Accept[StartPoint.first][StartPoint.second] = 1;//起点已经加入
  379. int flag = 1;//循环条件
  380. pair<int, int>DestPoint;//寻找终点,{0,0}只是初始化,无意义
  381. /*Print(PointList[0][0]);*/
  382. while (flag)//对第一层循环
  383. {
  384. int temp = distance;
  385. distance++;
  386. int stop = 0;
  387. int cnt = 0;
  388. for (int i = 0; i < 400 && !stop; i++)//对第二层循环
  389. {
  390. if (PointList[temp][i].first == 99)
  391. {
  392. stop = 1;
  393. break;
  394. }
  395. else
  396. {
  397. pair<int, int>tempPoint = PointList[temp][i];
  398. int x = tempPoint.first, y = tempPoint.second;
  399. for (int deltax = -1; deltax <= 1; deltax++)
  400. {
  401. for (int deltay = -1; deltay <= 1; deltay++)
  402. {
  403. pair<int, int>tempPoint2(x + deltax, y + deltay);
  404. if (Adjacent(tempPoint2, tempPoint))
  405. {
  406. int x2 = tempPoint2.first, y2 = tempPoint2.second;
  407. if (target[tempPoint2.first][tempPoint2.second] == 1)
  408. {
  409. flag = 0;
  410. DestPoint = tempPoint2;
  411. break;
  412. }
  413. else if ((0 <= x2 && x2 < 50 && 0 <= y2 && y2 < 50 && obstacleMap[x2][y2] == 0 && Accept[x2][y2] == 0))
  414. {
  415. Accept[tempPoint2.first][tempPoint2.second] = 1;
  416. PointList[distance][cnt] = tempPoint2;
  417. cnt++;
  418. }
  419. }
  420. if (!flag) break;
  421. }
  422. if (!flag) break;
  423. }
  424. }
  425. }
  426. }
  427. return DestPoint;
  428. }
  429. //获取当前状态下的技能冷却时间,返回一个一维数组指针
  430. double* skillTime(IStudentAPI& api)
  431. {
  432. auto student = api.GetSelfInfo();
  433. string string_skillTime[3] = { "" };
  434. for (const auto& time : student->timeUntilSkillAvailable)
  435. {
  436. int i = 0;
  437. string_skillTime[i] += std::to_string(time);
  438. i++;
  439. }
  440. double* double_skillTime = new double[3];
  441. double* point = double_skillTime;
  442. stringstream ss;
  443. for (int i = 0; i < 3; i++)
  444. {
  445. ss << string_skillTime[i];
  446. ss >> double_skillTime[i];
  447. }
  448. return(double_skillTime);
  449. }
  450. //检测是否有捣蛋鬼靠近,是否大于危险阈值,目前设置的3
  451. bool IfDanger(IStudentAPI& api)
  452. {
  453. auto self = api.GetSelfInfo();
  454. auto alert = self->dangerAlert;
  455. auto trickers = api.GetTrickers();
  456. if (!trickers.empty())return true;
  457. else
  458. {
  459. if (alert > 4)return true;
  460. else return false;
  461. }
  462. }
  463. void GoToClassAndLearning(IStudentAPI& api)
  464. {
  465. int x = api.GridToCell(api.GetSelfInfo()->x), y = api.GridToCell(api.GetSelfInfo()->y);
  466. int target[50][50] = { 0 };
  467. for (int i = 0; i < 10; i++)
  468. {
  469. if (LearningProcess[i] == 0) target[ClassroomPosition[i].first][ClassroomPosition[i].second] = 1;
  470. }
  471. pair<int, int>NearestClassRoom = FindNearestPoint(x, y, target);
  472. int index = (find(ClassroomPosition.begin(), ClassroomPosition.end(), pair<int, int>(NearestClassRoom.first, NearestClassRoom.second)) - ClassroomPosition.begin());
  473. auto path = Astar(api, NearestClassRoom.first, NearestClassRoom.second, NodeMap);
  474. int learning = 0;
  475. while (path.size() >= 2 && !learning)
  476. {
  477. if (Map[path[1].first][path[1].second] != THUAI6::PlaceType::Window)
  478. {
  479. moveinto(api, path[1].first, path[1].second);
  480. if (ArriveAt(api, path[1].first, path[1].second))
  481. {
  482. path.erase(path.begin());
  483. }
  484. }
  485. else//下一步就是窗户了
  486. {
  487. api.SkipWindow();
  488. path.erase(path.begin());
  489. path.erase(path.begin());
  490. }
  491. if (path.size() <= 2)
  492. {
  493. learning = 1;
  494. break;
  495. }
  496. }
  497. if (learning)
  498. {
  499. api.StartLearning();
  500. }
  501. //*****************************Try
  502. if (api.GetClassroomProgress(NearestClassRoom.first, NearestClassRoom.second) >= 10000000)
  503. {
  504. for (int indexnow = 0; indexnow < 4; indexnow++)
  505. {
  506. if (api.GetStudents()[indexnow]->playerID != myId(api))
  507. {
  508. api.SendMessage(indexnow, fmt::format("{}", index));
  509. }
  510. }
  511. LearningProcess[index] = 1;
  512. api.EndAllAction();
  513. }
  514. //*****************************Try
  515. }
  516. int myId(IStudentAPI& api)
  517. {
  518. return api.GetSelfInfo()->playerID;
  519. }
  520. bool OpenTheGate()
  521. {
  522. return(accumulate(LearningProcess.begin(), LearningProcess.end(), 0) >= 7);
  523. }
  524. bool OpenTheHiddenGate()
  525. {
  526. return(accumulate(LearningProcess.begin(), LearningProcess.end(), 0) >= 3);
  527. }
  528. void GoToGateAndGraduate(IStudentAPI& api)
  529. {
  530. if (!(OpenTheGate())) return;
  531. else
  532. {
  533. int x = api.GridToCell(api.GetSelfInfo()->x), y = api.GridToCell(api.GetSelfInfo()->y);
  534. int target[50][50] = { 0 };
  535. for (int i = 0; i < 2; i++)
  536. {
  537. target[Gate[i].first][Gate[i].second] = 1;
  538. }
  539. pair<int, int>NearestGate = FindNearestPoint(x, y, target);
  540. auto path = Astar(api, NearestGate.first, NearestGate.second, NodeMap);
  541. int Graduate = 0;
  542. while (path.size() >= 2 && !Graduate)
  543. {
  544. if (Map[path[1].first][path[1].second] != THUAI6::PlaceType::Window)
  545. {
  546. moveinto(api, path[1].first, path[1].second);
  547. if (ArriveAt(api, path[1].first, path[1].second))
  548. {
  549. path.erase(path.begin());
  550. }
  551. }
  552. else//下一步就是窗户了
  553. {
  554. api.SkipWindow();
  555. path.erase(path.begin());
  556. path.erase(path.begin());
  557. }
  558. if (path.size() <= 2)
  559. {
  560. Graduate = 1;
  561. break;
  562. }
  563. }
  564. if (Graduate)
  565. {
  566. api.Graduate();
  567. api.StartOpenGate();
  568. }
  569. }
  570. }
  571. void MoveToThePlace(IStudentAPI& api, pair<int, int>Terminal)
  572. {
  573. int x = api.GridToCell(api.GetSelfInfo()->x), y = api.GridToCell(api.GetSelfInfo()->y);
  574. auto path = Astar(api, Terminal.first, Terminal.second, NodeMap);
  575. while (path.size() >= 2)
  576. {
  577. if (Map[path[1].first][path[1].second] != THUAI6::PlaceType::Window)
  578. {
  579. moveinto(api, path[1].first, path[1].second);
  580. if (isInsideofcell(api, path[1].first, path[1].second))
  581. {
  582. path.erase(path.begin());
  583. }
  584. }
  585. else
  586. {
  587. api.SkipWindow();
  588. path.erase(path.begin());
  589. path.erase(path.begin());
  590. /*api.EndAllAction();*/
  591. }
  592. if (path.size() <= 2)
  593. {
  594. moveinto(api, path[1].first, path[1].second);
  595. /*api.EndAllAction();*/
  596. break;
  597. }
  598. else if (path.size() == 1)
  599. {
  600. moveinto(api, path[0].first, path[0].second);
  601. }
  602. else break;
  603. }
  604. }
  605. bool ToNearestGrass(IStudentAPI& api)
  606. {
  607. auto self = api.GetSelfInfo();
  608. int xcell = api.GridToCell(self->x);
  609. int ycell = api.GridToCell(self->y);
  610. int GrassChecked[50][50] = { 0 };
  611. auto students = api.GetStudents();
  612. for (int i = 0; i < 49; i++)
  613. {
  614. for (int j = 0; j < 49; j++)
  615. {
  616. GrassChecked[i][j] = Grass[i][j];
  617. }
  618. }
  619. for (auto stu : students)
  620. {
  621. if (stu->playerID == self->playerID)continue;
  622. else GrassChecked[api.GridToCell(stu->x)][api.GridToCell(stu->y)] = 0;
  623. }
  624. auto target = FindNearestPoint(xcell, ycell, Grass);
  625. if (target.second==0 && target.first==0)
  626. {
  627. return false;
  628. }
  629. api.Print("去草丛");
  630. auto path = Astar(api, target.first, target.second, NodeMap);
  631. while (path.size() >= 2)
  632. {
  633. if (Map[path[1].first][path[1].second] != THUAI6::PlaceType::Window)
  634. {
  635. moveinto(api, path[1].first, path[1].second);
  636. if (ArriveAt(api, path[1].first, path[1].second))
  637. {
  638. path.erase(path.begin());
  639. }
  640. }
  641. else//下一步就是窗户了
  642. {
  643. api.SkipWindow();
  644. path.erase(path.begin());
  645. path.erase(path.begin());
  646. }
  647. }
  648. return true;
  649. }
  650. bool RunAway(IStudentAPI& api)
  651. {
  652. auto self = api.GetSelfInfo();
  653. auto trickers = api.GetTrickers();
  654. if (trickers.empty())//看不见,盲去草丛
  655. {
  656. api.Print("runaway!");
  657. ToNearestGrass(api);
  658. return true;
  659. }
  660. else//看的见捣蛋鬼,更新捣蛋鬼信息,建议astar增加一个参数obstacles,用vector储存当前别去的地方
  661. {
  662. auto tx = trickers[0]->x;
  663. auto ty = trickers[0]->y;
  664. TrickerInfo.x = tx;
  665. TrickerInfo.y = ty;
  666. TrickerInfo.time = api.GetFrameCount();
  667. api.Print("runaway!");
  668. ToNearestGrass(api);
  669. return true;
  670. }
  671. }
  672. //检测是否有墙
  673. bool IfWall(IStudentAPI& api, int x1, int y1, int x2, int y2)//默认输入的是实际坐标(*1000了的那个)
  674. {
  675. int deltaX = x1 - x2;
  676. int deltaY = y1 - y2;
  677. int divide = std::max(std::abs(deltaX), std::abs(deltaY)) / 100;
  678. if (divide == 0)
  679. {
  680. return isWall((int)x1 / 1000, (int)y1 / 1000);
  681. }
  682. else
  683. {
  684. double dx = deltaX / divide;
  685. double dy = deltaY / divide;
  686. double myX = double(x2);
  687. double myY = double(y2);
  688. for (int i = 0; i < divide; i++)
  689. {
  690. myX += dx;
  691. myY += dy;
  692. if (isWall(myX, myY))
  693. return true;
  694. }
  695. return false;
  696. }
  697. }
  698. //获取BGM警戒信息,返回一个int值(警戒半径/二者距离)
  699. int getAlert(IStudentAPI& api)
  700. {
  701. auto self = api.GetSelfInfo();
  702. int alertData = self->dangerAlert;
  703. return alertData;
  704. }
  705. void StraightAStudentSkill(IStudentAPI& api)
  706. {
  707. int x = api.GridToCell(api.GetSelfInfo()->x), y = api.GridToCell(api.GetSelfInfo()->y);
  708. int target[50][50] = { 0 };
  709. for (int i = 0; i < 10; i++)
  710. {
  711. if (LearningProcess[i] == 0) target[ClassroomPosition[i].first][ClassroomPosition[i].second] = 1;
  712. }
  713. pair<int, int>NearestClassRoom = FindNearestPoint(x, y, target);
  714. int index = (find(ClassroomPosition.begin(), ClassroomPosition.end(), pair<int, int>(NearestClassRoom.first, NearestClassRoom.second)) - ClassroomPosition.begin());
  715. auto path = Astar(api, NearestClassRoom.first, NearestClassRoom.second, NodeMap);
  716. double* skilltime = skillTime(api);
  717. if ((path.size() <= 2) && (!skilltime[0]))
  718. api.UseSkill(0);
  719. }
  720. };
  721. class MyTrickerFunction
  722. {
  723. public:
  724. //移动函数
  725. bool isLeftoutofcell(ITrickerAPI& api, int32_t cellx, int32_t celly) {
  726. if (api.GetSelfInfo()->y - 400 < celly * 1000) return true;
  727. else return false;
  728. }//判断是否在目标格左边一定范围
  729. bool isRightoutofcell(ITrickerAPI& api, int32_t cellx, int32_t celly) {
  730. if (api.GetSelfInfo()->y + 400 > (celly + 1) * 1000) return true;
  731. else return false;
  732. }//判断是否在目标格右边一定范围
  733. bool isUpoutofcell(ITrickerAPI& api, int32_t cellx, int32_t celly) {
  734. if (api.GetSelfInfo()->x - 400 < cellx * 1000) return true;
  735. else return false;
  736. }//判断是否在目标格上边一定范围
  737. bool isDownoutofcell(ITrickerAPI& api, int32_t cellx, int32_t celly) {
  738. if (api.GetSelfInfo()->x + 400 > (cellx + 1) * 1000) return true;
  739. else return false;
  740. }//判断是否在目标格下边一定范围
  741. bool isInsideofcellx(ITrickerAPI& api, int32_t cellx) {
  742. if (!isUpoutofcell(api, cellx, -1) && !isDownoutofcell(api, cellx, -1)) return true;
  743. else return false;
  744. }
  745. //判断人是否整个在某行内
  746. bool isInsideofcelly(ITrickerAPI& api, int32_t celly) {
  747. if (!isLeftoutofcell(api, -1, celly) && !isRightoutofcell(api, -1, celly)) return true;
  748. else return false;
  749. }
  750. //判断人是否整个在某列内
  751. bool isInsideofcell(ITrickerAPI& api, int32_t cellx, int32_t celly) {
  752. if (isInsideofcellx(api, cellx) && isInsideofcelly(api, celly)) return true;
  753. else return false;
  754. }
  755. //判断人是否整个在某格子内
  756. void moveinto(ITrickerAPI& api, int32_t cellx, int32_t celly)
  757. {
  758. if (!isInsideofcell(api, cellx, celly)) {
  759. if (isLeftoutofcell(api, cellx, celly)) api.MoveRight(50);
  760. else if (isRightoutofcell(api, cellx, celly)) api.MoveLeft(50);
  761. if (isUpoutofcell(api, cellx, celly)) api.MoveDown(50);
  762. else if (isDownoutofcell(api, cellx, celly))
  763. api.MoveUp(50);
  764. }
  765. }
  766. //使人的碰撞箱进入一整格内,避免卡死等问题:只用于人在该格子附近的微调!
  767. bool ArriveAt(ITrickerAPI& api, int32_t desX, int32_t desY)
  768. {
  769. auto self = api.GetSelfInfo();
  770. int32_t sx = self->x;
  771. int32_t sy = self->y;
  772. if (std::abs(sx - 1000 * desX - 500) + std::abs(sy - 1000 * desY - 500) < 800) return true;
  773. return false;
  774. }
  775. //Astar算法
  776. class Compare
  777. {
  778. public:
  779. Compare() {};
  780. bool operator()(const Node& node1, const Node& node2) const
  781. {
  782. return node1.Fcost > node2.Fcost;
  783. }
  784. }compare_by_cost;
  785. std::vector<std::pair<int32_t, int32_t>>Astar(ITrickerAPI& api, int destX, int destY, Node Nodemap[50][50])
  786. {
  787. Node nodemap[50][50];
  788. for (int i = 0; i < 50; i++)
  789. {
  790. for (int j = 0; j < 50; j++)
  791. {
  792. nodemap[i][j] = Nodemap[i][j];
  793. }
  794. }
  795. api.Print("Astar被调用");
  796. std::priority_queue<Node, std::vector<Node>, Compare> openList;
  797. auto self = api.GetSelfInfo();
  798. if (self->x / 1000 == destX && self->y / 1000 == destY)
  799. {
  800. std::vector<std::pair<int, int>> path{ std::pair<int, int>(destX,destY) };
  801. return path;//返回自身坐标是已经到了
  802. }
  803. auto& startNode = nodemap[self->x / 1000][self->y / 1000];
  804. startNode.Gcost = 0;
  805. startNode.Hcost = GetManhattanDistance(self->x / 1000, self->y / 1000, destX, destY);
  806. startNode.Fcost = GetManhattanDistance(self->x / 1000, self->y / 1000, destX, destY);
  807. startNode.flag = 1;
  808. startNode.Parent = nullptr;
  809. auto& endNode = nodemap[destX][destY];
  810. endNode.Gcost = 0; endNode.Hcost = 0, endNode.Fcost = 0; endNode.flag = 0; endNode.Parent = nullptr;
  811. openList.push(startNode);
  812. Node* currentNode = &startNode;
  813. while (!openList.empty() && !(currentNode->x == destX && currentNode->y == destY))
  814. {
  815. currentNode->flag = 2;
  816. openList.pop();
  817. for (int i = -1; i <= 1; i++)
  818. {
  819. for (int j = -1; j <= 1; j++)
  820. {
  821. if (!(i * j) && !(i == 0 && j == 0))
  822. {
  823. if (isInMap(currentNode->x + i, currentNode->y + j))
  824. {
  825. Node* logicNode = &nodemap[currentNode->x + i][currentNode->y + j];
  826. if (logicNode->x == destX && logicNode->y == destY)
  827. {
  828. logicNode->flag = 2;
  829. logicNode->Parent = currentNode;
  830. currentNode = logicNode;
  831. goto here;
  832. }
  833. else if (!isWall((*logicNode).x, (*logicNode).y) && logicNode->flag == 0)
  834. {
  835. logicNode->Gcost = currentNode->Gcost + 1;
  836. logicNode->flag = 1;
  837. logicNode->Parent = currentNode;
  838. logicNode->Hcost = GetManhattanDistance(logicNode->x, logicNode->y, destX, destY);
  839. logicNode->Fcost = logicNode->Gcost + logicNode->Hcost;
  840. openList.push(*logicNode);
  841. }
  842. }
  843. else
  844. {
  845. continue;
  846. }
  847. }
  848. }
  849. }
  850. if (!openList.empty())
  851. {
  852. currentNode = &nodemap[openList.top().x][openList.top().y];
  853. }
  854. }
  855. here:
  856. if (currentNode->x == self->x / 1000 && currentNode->y == self->y / 1000)
  857. {
  858. std::vector<std::pair<int, int>> path;
  859. api.Print("到不了,返回空路径");
  860. return path;//返回空路径
  861. }
  862. auto path = GetPath(currentNode);
  863. api.Print(fmt::format("Astar返回值:path.size()={}", path.size()));
  864. return path;
  865. }
  866. int32_t GetManhattanDistance(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
  867. {
  868. return abs(x1 - x2) + abs(y1 - y2);
  869. }
  870. std::vector<std::pair<int, int>> GetPath(Node* endNode)
  871. {
  872. std::vector<std::pair<int, int>> path;
  873. Node* node = endNode;
  874. while (node->Parent)
  875. {
  876. path.push_back(std::pair<int, int>(node->x, node->y));
  877. node = node->Parent;
  878. }
  879. path.push_back(std::pair<int, int>(node->x, node->y)); // 添加起点
  880. std::reverse(path.begin(), path.end()); // 路径反转
  881. return path;
  882. }
  883. bool isWall(int x, int y)
  884. {
  885. return Map.at(x).at(y) == THUAI6::PlaceType::Wall;
  886. }
  887. bool isInMap(int x, int y)
  888. {
  889. return 0 <= x && x < 50 && 0 <= y && y < 50;
  890. }
  891. //前往该集合最近点,适用于点较少,2、3个左右
  892. bool ToNear(ITrickerAPI& api, std::vector<std::pair<int32_t, int32_t>>set)
  893. {
  894. if (set.empty())return false;//空集或者去不了
  895. int size = set.size();
  896. int flag = -1;
  897. int path_len = 50 * 50;
  898. std::vector<std::pair<int32_t, int32_t>> path;
  899. for (int i = 0; i < size; i++)
  900. {
  901. auto cur_path = Astar(api, set[i].first, set[i].second, NodeMap);
  902. if (cur_path.empty())
  903. {
  904. api.Print("当前路径为空?");
  905. continue;
  906. }
  907. else api.Print(fmt::format("{}", cur_path.size()));
  908. if (cur_path.size() < path_len && cur_path.size() != 0)
  909. {
  910. path_len = cur_path.size();
  911. path = cur_path;
  912. flag = i;
  913. }
  914. }
  915. if (flag == -1)
  916. {
  917. api.Print("无法到达");
  918. return false;
  919. }
  920. else
  921. {
  922. api.Print(fmt::format("{}", path.size()));
  923. if (path.size() > 1)
  924. {
  925. moveinto(api, path[1].first, path[1].second);
  926. api.Print("path>1");
  927. return true;
  928. }
  929. else
  930. {
  931. moveinto(api, path[0].first, path[0].second);
  932. api.Print("path=1");
  933. return true;
  934. }
  935. }
  936. }
  937. //焦神算法,寻找当前最近目标
  938. //寻找最近的目标点
  939. bool Adjacent(pair<int, int>point1, pair<int, int>point2)
  940. {
  941. int x = abs(point1.first - point2.first);
  942. int y = abs(point1.second - point2.second);
  943. return (x + y == 1);
  944. }//是否是四周的格
  945. pair<int, int> FindNearestPoint(int x, int y, int target[50][50])//实际还得一个参量:issTudent
  946. {
  947. int Accept[50][50] = { 0 };
  948. pair<int, int>StartPoint = { x,y };//举例子用
  949. //初始化PointList
  950. vector<vector<pair<int, int>>>PointList(400, vector<pair<int, int>>(400, { 99,99 }));
  951. int distance = 0;
  952. PointList[0][0] = StartPoint;
  953. Accept[StartPoint.first][StartPoint.second] = 1;//起点已经加入
  954. int flag = 1;//循环条件
  955. pair<int, int>DestPoint;//寻找终点,{0,0}只是初始化,无意义
  956. /*Print(PointList[0][0]);*/
  957. while (flag)//对第一层循环
  958. {
  959. int temp = distance;
  960. distance++;
  961. int stop = 0;
  962. int cnt = 0;
  963. for (int i = 0; i < 400 && !stop; i++)//对第二层循环
  964. {
  965. if (PointList[temp][i].first == 99)
  966. {
  967. stop = 1;
  968. break;
  969. }
  970. else
  971. {
  972. pair<int, int>tempPoint = PointList[temp][i];
  973. int x = tempPoint.first, y = tempPoint.second;
  974. for (int deltax = -1; deltax <= 1; deltax++)
  975. {
  976. for (int deltay = -1; deltay <= 1; deltay++)
  977. {
  978. pair<int, int>tempPoint2(x + deltax, y + deltay);
  979. if (Adjacent(tempPoint2, tempPoint))
  980. {
  981. int x2 = tempPoint2.first, y2 = tempPoint2.second;
  982. if (target[tempPoint2.first][tempPoint2.second] == 1)
  983. {
  984. flag = 0;
  985. DestPoint = tempPoint2;
  986. break;
  987. }
  988. else if ((0 <= x2 && x2 < 50 && 0 <= y2 && y2 < 50 && obstacleMap[x2][y2] == 0 && Accept[x2][y2] == 0))
  989. {
  990. Accept[tempPoint2.first][tempPoint2.second] = 1;
  991. PointList[distance][cnt] = tempPoint2;
  992. cnt++;
  993. }
  994. }
  995. if (!flag) break;
  996. }
  997. if (!flag) break;
  998. }
  999. }
  1000. }
  1001. }
  1002. return DestPoint;
  1003. }
  1004. //含有翻窗的移动函数
  1005. void trickerMove(ITrickerAPI& api, int x, int y)
  1006. {
  1007. //auto path = myt.Astar(api, x, y, NodeMap);
  1008. //while (path.size() > 2)
  1009. //{
  1010. // if (Map[path[1].first][path[1].second] != THUAI6::PlaceType::Window)
  1011. // {
  1012. // myt.moveinto(api, path[1].first, path[1].second);
  1013. // if (myt.isInsideofcell(api, path[1].first, path[1].second))
  1014. // {
  1015. // path.erase(path.begin());
  1016. // }
  1017. // }
  1018. // else
  1019. // {
  1020. // api.SkipWindow();
  1021. // path.erase(path.begin());
  1022. // path.erase(path.begin());
  1023. // /*api.EndAllAction();*/
  1024. // }
  1025. //}
  1026. std::vector<std::pair<int32_t, int32_t>> path;
  1027. auto tempPath = Astar(api, x, y, NodeMap);
  1028. if (tempPath.size() > 2)
  1029. {
  1030. path.push_back({ tempPath[0].first, tempPath[0].second });
  1031. path.push_back({ tempPath[1].first, tempPath[1].second });
  1032. while (!path.empty())
  1033. {
  1034. if (path.size() > 1)
  1035. {
  1036. if (Map[path[1].first][path[1].second] != THUAI6::PlaceType::Window)
  1037. {
  1038. moveinto(api, path[1].first, path[1].second);
  1039. if (isInsideofcell(api, path[1].first, path[1].second))
  1040. {
  1041. path.erase(path.begin());
  1042. }
  1043. }
  1044. else
  1045. {
  1046. api.SkipWindow();
  1047. path.erase(path.begin());
  1048. path.erase(path.begin());
  1049. /*api.EndAllAction();*/
  1050. }
  1051. }
  1052. else
  1053. {
  1054. while (!path.empty())
  1055. {
  1056. moveinto(api, path[0].first, path[0].second);
  1057. if (isInsideofcell(api, path[0].first, path[0].second))
  1058. {
  1059. path.erase(path.begin());
  1060. }
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. //获取当前状态下的技能冷却时间,返回一个一维数组指针
  1067. double* skillTime(ITrickerAPI& api)
  1068. {
  1069. auto tricker = api.GetSelfInfo();
  1070. string string_skillTime[3] = { "" };
  1071. for (const auto& time : tricker->timeUntilSkillAvailable)
  1072. {
  1073. int i = 0;
  1074. string_skillTime[i] += std::to_string(time);
  1075. i++;
  1076. }
  1077. double* double_skillTime = new double[3];
  1078. double* point = double_skillTime;
  1079. stringstream ss;
  1080. for (int i = 0; i < 3; i++)
  1081. {
  1082. ss << string_skillTime[i];
  1083. ss >> double_skillTime[i];
  1084. }
  1085. return(double_skillTime);
  1086. }
  1087. //找到视野中最近的学生并追击
  1088. void chaseStudents(ITrickerAPI& api)
  1089. {
  1090. api.Print("Chasing!");
  1091. int temp = 1000000;
  1092. bool StudentAddicted = true;
  1093. std::pair<int32_t, int32_t> nearestStudentPositon;
  1094. auto self = api.GetSelfInfo();
  1095. auto students = api.GetStudents();
  1096. //找到最近的学生
  1097. for (int i = 0; i < students.size(); i++)
  1098. {
  1099. //判断不是老师才进行追击
  1100. /* if (students[i]->studentType != THUAI6::StudentType::Teacher)
  1101. {*/
  1102. if (students[i]->playerState != THUAI6::PlayerState::Addicted && ((abs(students[i]->x - self->x) + abs(students[i]->y - self->y)) < temp))
  1103. {
  1104. nearestStudentPositon.first = students[i]->x; nearestStudentPositon.second = students[i]->y;
  1105. temp = abs(students[i]->x - self->x) + abs(students[i]->y - self->y);
  1106. StudentAddicted = false;
  1107. }
  1108. if (!StudentAddicted)
  1109. {
  1110. if (students[i]->playerState != THUAI6::PlayerState::Addicted && ((abs(students[i]->x - self->x) + abs(students[i]->y - self->y)) < temp))
  1111. {
  1112. nearestStudentPositon.first = students[i]->x; nearestStudentPositon.second = students[i]->y;
  1113. temp = abs(students[i]->x - self->x) + abs(students[i]->y - self->y);
  1114. StudentAddicted = false;
  1115. }
  1116. // }
  1117. }
  1118. }
  1119. /* if (students.size() == 1 && students[0]->studentType == THUAI6::StudentType::Teacher)
  1120. {
  1121. moveWithoutGoal(api);
  1122. }*/
  1123. int targetX = api.GridToCell(nearestStudentPositon.first);
  1124. int targetY = api.GridToCell(nearestStudentPositon.second);
  1125. trickerMove(api, targetX, targetY);
  1126. auto path = Astar(api, targetX, targetY, NodeMap);
  1127. if (path.size() < 3)
  1128. {
  1129. attackStudents(api);
  1130. }
  1131. }
  1132. //攻击函数
  1133. void attackStudents(ITrickerAPI& api)
  1134. {
  1135. int temp;
  1136. std::pair<int32_t, int32_t> nearestStudentPositon;
  1137. auto self = api.GetSelfInfo();
  1138. auto students = api.GetStudents();
  1139. //找到最近的学生
  1140. api.Print("Attacking");
  1141. for (int i = 0; i < students.size(); i++)
  1142. {
  1143. if (i == 0)
  1144. {
  1145. temp = abs(students[i]->x - self->x) + abs(students[i]->y - self->y);
  1146. nearestStudentPositon.first = students[i]->x; nearestStudentPositon.second = students[i]->y;
  1147. }
  1148. else if (((abs(students[i]->x - self->x) + abs(students[i]->y - self->y)) < temp))
  1149. {
  1150. nearestStudentPositon.first = students[i]->x; nearestStudentPositon.second = students[i]->y;
  1151. }
  1152. }
  1153. double attackangle = atan2((nearestStudentPositon.second - self->y), (nearestStudentPositon.first - self->x));
  1154. api.Attack(attackangle);
  1155. }
  1156. //随机挑选教室前往,并在附近停下来
  1157. void moveWithoutGoal(ITrickerAPI& api)
  1158. {
  1159. api.Print("MovingToClass");
  1160. //随机找一件教室前往
  1161. int targetX, targetY;
  1162. /*srand(time(0));
  1163. int dest = rand() % ClassroomPosition.size();
  1164. targetX = ClassroomPosition[dest].first;
  1165. targetY = ClassroomPosition[dest].second;*/
  1166. for (int i = 0; i < ClassroomPosition.size(); i++)
  1167. {
  1168. if (api.GetClassroomProgress(ClassroomPosition[i].first, ClassroomPosition[i].second) == -1)
  1169. {
  1170. targetX = ClassroomPosition[i].first; targetY = ClassroomPosition[i].second;
  1171. }
  1172. }
  1173. trickerMove(api, targetX, targetY);
  1174. }
  1175. //选择最近的教室前往
  1176. void goToNearestClassRoom(ITrickerAPI& api)
  1177. {
  1178. int temp; int targetX, targetY;
  1179. auto self = api.GetSelfInfo();
  1180. std::pair<int32_t, int32_t> nearestRoomPositon;
  1181. int CurX = api.GridToCell(self->x); int CurY = api.GridToCell(self->y);
  1182. //找到附近的教室
  1183. for (int i = 0; i < ClassroomPosition.size(); i++)
  1184. {
  1185. if (i == 0)
  1186. {
  1187. temp = abs(ClassroomPosition[i].first - CurX) + abs(ClassroomPosition[i].second - CurY);
  1188. targetX = ClassroomPosition[i].first; targetY = ClassroomPosition[i].second;
  1189. }
  1190. else if ((abs(ClassroomPosition[i].first - CurX) + abs(ClassroomPosition[i].second - CurY)) < temp)
  1191. {
  1192. temp = (abs(ClassroomPosition[i].first - CurX) + abs(ClassroomPosition[i].second - CurY));
  1193. targetX = ClassroomPosition[i].first; targetY = ClassroomPosition[i].second;
  1194. }
  1195. }
  1196. trickerMove(api, targetX, targetY);
  1197. }
  1198. //有学生在附近但不在视野内,就探寻附近的草地或空地
  1199. void trickerInAlert(ITrickerAPI& api)
  1200. {
  1201. api.Print("Alert!");
  1202. auto self = api.GetSelfInfo();
  1203. int x = api.GridToCell(self->x);
  1204. int y = api.GridToCell(self->y);
  1205. int targetX = 0; int targetY = 0;
  1206. //找最近的门或者草地或者空地,如果人在空地,就找草地,如果人在
  1207. if (x > 2 && y > 2)
  1208. {
  1209. for (int i = -2; i++; i < 3)
  1210. {
  1211. for (int j = -2; j++; j < 3)
  1212. {
  1213. //自身在空地,探寻附近是否有草地
  1214. if (api.GetSelfInfo()->place == THUAI6::PlaceType::Land)
  1215. {
  1216. if (api.GetFullMap()[x + i][x + j] == THUAI6::PlaceType::Grass)
  1217. {
  1218. targetX = x + i; targetY = y + j;
  1219. }
  1220. }
  1221. //自身在草地,探寻附近是否有空地
  1222. if (api.GetSelfInfo()->place == THUAI6::PlaceType::Grass)
  1223. {
  1224. if (api.GetFullMap()[x + i][x + j] == THUAI6::PlaceType::Land)
  1225. {
  1226. targetX = x + i; targetY = y + j;
  1227. }
  1228. }
  1229. }
  1230. }
  1231. }
  1232. //前往最近的空地或者是草丛
  1233. if (targetX != 0 && targetY != 0)
  1234. {
  1235. trickerMove(api, targetX, targetY);
  1236. }
  1237. else { goToNearestClassRoom(api); }
  1238. }
  1239. //获取警戒范围内的学生信息,返回一个int值(警戒半径/可被发现的最近的学生的距离)
  1240. int getDesire(ITrickerAPI& api)
  1241. {
  1242. auto self = api.GetSelfInfo();
  1243. int desireData = self->trickDesire;
  1244. return desireData;
  1245. }
  1246. //获取警戒范围内学生学习的声音,返回一个int值(警戒半径*学习进度百分比)/ 二者距离)
  1247. int gerVolume(ITrickerAPI& api)
  1248. {
  1249. auto self = api.GetSelfInfo();
  1250. int volumeData;
  1251. return volumeData;
  1252. }
  1253. //用于tricker去打开箱子,返回值:0->附近没有箱子;1->朝可开箱子前进中;2->已到达可开箱子附近
  1254. int findChestClosed(ITrickerAPI& api)
  1255. {
  1256. api.Print("findChest");
  1257. auto self = api.GetSelfInfo();
  1258. int propsNum = 0;
  1259. int targetX = 0, targetY = 0;
  1260. for (int i = 0; i < 3; i++)
  1261. {
  1262. if (api.GetSelfInfo()->props[i] != THUAI6::PropType::NullPropType)
  1263. propsNum++;
  1264. }
  1265. int x = api.GridToCell(self->x);
  1266. int y = api.GridToCell(self->y);
  1267. api.Print(fmt::format("{}", api.GetSelfInfo()->props.size()));
  1268. if (propsNum <= 1)
  1269. {
  1270. for (int j = -2; j < 3; j++)
  1271. {
  1272. for (int i = -2; i < 3; i++)
  1273. {
  1274. if (!isInMap(x + i, y + j))
  1275. {
  1276. continue;
  1277. }
  1278. api.Print(fmt::format("{},{}", x, y));
  1279. api.Print(fmt::format("({},{}),{}", x + i, y + j, THUAI6::placeTypeDict[Map[x + i][y + j]]));
  1280. if (Map[x + i][y + j] == THUAI6::PlaceType::Chest)
  1281. {
  1282. api.Print("ChestFound");
  1283. if (api.GetChestProgress(x + i, y + j) == 10000000);
  1284. else
  1285. {
  1286. targetX = x + i; targetY = y + j;
  1287. api.Print("ChestChecked");
  1288. }
  1289. }
  1290. }
  1291. }
  1292. }
  1293. if (targetX != 0 || targetY != 0)
  1294. {
  1295. auto tempPath = Astar(api, targetX, targetY, NodeMap);
  1296. std::vector<std::pair<int32_t, int32_t>> path;
  1297. if (tempPath.size() > 2)
  1298. {
  1299. api.Print("Moving");
  1300. path.push_back({ tempPath[0].first, tempPath[0].second });
  1301. path.push_back({ tempPath[1].first, tempPath[1].second });
  1302. while (!path.empty())
  1303. {
  1304. api.Print("GoToChest");
  1305. moveinto(api, path[0].first, path[0].second);
  1306. if (isInsideofcell(api, path[0].first, path[0].second))
  1307. {
  1308. path.erase(path.begin());
  1309. }
  1310. }
  1311. return 1;
  1312. }
  1313. else return 2;
  1314. }
  1315. return 0;
  1316. }
  1317. //捡道具
  1318. void trickerPickProp(ITrickerAPI& api)
  1319. {
  1320. if (api.GetProps().size() != 0)
  1321. {
  1322. api.Print("OpenChest");
  1323. auto props = api.GetProps();
  1324. api.PickProp(props[0]->type);
  1325. api.PickProp(props[1]->type);
  1326. }
  1327. }
  1328. };
  1329. class StudentCommunication
  1330. {
  1331. public:
  1332. pair<int, int>ExtractNum2(string& str)
  1333. {
  1334. int index = str.find(',');
  1335. string str1(str.begin(), str.begin() + index);
  1336. string str2(str.begin() + index + 1, str.end());
  1337. int x1 = atoi(str1.c_str()), x2 = atoi(str2.c_str());
  1338. return pair<int, int>(x1, x2);
  1339. }
  1340. int ExtractNum1(string& str)
  1341. {
  1342. return atoi(str.c_str());
  1343. }
  1344. }StuCom;
  1345. extern const bool asynchronous = true;
  1346. // 选手需要依次将player0到player4的职业在这里定义
  1347. extern const std::array<THUAI6::StudentType, 4> studentType = {
  1348. THUAI6::StudentType::Athlete,
  1349. THUAI6::StudentType::StraightAStudent,
  1350. THUAI6::StudentType::StraightAStudent,
  1351. THUAI6::StudentType::Sunshine };
  1352. extern const THUAI6::TrickerType trickerType = THUAI6::TrickerType::Assassin;
  1353. //可以在AI.cpp内部声明变量与函数
  1354. void AI::play(IStudentAPI& api)
  1355. {
  1356. if (!ifchecked)
  1357. {
  1358. api.Print("check map!");
  1359. Map.reserve(50 * 50);
  1360. Map = api.GetFullMap();
  1361. for (int i = 0; i < 10; i++)
  1362. {
  1363. LearningProcess.push_back(0);
  1364. }
  1365. for (int i = 0; i < 50; i++)
  1366. {
  1367. for (int j = 0; j < 50; j++)
  1368. {
  1369. switch (Map.at(i).at(j))
  1370. {
  1371. case(THUAI6::PlaceType::Window):Windows.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; break;
  1372. case(THUAI6::PlaceType::Wall):NodeMap[i][j] = { i,j,0,0,0,2,nullptr }; obstacleMap[i][j] = 1; break;
  1373. case(THUAI6::PlaceType::NullPlaceType):NodeMap[i][j] = { i,j,0,0,0,0,nullptr };break;
  1374. case(THUAI6::PlaceType::Land):NodeMap[i][j] = { i,j,0,0,0,0,nullptr };break;
  1375. case(THUAI6::PlaceType::Grass):NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Grass[i][j] = 1; GrassPos.push_back({ i,j }); break;
  1376. case(THUAI6::PlaceType::Chest):Chest.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1377. case(THUAI6::PlaceType::Door3):Door3Position.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1378. case(THUAI6::PlaceType::Door5):Door5Position.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1379. case(THUAI6::PlaceType::Door6):Door6Position.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1380. case(THUAI6::PlaceType::Gate):Gate.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; break;
  1381. case(THUAI6::PlaceType::ClassRoom):ClassroomPosition.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,2,nullptr }; Target[i][j] = 1; obstacleMap[i][j] = 1; break;
  1382. }
  1383. }
  1384. }//弄个大地图
  1385. ifchecked = true;
  1386. }
  1387. // 公共操作
  1388. class MyFunction myf;
  1389. if (this->playerID == 0)
  1390. {
  1391. if (api.HaveMessage())
  1392. {
  1393. for (int i = 0; i < 4; i++)
  1394. {
  1395. if (i != myf.myId(api))
  1396. {
  1397. string ClassMessage = api.GetMessage().second;
  1398. int classindex=StuCom.ExtractNum1(ClassMessage);
  1399. LearningProcess[classindex] = 1;
  1400. }
  1401. }
  1402. }
  1403. auto time_now = api.GetFrameCount();
  1404. if (myf.IfDanger(api)&&time_now- RunAwayCoolTime >20)
  1405. {
  1406. RunAwayCoolTime = time_now;
  1407. myf.RunAway(api);
  1408. }
  1409. else
  1410. {
  1411. if (myf.OpenTheGate())
  1412. {
  1413. myf.GoToGateAndGraduate(api);
  1414. }
  1415. else
  1416. {
  1417. myf.GoToClassAndLearning(api);
  1418. myf.StraightAStudentSkill(api);
  1419. }
  1420. }
  1421. }
  1422. else if (this->playerID == 1)
  1423. {
  1424. if (api.HaveMessage())
  1425. {
  1426. for (int i = 0; i < 4; i++)
  1427. {
  1428. if (i != myf.myId(api))
  1429. {
  1430. string ClassMessage = api.GetMessage().second;
  1431. int classindex = StuCom.ExtractNum1(ClassMessage);
  1432. LearningProcess[classindex] = 1;
  1433. }
  1434. }
  1435. }
  1436. auto time_now = api.GetFrameCount();
  1437. if (myf.IfDanger(api) && time_now - RunAwayCoolTime > 20)
  1438. {
  1439. RunAwayCoolTime = time_now;
  1440. myf.RunAway(api);
  1441. }
  1442. else
  1443. {
  1444. if (myf.OpenTheGate())
  1445. {
  1446. myf.GoToGateAndGraduate(api);
  1447. }
  1448. else
  1449. {
  1450. myf.GoToClassAndLearning(api);
  1451. myf.StraightAStudentSkill(api);
  1452. }
  1453. }
  1454. }
  1455. else if (this->playerID == 2)
  1456. {
  1457. // 玩家2执行操作
  1458. if (api.HaveMessage())
  1459. {
  1460. for (int i = 0; i < 4; i++)
  1461. {
  1462. if (i != myf.myId(api))
  1463. {
  1464. string ClassMessage = api.GetMessage().second;
  1465. int classindex = StuCom.ExtractNum1(ClassMessage);
  1466. LearningProcess[classindex] = 1;
  1467. }
  1468. }
  1469. }
  1470. auto time_now = api.GetFrameCount();
  1471. if (myf.IfDanger(api) && time_now - RunAwayCoolTime > 20)
  1472. {
  1473. RunAwayCoolTime = time_now;
  1474. myf.RunAway(api);
  1475. }
  1476. else
  1477. {
  1478. if (myf.OpenTheGate())
  1479. {
  1480. myf.GoToGateAndGraduate(api);
  1481. }
  1482. else
  1483. {
  1484. myf.GoToClassAndLearning(api);
  1485. myf.StraightAStudentSkill(api);
  1486. }
  1487. }
  1488. }
  1489. else if (this->playerID == 3)
  1490. {
  1491. if (api.HaveMessage())
  1492. {
  1493. for (int i = 0; i < 4; i++)
  1494. {
  1495. if (i != myf.myId(api))
  1496. {
  1497. string ClassMessage = api.GetMessage().second;
  1498. int classindex = StuCom.ExtractNum1(ClassMessage);
  1499. LearningProcess[classindex] = 1;
  1500. }
  1501. }
  1502. }
  1503. auto time_now = api.GetFrameCount();
  1504. if (myf.IfDanger(api) && time_now - RunAwayCoolTime > 20)
  1505. {
  1506. RunAwayCoolTime = time_now;
  1507. myf.RunAway(api);
  1508. }
  1509. else
  1510. {
  1511. if (myf.OpenTheGate())
  1512. {
  1513. myf.GoToGateAndGraduate(api);
  1514. }
  1515. else
  1516. {
  1517. myf.GoToClassAndLearning(api);
  1518. myf.StraightAStudentSkill(api);
  1519. }
  1520. }
  1521. }
  1522. //当然可以写成if (this->playerID == 2||this->playerID == 3)之类的操作
  1523. // 公共操作
  1524. }
  1525. void AI::play(ITrickerAPI& api)
  1526. {
  1527. auto self = api.GetSelfInfo();
  1528. api.PrintSelfInfo();
  1529. MyTrickerFunction myt;
  1530. if (!ifchecked)
  1531. {
  1532. api.Print("check map!");
  1533. Map.reserve(50 * 50);
  1534. Map = api.GetFullMap();
  1535. for (int i = 0; i < 10; i++)
  1536. {
  1537. LearningProcess.push_back(0);
  1538. }
  1539. for (int i = 0; i < 50; i++)
  1540. {
  1541. for (int j = 0; j < 50; j++)
  1542. {
  1543. switch (Map.at(i).at(j))
  1544. {
  1545. case(THUAI6::PlaceType::Window):Windows.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; break;
  1546. case(THUAI6::PlaceType::Wall):obstacleMap[i][j] = 1; break;
  1547. case(THUAI6::PlaceType::NullPlaceType):;
  1548. case(THUAI6::PlaceType::Land):;
  1549. case(THUAI6::PlaceType::Grass):NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; break;
  1550. case(THUAI6::PlaceType::Chest):Chest.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1551. case(THUAI6::PlaceType::Door3):Door3Position.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1552. case(THUAI6::PlaceType::Door5):Door5Position.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1553. case(THUAI6::PlaceType::Door6):Door6Position.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; break;
  1554. case(THUAI6::PlaceType::Gate):Gate.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; break;
  1555. case(THUAI6::PlaceType::ClassRoom):ClassroomPosition.push_back({ i,j }); NodeMap[i][j] = { i,j,0,0,0,0,nullptr }; Target[i][j] = 1; obstacleMap[i][j] = 1; break;
  1556. }
  1557. }
  1558. }//弄个大地图
  1559. ifchecked = true;
  1560. }
  1561. //视野内有学生,进行追击
  1562. if (!api.GetStudents().empty())
  1563. {
  1564. api.Print("Chasing");
  1565. myt.chaseStudents(api);
  1566. }
  1567. //视野内没学生,但警惕值很高,学生就在很近的位置。
  1568. else if (self->trickDesire > 7)
  1569. {
  1570. api.Print("Alert");
  1571. myt.trickerInAlert(api);
  1572. }
  1573. //进行游走
  1574. else
  1575. {
  1576. api.Print("WithoutGoal");
  1577. myt.moveWithoutGoal(api);
  1578. }
  1579. }