和平守护者卫士NPC脚本,当玩家在这个NPC的一定范围内,一定的x,y坐标内,你就不会有PVP标记。
简单说来,这个就是一个设置安全区域的办法之一!
#include "ScriptPCH.h"
typedef struct {float x, y;} Point;
// This is a list of X, Y coordinates that you want to make your "safe area" 这里就是定义安全区域的坐标位置的地方
// The first and last points MUST be the same for this to work
static const Point V[] = { {4519.443359f, 2709.068359f},
{4518.447266f, 2699.789795f},
{4512.137695f, 2689.388184f},
{4512.927734f, 2674.512207f},
{4515.222168f, 2658.663330f},
{4523.524902f, 2641.542480f},
{4536.573730f, 2630.782715f},
{4546.353027f, 2636.786865f},
{4562.053711f, 2651.318359f},
{4570.511719f, 2663.249756f},
{4578.481445f, 2696.588135f},
{4564.876465f, 2707.007324f},
{4542.565918f, 2708.313477f},
{4526.848145f, 2710.782471f},
{4519.443359f, 2709.068359f} };
static const int n = 14;
class npc_peacekeeper : public CreatureScript
{
public:
npc_peacekeeper() : CreatureScript("npc_peacekeeper") {}
CreatureAI* GetAI(Creature* c) const
{
return new npc_peacekeeperAI(c);
}
struct npc_peacekeeperAI : public ScriptedAI
{
npc_peacekeeperAI(Creature* c) : ScriptedAI(c) {}
int isLeft(Point P0, Point P1, Point P2)
{
return ( (P1.x - P0.x) * (P2.y - P0.y)
- (P2.x - P0.x) * (P1.y - P0.y) );
}
// Checks if a Point is inside the predefined area
bool IsInArea(Point P)
{
int wn = 0;
for (int i=0; i < n; i++)
{
if (V[i].y <= P.y)
{
if (V[i + 1].y > P.y)
if (isLeft(V[i], V[i+1], P) > 0)
++wn;
}
else
{
if (V[i + 1].y <= P.y)
if (isLeft(V[i], V[i+1], P) < 0)
--wn;
}
}
return wn != 0;
}
void Reset()
{
// Only run the check every 1 second rather than every update
// Not sure how intensive this algorithm is, but it's better to be safe
timer = 1000;
}
void UpdateAI(uint32 diff)
{
if (timer <= diff)
{
Map::PlayerList const& Players = me->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = Players.begin(); itr != Players.end(); ++itr)
{
if (Player* player = itr->GetSource())
{
if (player->GetAreaId() != me->GetAreaId())
continue;
Point p = { player->GetPositionX(), player->GetPositionY() };
if (IsInArea(p))
player->SetPvP(false);
else
player->SetPvP(true);
}
}
timer = 1000;
} else timer -= diff;
}
uint32 timer;
};
};
void AddSC_npc_peacekeeper()
{
new npc_peacekeeper();
}
上面就是NPC的脚本代码。具体怎么加入到你的内核中去,你就自己想办法了!!
|