![](http://imgsrc.baidu.com/forum/w%3D580/sign=959572a9f51fbe091c5ec31c5b610c30/a0d920381f30e92456c7d1ee4b086e061f95f7e4.jpg)
我正在写避难所模式,最近已完成建造系统。
![](http://imgsrc.baidu.com/forum/w%3D580/sign=e5188af0b9096b6381195e583c328733/8fe91730e924b899465f34e769061d95087bf6e4.jpg)
可是npc部分一直出现问题,把进度一直往后拖(2015-03-05入坑)....
入正题
我的写法(能运作):
public FindClosesEnemy(entid)
{
new Float:Dist
new Float:maxdistance=1200.0
new indexid=0
new i = -1
while((i = find_ent_by_class(i, "player")))
{
if(is_user_alive(i) && is_valid_ent(i) && can_see_fm(entid, i))
{
Dist = entity_range(entid, i)
if(Dist <= maxdistance)
{
maxdistance=Dist
indexid=i
}
}
}
while((i = find_ent_by_class(i, "materials")))
{
if(0.0 < pev(i, pev_health) && is_valid_ent(i) && can_see_fm(entid, i))
{
Dist = entity_range(entid, i)
if(Dist <= maxdistance)
{
maxdistance=Dist
indexid=i
}
}
}
return (indexid) ? indexid : 0
}
public bool:can_see_fm(entindex1, entindex2)
{
if (!entindex1 || !entindex2)
return false
if (pev_valid(entindex1) && pev_valid(entindex1))
{
new flags = pev(entindex1, pev_flags)
if (flags & EF_NODRAW || flags & FL_NOTARGET)
{
return false
}
new Float:lookerOrig[3]
new Float:targetBaseOrig[3]
new Float:targetOrig[3]
new Float:temp[3]
pev(entindex1, pev_origin, lookerOrig)
pev(entindex1, pev_view_ofs, temp)
lookerOrig[0] += temp[0]
lookerOrig[1] += temp[1]
lookerOrig[2] += temp[2]
pev(entindex2, pev_origin, targetBaseOrig)
pev(entindex2, pev_view_ofs, temp)
targetOrig[0] = targetBaseOrig [0] + temp[0]
targetOrig[1] = targetBaseOrig [1] + temp[1]
targetOrig[2] = targetBaseOrig [2] + temp[2]
engfunc(EngFunc_TraceLine, lookerOrig, targetOrig, 0, entindex1, 0) // checks the had of seen player
if (get_tr2(0, TraceResult:TR_InOpen) && get_tr2(0, TraceResult:TR_InWater))
{
return false
}
else
{
new Float:flFraction
get_tr2(0, TraceResult:TR_flFraction, flFraction)
if (flFraction == 1.0 || (get_tr2(0, TraceResult:TR_pHit) == entindex2))
{
return true
}
else
{
targetOrig[0] = targetBaseOrig [0]
targetOrig[1] = targetBaseOrig [1]
targetOrig[2] = targetBaseOrig [2]
engfunc(EngFunc_TraceLine, lookerOrig, targetOrig, 0, entindex1, 0) // checks the body of seen player
get_tr2(0, TraceResult:TR_flFraction, flFraction)
if (flFraction == 1.0 || (get_tr2(0, TraceResult:TR_pHit) == entindex2))
{
return true
}
else
{
targetOrig[0] = targetBaseOrig [0]
targetOrig[1] = targetBaseOrig [1]
targetOrig[2] = targetBaseOrig [2] - 17.0
engfunc(EngFunc_TraceLine, lookerOrig, targetOrig, 0, entindex1, 0) // checks the legs of seen player
get_tr2(0, TraceResult:TR_flFraction, flFraction)
if (flFraction == 1.0 || (get_tr2(0, TraceResult:TR_pHit) == entindex2))
{
return true
}
}
}
}
}
return false
}
徴求其他写法