Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #118 from BowmanChow/dev
Browse files Browse the repository at this point in the history
加入信号量,修复人物移动速度低的bug
  • Loading branch information
Sweetnow authored Apr 29, 2020
2 parents 148d877 + 41bf6f3 commit 8b605bc
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 38 deletions.
16 changes: 7 additions & 9 deletions CAPI/AI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<RuntimeTypeInfo>true</RuntimeTypeInfo>
<UseFullPaths>false</UseFullPaths>
<WarningLevel>EnableAllWarnings</WarningLevel>
<WarningLevel>Level1</WarningLevel>
<PreprocessorDefinitions>WIN32;_WINDOWS;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
Expand Down Expand Up @@ -188,12 +188,11 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<RuntimeTypeInfo>true</RuntimeTypeInfo>
<UseFullPaths>false</UseFullPaths>
<WarningLevel>EnableAllWarnings</WarningLevel>
<WarningLevel>Level1</WarningLevel>
<PreprocessorDefinitions>WIN32;_WINDOWS;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand All @@ -210,7 +209,7 @@
<Link>
<AdditionalDependencies>gmock.lib;gmock_main.lib;libprotobuf.lib;libprotobuf-lite.lib;libprotoc.lib;pthreadVSE2.lib;HPSocket.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>./windows_only/lib;./windows_only/lib/$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalOptions>%(AdditionalOptions) /machine:x86</AdditionalOptions>
<AdditionalOptions>%(AdditionalOptions) /machine:x64</AdditionalOptions>
<GenerateDebugInformation>true</GenerateDebugInformation>
<IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
<ImportLibrary>./Debug/AI.lib</ImportLibrary>
Expand All @@ -231,7 +230,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<RuntimeTypeInfo>true</RuntimeTypeInfo>
<UseFullPaths>false</UseFullPaths>
<WarningLevel>EnableAllWarnings</WarningLevel>
<WarningLevel>Level1</WarningLevel>
<PreprocessorDefinitions>WIN32;_WINDOWS;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
Expand Down Expand Up @@ -266,14 +265,14 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>.\windows_only\include;.\include;.\windows_only\proto_files;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>%(AdditionalOptions) -O3</AdditionalOptions>
<AdditionalOptions>%(AdditionalOptions) </AdditionalOptions>
<AssemblerListingLocation>$(IntDir)</AssemblerListingLocation>
<CompileAs>CompileAsCpp</CompileAs>
<ExceptionHandling>Sync</ExceptionHandling>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<RuntimeTypeInfo>true</RuntimeTypeInfo>
<UseFullPaths>false</UseFullPaths>
<WarningLevel>EnableAllWarnings</WarningLevel>
<WarningLevel>Level1</WarningLevel>
<PreprocessorDefinitions>WIN32;_WINDOWS;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
Expand Down Expand Up @@ -485,6 +484,7 @@
<ClCompile Include=".\src\CAPI.cpp" />
<ClCompile Include=".\src\Message.cpp" />
<ClCompile Include=".\src\OS_related.cpp" />
<ClCompile Include=".\src\Sema.cpp" />
<ClCompile Include=".\src\debug.cpp" />
<ClCompile Include=".\src\main.cpp" />
<ClCompile Include=".\src\player.cpp" />
Expand All @@ -495,8 +495,6 @@
<ClCompile Include=".\windows_only\proto_files\MessageToServer.pb.cc" />
<ClCompile Include=".\windows_only\proto_files\PingPacket.pb.cc" />
</ItemGroup>
<ItemGroup>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
3 changes: 3 additions & 0 deletions CAPI/AI.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<ClCompile Include=".\src\OS_related.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include=".\src\Sema.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include=".\src\debug.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
8 changes: 8 additions & 0 deletions CAPI/AI.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@
<LocalDebuggerCommandArguments>127.0.0.1 30000</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>127.0.0.1 30000</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>127.0.0.1 30000</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions CAPI/include/CAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Constant.h"
#include <MessageToServer.pb.h>
#include <MessageToClient.pb.h>
#include "Sema.h"

using namespace std;

Expand Down Expand Up @@ -44,6 +45,8 @@ class CAPI
Constant::Player player;
bool Closed;
bool PauseUpdate;
Sema sema;
Sema start_game_sema;

private:
CListenerImpl listener;
Expand Down
19 changes: 19 additions & 0 deletions CAPI/include/Sema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#ifndef SEMA_H
#define SEMA_H
#endif // !SEMA_H

#include <mutex>
#include <condition_variable>

class Sema
{
private:
std::mutex mu;
std::condition_variable cv;

public:
void notify();
void wait();
};

2 changes: 2 additions & 0 deletions CAPI/include/structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,6 @@ class player_info
};
extern player_info PlayerInfo;

extern long long getSystemTime();

#endif // !STRUCTURES_H
4 changes: 3 additions & 1 deletion CAPI/modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
lines = open(doc, encoding="utf-8").readlines()
fp = open(doc, 'w', encoding="utf-8")
for s in lines:
fp.write(s.replace("..\\", ".\\"))
str = s.replace(pwd2, ".")
str = str.replace(pwd, ".")
fp.write(str)
7 changes: 0 additions & 7 deletions CAPI/src/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ extern CAPI API;

unsigned long long THUAI3::initGameTime;

long long getSystemTime()
{
timeb t;
ftime(&t);
return t.time * 1000 + t.millitm;
}

int THUAI3::GetPing()
{
return API.Ping;
Expand Down
19 changes: 9 additions & 10 deletions CAPI/src/CAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <thread>
#include "structures.h"
#include "player.h"
#include "Sema.h"
#pragma comment(lib, "HPSocket.lib")

#include <sys/timeb.h>
Expand Down Expand Up @@ -162,6 +163,11 @@ void CAPI::SendChatMessage(string message)

void CAPI::SendCommandMessage(MessageToServer message)
{
static long long lastSendTime = 0;
long long now = getSystemTime();
if (now < lastSendTime + 50)
return;
lastSendTime = now;
MessageToServer* mes0 = new MessageToServer(message);
Message* mes2 = new Message(PlayerId, mes0);
Message* mes3 = new Message(-1, mes2);
Expand Down Expand Up @@ -217,8 +223,9 @@ void CAPI::UpdateInfo(Protobuf::MessageToClient* message)
mesC2S.set_issettalent(true);
mesC2S.set_talent(initTalent);
SendCommandMessage(mesC2S);
GameRunning = true;
std::cout << "Game start" << std::endl;
GameRunning = true;
start_game_sema.notify();
}
google::protobuf::Map<int64_t, Protobuf::GameObject>::const_iterator self_iter = message->gameobjectlist().find(PlayerInfo._id);
if (self_iter != message->gameobjectlist().end())
Expand All @@ -233,15 +240,6 @@ void CAPI::UpdateInfo(Protobuf::MessageToClient* message)
PlayerInfo.tool = self_iter->second.tooltype();
PlayerInfo.recieveText = self_iter->second.recievetext();
}
//Protobuf::GameObject self = message->gameobjectlist().at(PlayerInfo._id);
//PlayerInfo._position.x = PlayerInfo.position.x = self.positionx();
//PlayerInfo._position.y = PlayerInfo.position.y = self.positiony();
//PlayerInfo.facingDirection = self.direction();
//PlayerInfo.moveSpeed = self.movespeed();
//PlayerInfo.sightRange = PlayerInfo._sightRange = self.sightrange();
//PlayerInfo.dish = self.dishtype();
//PlayerInfo.tool = self.tooltype();
//PlayerInfo.recieveText = self.recievetext();
if (message->scores().contains(PlayerInfo._team))
PlayerInfo.score = message->scores().at(PlayerInfo._team);

Expand All @@ -262,6 +260,7 @@ void CAPI::UpdateInfo(Protobuf::MessageToClient* message)
{
task_list.push_back((DishType)(*i));
}
sema.notify();
}


Expand Down
14 changes: 14 additions & 0 deletions CAPI/src/Sema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Sema.h"
#include <mutex>
#include <condition_variable>

void Sema::notify()
{
std::unique_lock<std::mutex> locker(mu);
cv.notify_one();
}
void Sema::wait()
{
std::unique_lock<std::mutex> locker(mu);
cv.wait(locker);
}
4 changes: 3 additions & 1 deletion CAPI/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "player.h"
#include "API.h"
#include "OS_related.h"
#include "Sema.h"

using namespace std;

Expand Down Expand Up @@ -51,11 +52,12 @@ int main(int argc, char* argv[])

while (!GameRunning)
{
Sleep(1);
API.start_game_sema.wait();
}
THUAI3::initializeGameTime();
while (GameRunning)
{
API.sema.wait();
play();
}
getchar();
Expand Down
3 changes: 2 additions & 1 deletion CAPI/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#include "OS_related.h"
using namespace THUAI3;
Protobuf::Talent initTalent = Protobuf::Talent::Runner;//指定人物天赋。选手代码必须定义此变量,否则报错
Protobuf::Talent initTalent = Protobuf::Talent::None;//指定人物天赋。选手代码必须定义此变量,否则报错
void play()
{
char c;
Expand All @@ -18,6 +18,7 @@ void play()
int moveDistance = 0;
std::cout << endl << "Please Input your move distance" << endl;
cin >> moveDistance;
cout << moveDistance / PlayerInfo.moveSpeed * 1000 << endl;
move(Protobuf::Direction::Right, moveDistance / PlayerInfo.moveSpeed * 1000);
}break;
case 'e':
Expand Down
7 changes: 7 additions & 0 deletions CAPI/src/structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ std::list<Obj> MapInfo::get_mapcell(const int x, const int y)
Obj::Obj(const XYPosition& pos, ObjType objType) : position(pos), objType(objType)
{ }
player_info PlayerInfo;

long long getSystemTime()
{
timeb t;
ftime(&t);
return t.time * 1000 + t.millitm;
}
15 changes: 11 additions & 4 deletions communication/Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Program
private static IDServer server = new IDServer();
private static System.Timers.Timer myTimer = new System.Timers.Timer();
private static IPEndPoint Server;
private static object LastSpam;// = Constants.MaxMessage;
private static object[] LastSpam;// = Constants.MaxMessage;
/*
private static void TimeCount(object source, System.Timers.ElapsedEventArgs e) //倒计时
{
Expand All @@ -36,6 +36,7 @@ public static void Main(string[] args)
Constants.PlayerCount = ushort.Parse(playercount.Value());
if (Constants.PlayerCount < 1) Constants.PlayerCount = 1;
else if (Constants.PlayerCount > 2) Constants.PlayerCount = 2;
LastSpam = new object[Constants.PlayerCount];
//Constants.MaxMessage = int.Parse(messagelmt.Value());
Constants.TimeLimit = double.Parse(timelmt.Value());
if (Constants.TimeLimit < 10) Constants.TimeLimit = 10;
Expand All @@ -52,7 +53,8 @@ private static int MainInternal(string ep, ushort port, string token, int debugL
server.Port = port;
Constants.Debug("Agent Listen Port: " + server.Port.ToString());
Constants.Debug("Client Token: " + (token ?? "<offline>"));
LastSpam = Environment.TickCount;
for (int i = 0; i < LastSpam.Length; i++)
LastSpam[i] = Environment.TickCount;

//init timer
myTimer.Interval = Interval;
Expand All @@ -74,8 +76,13 @@ private static int MainInternal(string ep, ushort port, string token, int debugL
var now = Environment.TickCount;
lock (LastSpam)
{
if (now <= (int)LastSpam + Constants.TimeLimit) return;
LastSpam = now;
if (now <= (int)LastSpam[0] + Constants.TimeLimit) return;
for (int i = 1; i < Constants.PlayerCount; i++)
{
LastSpam[i - 1] = LastSpam[i];
}
LastSpam[Constants.PlayerCount - 1] = now;
Console.WriteLine(now);
}
}
else
Expand Down
4 changes: 1 addition & 3 deletions logic/THUnity2D/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,8 @@ public virtual void Move(double angle, double distance)
lock (privateLock)
{
if (!this._movable)
{
return;
}
if ((DateTime.Now - lastMoveTime).TotalSeconds < 1 / _frameRate)
if ((DateTime.Now - lastMoveTime).TotalSeconds < 0.7 / _frameRate)
return;
lastMoveTime = DateTime.Now;
MoveStart?.Invoke(this);
Expand Down
2 changes: 1 addition & 1 deletion runAgent.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
communication\Agent\bin\Release\netcoreapp3.0\Communication.Agent.exe --server 127.0.0.1:20000 --port 30000 --debugLevel 0 --playercount 2 --timelimit 25
communication\Agent\bin\Release\netcoreapp3.0\Communication.Agent.exe --server 127.0.0.1:20000 --port 30000 --debugLevel 0 --playercount 2 --timelimit 50
2 changes: 1 addition & 1 deletion runServer.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
logic\Logic.Server\bin\Release\netcoreapp3.0\Logic.Server.exe --port 20000 --debugLevel 1 --playerCount 2 --agentCount 4 --gameTime 600
logic\Logic.Server\bin\Release\netcoreapp3.0\Logic.Server.exe --port 20000 --debugLevel 1 --playerCount 2 --agentCount 1 --gameTime 600

0 comments on commit 8b605bc

Please sign in to comment.