Browse Source

feat(CAPI): add invisible judgement

tags/0.1.0
DragonAura 2 years ago
parent
commit
02b9398848
3 changed files with 117 additions and 132 deletions
  1. +18
    -0
      CAPI/cpp/API/src/logic.cpp
  2. +95
    -132
      CAPI/python/PyAPI/AI.py
  3. +4
    -0
      CAPI/python/PyAPI/logic.py

+ 18
- 0
CAPI/cpp/API/src/logic.cpp View File

@@ -543,6 +543,15 @@ void Logic::LoadBuffer(protobuf::MessageToClient& message)
}
case THUAI6::MessageOfObj::TrickerMessage:
{
bool flag = false;
for (int i = 0; i < item.tricker_message().buff_size(); i++)
if (Proto2THUAI6::trickerBuffTypeDict[item.tricker_message().buff(i)] == THUAI6::TrickerBuffType::Invisible)
{
flag = true;
break;
}
if (flag)
break;
if (AssistFunction::HaveView(bufferState->studentSelf->viewRange, bufferState->studentSelf->x, bufferState->studentSelf->y, item.tricker_message().x(), item.tricker_message().y(), bufferState->gameMap))
{
bufferState->trickers.push_back(Proto2THUAI6::Protobuf2THUAI6Tricker(item.tricker_message()));
@@ -689,6 +698,15 @@ void Logic::LoadBuffer(protobuf::MessageToClient& message)
}
case THUAI6::MessageOfObj::StudentMessage:
{
bool flag = false;
for (int i = 0; i < item.student_message().buff_size(); i++)
if (Proto2THUAI6::studentBuffTypeDict[item.student_message().buff(i)] == THUAI6::StudentBuffType::Invisible)
{
flag = true;
break;
}
if (flag)
break;
if (AssistFunction::HaveView(bufferState->trickerSelf->viewRange, bufferState->trickerSelf->x, bufferState->trickerSelf->y, item.student_message().x(), item.student_message().y(), bufferState->gameMap))
{
bufferState->students.push_back(Proto2THUAI6::Protobuf2THUAI6Student(item.student_message()));


+ 95
- 132
CAPI/python/PyAPI/AI.py View File

@@ -50,138 +50,101 @@ fixedclass = []
class AI(IAI):
# 选手在这里实现自己的逻辑,要求和上面选择的阵营保持一致
def StudentPlay(self, api: IStudentAPI) -> None:
global fixedclass
selfInfo = api.GetSelfInfo()
available = [THUAI6.PlaceType.Land,
THUAI6.PlaceType.Grass, THUAI6.PlaceType.Door3, THUAI6.PlaceType.Door6, THUAI6.PlaceType.Door5, THUAI6.PlaceType.Gate]

def bfs(x, y):
if api.GetPlaceType(x, y) not in available:
return []

def GetSuccessors(x, y):
successors = []
if x > 0 and api.GetPlaceType(x - 1, y) in available:
successors.append((x - 1, y))
if x < 49 and api.GetPlaceType(x + 1, y) in available:
successors.append((x + 1, y))
if y > 0 and api.GetPlaceType(x, y - 1) in available:
successors.append((x, y - 1))
if y < 49 and api.GetPlaceType(x, y + 1) in available:
successors.append((x, y + 1))
return successors
selfX = AssistFunction.GridToCell(api.GetSelfInfo().x)
selfY = AssistFunction.GridToCell(api.GetSelfInfo().y)
frontier = queue.Queue()
frontier.put((selfX, selfY, []))
visited = []
while not frontier.empty():
currentX, currentY, path = frontier.get()
if currentX == x and currentY == y:
return path
for nextX, nextY in GetSuccessors(currentX, currentY):
if (nextX, nextY) not in visited:
visited.append((nextX, nextY))
frontier.put((nextX, nextY, path + [(nextX, nextY)]))
return []

def GoTo(x, y):
global path, cur
if path != [] and cur < len(path):
selfX = api.GetSelfInfo().x
selfY = api.GetSelfInfo().y
nextX, nextY = path[cur]
nextX = AssistFunction.CellToGrid(nextX)
nextY = AssistFunction.CellToGrid(nextY)
if selfX < nextX - 100:
api.MoveDown(10)
time.sleep(0.01)
return
if selfX > nextX + 100:
api.MoveUp(10)
time.sleep(0.01)
return
if selfY < nextY - 100:
api.MoveRight(10)
time.sleep(0.01)
return
if selfY > nextY + 100:
api.MoveLeft(10)
time.sleep(0.01)
return
cur += 1
return
else:
path = bfs(x, y)
cur = 0
return

if (AssistFunction.GridToCell(api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y)) == (6, 6) and api.GetGateProgress(5, 6) < 18000:
api.StartOpenGate()
return

if (AssistFunction.GridToCell(api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y)) == (6, 6) and api.GetGateProgress(5, 6) >= 18000:
api.Graduate()
return

if len(fixedclass) == 7:
GoTo(6, 6)
return

if api.GetPlaceType(AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)) == THUAI6.PlaceType.ClassRoom:
if api.GetSelfInfo().playerID == 0:
api.Print("Trying to fix!")
if api.GetClassroomProgress(AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)) < 103000:
api.StartLearning()
return
else:
if (AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)) not in fixedclass:
fixedclass.append(
(AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)))
elif api.GetSelfInfo().playerID == 1:
if api.GetClassroomProgress(AssistFunction.GridToCell(api.GetSelfInfo().x) - 1, AssistFunction.GridToCell(api.GetSelfInfo().y)) < 103000:
api.StartLearning()
return
else:
fixedclass.append((AssistFunction.GridToCell(
api.GetSelfInfo().x) - 1, AssistFunction.GridToCell(api.GetSelfInfo().y)))
elif api.GetSelfInfo().playerID == 2:
if api.GetClassroomProgress(AssistFunction.GridToCell(api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y) + 1) < 103000:
api.StartLearning()
return
else:
fixedclass.append((AssistFunction.GridToCell(
api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y) + 1))
elif api.GetSelfInfo().playerID == 3:
if api.GetClassroomProgress(AssistFunction.GridToCell(api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y) - 1) < 103000:
api.StartLearning()
return
else:
fixedclass.append((AssistFunction.GridToCell(
api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y) - 1))

for i in range(50):
for j in range(50):
if api.GetSelfInfo().playerID == 0:
if api.GetPlaceType(i, j) == THUAI6.PlaceType.ClassRoom and (i, j) not in fixedclass:
if api.GetPlaceType(i - 1, j) in available:
GoTo(i - 1, j)
return
elif api.GetSelfInfo().playerID == 1:
if api.GetPlaceType(i, j) == THUAI6.PlaceType.ClassRoom and (i, j) not in fixedclass:
if api.GetPlaceType(i + 1, j) in available:
GoTo(i + 1, j)
return
elif api.GetSelfInfo().playerID == 2:
if api.GetPlaceType(i, j) == THUAI6.PlaceType.ClassRoom and (i, j) not in fixedclass:
if api.GetPlaceType(i, j - 1) in available:
GoTo(i, j - 1)
return
elif api.GetSelfInfo().playerID == 3:
if api.GetPlaceType(i, j) == THUAI6.PlaceType.ClassRoom and (i, j) not in fixedclass:
if api.GetPlaceType(i, j + 1) in available:
GoTo(i, j + 1)
return
# global fixedclass
# selfInfo = api.GetSelfInfo()
# available = [THUAI6.PlaceType.Land,
# THUAI6.PlaceType.Grass, THUAI6.PlaceType.Door3, THUAI6.PlaceType.Door6, THUAI6.PlaceType.Door5, THUAI6.PlaceType.Gate]

# def bfs(x, y):
# if api.GetPlaceType(x, y) not in available:
# return []

# def GetSuccessors(x, y):
# successors = []
# if x > 0 and api.GetPlaceType(x - 1, y) in available:
# successors.append((x - 1, y))
# if x < 49 and api.GetPlaceType(x + 1, y) in available:
# successors.append((x + 1, y))
# if y > 0 and api.GetPlaceType(x, y - 1) in available:
# successors.append((x, y - 1))
# if y < 49 and api.GetPlaceType(x, y + 1) in available:
# successors.append((x, y + 1))
# return successors
# selfX = AssistFunction.GridToCell(api.GetSelfInfo().x)
# selfY = AssistFunction.GridToCell(api.GetSelfInfo().y)
# frontier = queue.Queue()
# frontier.put((selfX, selfY, []))
# visited = []
# while not frontier.empty():
# currentX, currentY, path = frontier.get()
# if currentX == x and currentY == y:
# return path
# for nextX, nextY in GetSuccessors(currentX, currentY):
# if (nextX, nextY) not in visited:
# visited.append((nextX, nextY))
# frontier.put((nextX, nextY, path + [(nextX, nextY)]))
# return []

# def GoTo(x, y):
# global path, cur
# if path != [] and cur < len(path):
# selfX = api.GetSelfInfo().x
# selfY = api.GetSelfInfo().y
# nextX, nextY = path[cur]
# nextX = AssistFunction.CellToGrid(nextX)
# nextY = AssistFunction.CellToGrid(nextY)
# if selfX < nextX - 100:
# api.MoveDown(10)
# time.sleep(0.01)
# return
# if selfX > nextX + 100:
# api.MoveUp(10)
# time.sleep(0.01)
# return
# if selfY < nextY - 100:
# api.MoveRight(10)
# time.sleep(0.01)
# return
# if selfY > nextY + 100:
# api.MoveLeft(10)
# time.sleep(0.01)
# return
# cur += 1
# return
# else:
# path = bfs(x, y)
# cur = 0
# return

# if (AssistFunction.GridToCell(api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y)) == (6, 6) and api.GetGateProgress(5, 6) < 18000:
# api.StartOpenGate()
# return

# if (AssistFunction.GridToCell(api.GetSelfInfo().x), AssistFunction.GridToCell(api.GetSelfInfo().y)) == (6, 6) and api.GetGateProgress(5, 6) >= 18000:
# api.Graduate()
# return

# if len(fixedclass) == 7:
# GoTo(6, 6)
# return

# if api.GetPlaceType(AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)) == THUAI6.PlaceType.ClassRoom:
# api.Print("Trying to fix!")
# if api.GetClassroomProgress(AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)) < 103000:
# api.StartLearning()
# return
# else:
# if (AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)) not in fixedclass:
# fixedclass.append(
# (AssistFunction.GridToCell(api.GetSelfInfo().x) + 1, AssistFunction.GridToCell(api.GetSelfInfo().y)))

# for i in range(50):
# for j in range(50):
# if api.GetPlaceType(i, j) == THUAI6.PlaceType.ClassRoom and (i, j) not in fixedclass:
# if api.GetPlaceType(i - 1, j) in available:
# GoTo(i - 1, j)
# return
api.PrintTricker()

def TrickerPlay(self, api: ITrickerAPI) -> None:
return

+ 4
- 0
CAPI/python/PyAPI/logic.py View File

@@ -358,6 +358,8 @@ class Logic(ILogic):
self.__logger.debug("Add Student!")
for item in message.obj_message:
if item.WhichOneof("message_of_obj") == "tricker_message":
if MessageType.TRICKER_INVISIBLE in item.tricker_message.buff:
continue
if AssistFunction.HaveView(self.__bufferState.self.viewRange, self.__bufferState.self.x, self.__bufferState.self.y, item.tricker_message.x, item.tricker_message.y, self.__bufferState.gameMap):
self.__bufferState.trickers.append(
Proto2THUAI6.Protobuf2THUAI6Tricker(item.tricker_message))
@@ -450,6 +452,8 @@ class Logic(ILogic):
self.__logger.debug("Add Tricker!")
for item in message.obj_message:
if item.WhichOneof("message_of_obj") == "student_message":
if MessageType.STUDENT_INVISIBLE in item.student_message.buff:
continue
if AssistFunction.HaveView(self.__bufferState.self.viewRange, self.__bufferState.self.x, self.__bufferState.self.y, item.student_message.x, item.student_message.y, self.__bufferState.gameMap):
self.__bufferState.students.append(
Proto2THUAI6.Protobuf2THUAI6Student(item.student_message))


Loading…
Cancel
Save