⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 RoboCup仿真组世界冠军源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
    Copyright (C) 2001  Tsinghuaeolus

    Authors : ChenJiang, YaoJinyi, CaiYunpeng, Lishi

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

	If you make any changes or have any comments we would appreciate a 
	message to yjy01@mails.tsinghua.edu.cn.
*/

#include "stdafx.h"

#include "netif.h"
#include "Global.h"
#include "Params.h"
#include "utils.h"
#include <stdlib.h>
#include <stdio.h>

/*  timer handle of information process and ai process */
HANDLE siginf = NULL, sigctrl = NULL;
CRITICAL_SECTION parse_update_mutex;
HANDLE newcycle_event =NULL, newsightinfo_event = NULL, newsenseinfo_event = NULL;
UINT WaitServerTimer = NULL;
DWORD  waittime = 20;
float  CP_UpdateAItime = 25.0f;
/* Interval = SP_simulator_step / Interrupts */
void InitializeObjs();

DWORD WINAPI InfProc(LPVOID lpParameter);
DWORD WINAPI CtrlProc(LPVOID lpParameter);
void CALLBACK CheckServer(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);
int InitInstance()
{
/**********    open files     **********/
#ifdef _LOG
	//rec.AssociateFile(CP_logfile);
	rec.SetLabel(LOG_SYNC, "Sync");
	rec.SetLabel(LOG_NONE,"");
	rec.SetLabel(LOG_SENDACTION, "SendAction");
	rec.SetLabel(LOG_STOPWATCH,"StopWatch");
	rec.SetLabel(LOG_ACTION, "Action");
	rec.SetLabel(LOG_UPDATE, "Update");
	rec.SetLabel(LOG_MISS, "Miss");
	rec.SetLabel(LOG_COLLIDE, "Coll");
	rec.SetLabel(LOG_KICK, "Kick");
	rec.SetLabel(LOG_UNKPLAYER, "Unk P");
	rec.SetLabel(LOG_HEAR, "Hear");
	rec.SetLabel(LOG_IT, "Interception");
	rec.SetLabel(LOG_PASS,"Pass"); 
	rec.SetLabel(LOG_SEE,"See");
	rec.SetLabel(LOG_DEFENSE, "Defense");
	rec.SetLabel(LOG_MEDIATION, "Mediation");
	rec.SetLabel(LOG_BUG, "BUG");
	rec.SetLabel(LOG_SITUATION, "Situation");
	rec.SetLabel(LOG_GOALIE, "Goalie");
	rec.SetLabel(LOG_SETPLAY, "SetPlay");
	rec.SetLabel(LOG_POSITIONING, "Positioning");
	rec.SetLabel(LOG_DRIBBLE,"Dribble");
	rec.SetLabel(LOG_OAKICK, "Oa kick");
	rec.SetLabel(LOG_VISUAL, "Visual");
	rec.SetLabel(LOG_DEFEXTEND, "Def extend");
	rec.SetLabel(LOG_VISUALREQ,"VReq");
	rec.SetLabel(LOG_VDEC, "VDec");
	rec.SetLabel(LOG_SHOOT, "Shoot");
	rec.SetLabel(LOG_HANDLEBALL, "HB");
	rec.SetLabel(LOG_OFFSIDE, "Offside");

	//rec.SetLogOn(LOG_NONE);
	//rec.SetLogOn(LOG_SENDACTION);
	//rec.SetLogOn(LOG_ACTION);
	//rec.SetLogOn(LOG_IT);
	//rec.SetLogOn(LOG_DRIBBLE);
//	rec.SetLogOn(LOG_PASS);
	//rec.SetLogOn(LOG_HEAR);
//	rec.SetLogOn(LOG_HANDLEBALL);
	//rec.SetLogOn(LOG_DEFEXTEND); 
	//rec.SetLogOn(LOG_POSITIONING);
	//rec.SetLogOn(LOG_OFFSIDE);
	//rec.SetLogOn(LOG_UPDATE);
	//rec.SetLogOn(LOG_STOPWATCH);
	//rec.SetLogOn(LOG_UNKPLAYER);
	//rec.SetLogOn(LOG_VDEC); 
	//rec.SetLogOn(LOG_MEDIATION); 
	//rec.SetLogOn(LOG_DRIBBLE); 
	//rec.SetLogOn(LOG_OAKICK);
	rec.SetLogOn(LOG_MISS);
	rec.SetLogOn(LOG_SHOOT);

	//recmsg.AssociateFile(CP_recfile);
	recmsg.SetLabel(LOG_SYNC,"Sync");
	recmsg.SetLabel(LOG_STOPWATCH,"StopWatch");
	recmsg.SetLabel(LOG_HEAR, "Hear");
	recmsg.SetLabel(LOG_MISS, "Miss");

	recmsg.SetLogOn(LOG_NONE);
	//recmsg.SetLogOn(LOG_HEAR);
	//recmsg.SetLogOn(LOG_MISS);
	recmsg.SetLogOn(LOG_STOPWATCH); 
	//recmsg.SetLogOn(LOG_SYNC);
#endif

	enable_my_error = 0;
	sock = init_connection(SP_host, SP_port);

	action.send_init_message();//sent init message

	if (!wait_message(recvbuf,sock)){// wait for server's reply
		my_error("No response form server!");
		return false;
	}

	sensory.Parse_initialize_message(recvbuf);
	my_error("Connected!");


/**********   Initialize Objects ***************/
	InitializeObjs();

/* set the socket back to blocking		*/
	unsigned long arp = 0;
	ioctlsocket(sock->socketfd, FIONBIO, &arp);

	InitializeCriticalSection(&parse_update_mutex); //initialize mutex	

	newcycle_event = CreateEvent(NULL, TRUE, FALSE, NULL);
	newsightinfo_event = CreateEvent(NULL, TRUE, FALSE, NULL);
	newsenseinfo_event = CreateEvent(NULL, TRUE, FALSE, NULL);

	WaitServerTimer = SetTimer(hWND, 1, 2000, CheckServer);

	watch.Changeview();//
	if ((siginf = CreateThread(NULL, 0, InfProc, NULL, 0, NULL)) == NULL){
		my_error("creat thread error");
		return false;
	}
	
	SetThreadPriority(siginf,THREAD_PRIORITY_TIME_CRITICAL);//enhance the priority of the thread
	
/********************   start control process   ******************/
	if ((sigctrl = CreateThread(NULL, 0, CtrlProc, NULL, 0, NULL)) == NULL){
		my_error("creat thread error");
		return false;
	}
	
	SetThreadPriority(sigctrl,THREAD_PRIORITY_HIGHEST);

	return true;
}


/*	Information  process	*/
DWORD WINAPI InfProc(LPVOID lpParameter){
	if (!ServerAlive) return 0;
	while(receive_message(recvbuf,sock) == 1){
		if (!ServerAlive) return 0;
		watch.SetNewmsgTime();//when receive new msg
		NoResponseFromServer = 0; // There is response frome server

		EnterCriticalSection(&parse_update_mutex);
		sensory.Parse(recvbuf);
		DoRecord(recvbuf);
// Notify the CtrlProc about some events				

		if(sensory.LastMsgIsSight()){
			SetEvent(newsightinfo_event);			
			watch.SetSeeTime(sensory.LatestTime);
			DoRecord(LOG_STOPWATCH,"See Time %f sight gap %.2f(%d %d) chview gap %.2f",
				watch.GetSeeTime(sensory.LatestTime), watch.SightIntervalBetween(sensory.PrevSightTime, sensory.LastSightTime),
				sensory.PrevSightTime, sensory.LastSightTime, watch.Sincechangeview(sensory.LastSightTime));
		}

		if(sensory.LastMsgIsSense()){
			SetEvent(newsenseinfo_event);
		}

		if (sensory.LatestTime > sensory.PrevInterruptTime || 
			(situation.ClockStopped && sensory.LastMsgIsSense())){
			//It is a new Cycle
			watch.SetCycleStartTime(sensory.LatestTime);//when the cycle starts
			
			//if (!sensory.LastMsgIsSense())	ResetEvent(newsenseinfo_event);

			if (!sensory.LastMsgIsSight())
				ResetEvent(newsightinfo_event);

			watch.SetNotificationTime(sensory.LatestTime); // when notify the control process
			SetEvent(newcycle_event);
			DoRecord(LOG_STOPWATCH,"Last Cycle%f,New Cycle Notify%f",watch.LastCycleLength(sensory.LatestTime),watch.GetNotificationTime(sensory.LatestTime));
		}
		LeaveCriticalSection(&parse_update_mutex);
	}
	return 0;
}

/*	Control Process	*/
DWORD WINAPI CtrlProc(LPVOID plParameter){
	int behave();

	HANDLE Handles[2];
	Handles[0] = newsenseinfo_event;
	Handles[1] = newsightinfo_event;
	if (!ServerAlive) return 0;
	
	int logwaitcounts = 0;
	int logwaitfails = 0;
	bool logwait;
	float predictsightime;
	while(1){
		//WaitForSingleObject(newcycle_event, INFINITE);// wait for the new cycle
		WaitForSingleObject(newsenseinfo_event, INFINITE);// wait for the new cycle		
	
		if (!situation.ClockStopped && sensory.LatestTime - 1 > situation.LastInterruptTime){
			my_error("Miss %d cycles?", sensory.LatestTime - 1 - situation.LastInterruptTime);
			DoLog(LOG_MISS, "%d cycles", sensory.LatestTime - 1 - situation.LastInterruptTime);
		}
		situation.LastInterruptTime = sensory.LatestTime;
		watch.SetCurrentCycle(sensory.LatestTime);
		watch.RecvNotification();
		
		logwait = false;
		if(sensory.LastSightTime < sensory.LatestTime){
			predictsightime = watch.PredictNextSight();
			if(predictsightime < (float)SP_simulator_step - CP_UpdateAItime){
				logwaitcounts ++;
				waittime = DWORD((float)SP_simulator_step - CP_UpdateAItime - watch.GetRecvTime()); 
				WaitForSingleObject(newsightinfo_event, waittime);
				logwait = true;
			}
		}else
			predictsightime = 0;

		//WaitForMultipleObjects(2, Handles, TRUE, waittime);// wait for the sense info and sight info
		watch.Wake();

// Update the inner world
		EnterCriticalSection(&parse_update_mutex);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -