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

📄 bugbots.cpp

📁 BugBots是一个游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "utility.h"#include "BugBot.h"#include "food.h"#include "clump.h"#include "mainbrain.h"
#include "soundmanager.h"#include <cmath>#include <iostream>#include <algorithm>#include <map>#include <list>#include "sutil.h"#include "MapHandler.h"const extern int SCREENWIDTH;const extern int SCREENHEIGHT;extern MapHandler MH;BugBot::BugBot(){    m_dest = NoPosition;    m_health = 1000;    m_targetbot = -1;    m_me = -1;
	starved = false;}void BugBot::CheckHunger(){    if(m_health < 100)    {	m_flags += HUNGRY;	if(HasItem() && !m_item.bot || (HasItem() && m_item.bot && IsCannibal()))	{	    Eat(m_item);	    m_dest = (*MH.GetMainBrainIter(m_team)).GetTargetPos(m_pos);	    if(m_dest == NoPosition)	    {		m_dest.Random();		m_flags += SEARCHING;	    }	    if(m_health >= 100)	    {		m_flags -= HUNGRY;	    }	}	if(MH.IsOccupiedByItem(m_pos))	{	    std::list<BugBot>::iterator Corpse = MH.GetCorpseAt(m_pos);	    std::list<Food>::iterator Food = MH.GetFoodAt(m_pos);	    if(Food != 0)	    {		Eat(Item((*Food).GetMe(), false));	    }	    if(Corpse != 0 && IsCannibal())	    {		Eat(Item((*Corpse).GetMe(), true));	    }	}    }    else    {	m_flags -= HUNGRY;    }    if(m_health < 40)    {	m_flags += STARVING;	if(HasItem())	{	    Eat(m_item);	    m_dest = (*MH.GetMainBrainIter(m_team)).GetTargetPos(m_pos);	    if(m_dest == NoPosition)	    {		m_dest.Random();		m_flags += SEARCHING;	    }	}	if(MH.IsOccupiedByItem(m_pos))	{	    std::list<BugBot>::iterator Corpse = MH.GetCorpseAt(m_pos);	    std::list<Food>::iterator Food = MH.GetFoodAt(m_pos);	    if(Food != 0)	    {		Eat(Item((*Food).GetMe(), false));	    }	    if(Corpse != 0)	    {		Eat(Item((*Corpse).GetMe(), true));	    }	}    }    else    {	m_flags -= STARVING;    }    if(m_health <=0)    {	if(HasItem())	{	    Eat(m_item);	    m_dest = (*MH.GetMainBrainIter(m_team)).GetTargetPos(m_pos);	    if(m_dest == NoPosition)	    {		m_dest.Random();		m_flags += SEARCHING;	    }	}	if(MH.IsOccupiedByItem(m_pos))	{	    std::list<BugBot>::iterator Corpse = MH.GetCorpseAt(m_pos);	    std::list<Food>::iterator Food = MH.GetFoodAt(m_pos);	    if(Food != 0)	    {		Eat(Item((*Food).GetMe(), false));	    }	    if(Corpse != 0)	    {		Eat(Item((*Corpse).GetMe(), true));	    }	}	if(m_health <= 0)	{
		starved = true;	    Die();	}    }    if(m_health >= 40)    {	m_flags -= STARVING;	if(m_health >= 100)	{	    m_flags -= HUNGRY;	}    }}void BugBot::CheckFollow(){    if(m_targetbot != -1)    {	std::list<BugBot>::iterator i = MH.GetBugBotIter(m_targetbot);	if(i==0) 	{		    //std::cout << "CheckFollow() Invalid target:" << m_targetbot << endl;	    m_targetbot=-1;	    return;	}		m_dest =(*i).GetPos();    }}void BugBot::CheckDest(){    // //std::cout << "My team is " << m_team << endl;    if(m_dest == m_pos)    {	m_dest = NoPosition; //clear m_dest when arrived	m_flags -= SEARCHING;    }//m_dest == mpos            if(m_dest == NoPosition)    {	if(HasItem() && !IsRenegade())	{	    std::list<MainBrain>::iterator mbit= MH.GetMainBrainIter(m_team);	    	    if(mbit!=0)		m_dest = (*mbit).GetPos(); //head towards mainbrain	}	if(!HasItem() && !IsRenegade())// && !m_flags[SEARCHING])	{	    std::list<MainBrain>::iterator mbit= MH.GetMainBrainIter(m_team);	    	    m_dest = (*mbit).GetTargetPos(m_pos);	    	    if(m_dest == NoPosition) //mainbrain doesnt know	    {		m_flags.SetFlag(SEARCHING);		m_dest.Random();	    }	    else	    {		m_flags -= SEARCHING;	    }	}	if(IsRenegade())	{	    //m_targetbot = MH.GetRandomBot()->GetMe();	}    }    if(m_flags[SEARCHING] && !IsRenegade())    {	std::list<MainBrain>::iterator mbit = MH.GetMainBrainIter(m_team);		if(!m_team) return;		Position tpos = (*mbit).GetTargetPos(m_pos);	if(tpos != NoPosition)	{	    m_dest = tpos;	    m_flags -= SEARCHING;	}    }

	if(m_dest == NoPosition)
	{
		m_flags.SetFlag(SEARCHING);
		m_dest.Random();
	}}bool BugBot::MoveToDest(){        Position tmp;    Pixel Location;    vector<Pixel> Points;        Location.Location = m_pos;    Location.DistToDest = DistPTP(m_pos, m_dest);        tmp = m_pos;        tmp.x++;    if(tmp.x < SCREENWIDTH && tmp.y < SCREENHEIGHT && tmp.x > 0 && tmp.y >0)    {	Points.push_back(Pixel(tmp, DistPTP(m_dest, tmp)));    }    tmp.x--;        tmp.x--;    if(tmp.x < SCREENWIDTH && tmp.y < SCREENHEIGHT && tmp.x > 0 && tmp.y >0)    {	Points.push_back(Pixel(tmp, DistPTP(m_dest, tmp)));    }    tmp.x++;        tmp.y++;    if(tmp.x < SCREENWIDTH && tmp.y < SCREENHEIGHT && tmp.x > 0 && tmp.y >0)    {	Points.push_back(Pixel(tmp, DistPTP(m_dest, tmp)));    }    tmp.y--;        tmp.y--;    if(tmp.x < SCREENWIDTH && tmp.y < SCREENHEIGHT && tmp.x > 0 && tmp.y >0)    {	Points.push_back(Pixel(tmp, DistPTP(m_dest, tmp)));    }    tmp.y++;        sort(Points.begin(),Points.end());        for(int i = 0; i < Points.size(); i++ )    {	if(!IsCorpse())	{	    if( Move(Points[i].Location) )	    {		if( Points[i] > Location ) // we moved away from our target.		{		    m_avoids.push_back(Avoid( Location.Location, AvoidLength ) );		}		if(MH.IsOccupiedByItem(Points[i].Location))		{		    ItemInPath(Points[i].Location);		}		break;	    }	}	else	{	    break;	}    }    if(HasItem())    {	if(!m_item.bot)	{	    (*MH.GetFoodIter(m_item.vnum)).SetPos(m_pos,true);	    	}	else	{	    std::list<BugBot>::iterator it = MH.GetBugBotIter(m_item.vnum);	    if( it == 0 )	    {		std::cout << "MoveToDest() Bad item vnum:" << m_item.vnum << endl;	    }	    else	    {		(*it).SetPos(m_pos,true);	    }	}    }    if(IsCorpse())    {	return false;    }    else    {	return true;    }}bool BugBot::Move(Position pos){    if(pos.x < 0 || pos.x > SCREENWIDTH || pos.y < 0 || pos.y > SCREENHEIGHT)    {	return false;    }    for(std::vector<Avoid>::iterator i = m_avoids.begin();i != m_avoids.end();i++)    {	if( (*i).Location == pos )	{	    return false;	}    }

	if(MH.IsOccupiedByCorpse(pos) && HasItem() && m_item.bot)
	{
		m_avoids.push_back(Avoid(pos, AvoidLength));
		return false;
	}
    if(MH.IsOccupiedByBugBot(pos))    {	if(BotInPath(pos))	{	    return false;	}    }    if(!IsCorpse())    {		SetPos(pos,true); // WOOP		return true;    }    else    {		return false;    }}bool BugBot::BotInPath(Position pos){    const bool can = IsCannibal();    const bool ren = IsRenegade();    const bool food = HasItem();    const bool hungry = IsHungry();    const bool starving = IsStarving();    const std::list<BugBot>::iterator BotIt = MH.GetBugBotAt(pos);        //if(!BotIt) return false;        const bool SameTeam = (m_team == (*BotIt).GetTeam());        if(BotIt == 0)    {	return false;    }        if(IsCorpse())    {	return true;    }    if(SameTeam)    {	if(!ren)	{	    return true;	}	else	{

	    if(Attack((*BotIt).GetMe()))	    {		if(starving || (can && hungry))		{		    Eat(Item((*BotIt).GetMe(),true));		}		return false;	    }	}    }    else    {	if(Attack((*BotIt).GetMe()))	{	    if(starving || (can && hungry))	    {		Eat(Item((*BotIt).GetMe(),true));	    }	    return false;	}    }    return true;}void BugBot::ItemInPath(Position pos){    std::list<Food>::iterator food = MH.GetFoodAt(pos);    std::list<BugBot>::iterator corpse = MH.GetCorpseAt(pos);        if(food != 0)//food in path    {		if(!IsRenegade())		{			std::list<MainBrain>::iterator mbit = MH.GetMainBrainIter(m_team);	    			if(mbit!=0)			(*mbit).ReportFood((*food).GetMe());		}		if(HasItem())		{			return;		}		if(IsHungry() || IsStarving())		{			Eat(Item((*food).GetMe(),false));			return;		}		else		{			if(!(m_flags.IsSet(SEARCHING)) && (m_dest != pos))			{				return;			}			PickUp(Item((*food).GetMe(),false));		}    }    if(corpse != 0 && !(*corpse).IsCarried())//corpse    {		if(HasItem() && (m_item.bot==false) )		{						if(!MH.IsOccupiedByFood(pos))			{			DropItem();			PickUp(Item((*corpse).GetMe(),true));			}			return;		}		if(HasItem())		{			if(!IsRenegade())			{			std::list<MainBrain>::iterator mbit = MH.GetMainBrainIter(m_team);						if(mbit!=0)				(*mbit).ReportCorpse((*corpse).GetMe());			}			return;		}		if(IsStarving() || (IsHungry() && IsCannibal()))		{			Eat(Item((*corpse).GetMe(),true));			return;		}		if(!(m_flags.IsSet(SEARCHING)) && (m_dest != pos))		{			if(!IsRenegade())			{			std::list<MainBrain>::iterator mbit = MH.GetMainBrainIter(m_team);						if(mbit!=0)				(*mbit).ReportCorpse((*corpse).GetMe());			}			return;		}
		if(!HasItem())		PickUp(Item((*corpse).GetMe(),true));	}}void BugBot::AreaScan(){    std::vector<Item> Objects;    std::vector<int> Food;    std::vector<int> BotSameTeam;    std::vector<int> BotDifferentTeam;    std::vector<int> Corpse;        Objects = Radar();        if(!Objects.empty())    {	for(std::vector<Item>::iterator i = Objects.begin(); i != Objects.end(); i++)	{	    if((*i).bot && MH.GetBugBotIter((*i).vnum) != 0)	    {			if( (*MH.GetBugBotIter((*i).vnum)).IsCorpse() && !(MH.GetBugBotIter(i->vnum)->GetFlags().IsSet(CARRIED)))			{				Corpse.push_back((*i).vnum);			}			else if((*MH.GetBugBotIter((*i).vnum)).GetTeam() == m_team)			{				BotSameTeam.push_back((*i).vnum);			}			else			{				BotDifferentTeam.push_back((*i).vnum);			}	    }	    else	    {			if((*MH.GetFoodIter((*i).vnum)).GetClump() != -2)			{				Food.push_back((*i).vnum);			}	    }	}	if(!Corpse.empty())	{	    for(std::vector<int>::iterator i = Corpse.begin(); i != Corpse.end(); i++)	    {		(*MH.GetMainBrainIter(m_team)).ReportCorpse(*i);	    }	    if(!HasItem() && m_flags[SEARCHING])	    {		std::vector<Pixel> CorpseSpots;		Position tmp;		for(std::vector<int>::iterator i = Corpse.begin(); i != Corpse.end(); i++)		{		    std::list<BugBot>::iterator it = MH.GetBugBotIter(*i);		    if( it == 0 )		    {			//std::cout << "AreaScan, Bad corpse" << endl;		    }		    else		    {			tmp = (*it).GetPos();			CorpseSpots.push_back(Pixel(tmp, DistPTP(tmp, m_pos)));		    }		}		std::sort(CorpseSpots.begin(), CorpseSpots.end());		std::vector<Pixel>::iterator vi = CorpseSpots.begin();		m_dest = (*vi).Location;
		m_flags -= SEARCHING; // VLOOP	    }	}	if(!Food.empty())	{	    for(std::vector<int>::iterator i = Food.begin(); i != Food.end(); i++)	    {		(*MH.GetMainBrainIter(m_team)).ReportFood(*i);	    }	    if(m_item.vnum == -1 && m_flags[SEARCHING])	    {		std::vector<Pixel> FoodSpots;		Position tmp;		for(std::vector<int>::iterator i = Food.begin(); i != Food.end(); i++)		{		    tmp = (*MH.GetFoodIter(*i)).GetPos();		    FoodSpots.push_back(Pixel(tmp, DistPTP(tmp, m_pos)));		}		std::sort(FoodSpots.begin(), FoodSpots.end());		std::vector<Pixel>::iterator vi2 = FoodSpots.begin();		m_dest = (*vi2).Location;
		m_flags -= SEARCHING; //VLOOP	    }	}	if(!BotSameTeam.empty())	{	    Position tmp;	    std::vector<Pixel> BotPos;	    if(m_flags[RENEGADE])	    {			for(std::vector<int>::iterator i = BotSameTeam.begin(); i != BotSameTeam.end(); i++)			{		   // tmp = (MH->GetMainBrainIter(*i))->GetPos();
				tmp = (MH.GetBugBotIter(*i))->GetPos();
				if(MH.GetBugBotAt(tmp) == 0 )

⌨️ 快捷键说明

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