1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package NPCAI.AIType_198
#
# @todo:ÅÜÅÜÅÜ-Ëæ»ú
# @author hxp
# @date 2017-11-27
# @version 1.0
#
# ÏêϸÃèÊö: ÅÜÅÜÅÜ-Ëæ»ú£¬ÌÓÀëÍæ¼ÒAI
#
#-------------------------------------------------------------------------------
#"""Version = 2017-11-27 16:30"""
#-------------------------------------------------------------------------------
 
import ChConfig
import GameWorld
import IPY_GameWorld
import NPCCommon
 
import random
#-------------------------------------------------------------------------------
 
## ³õʼ»¯
#  @param curNPC µ±Ç°npc
#  @return None
#  @remarks º¯ÊýÏêϸ˵Ã÷.
def DoInit(curNPC):
    curNPC.GetNPCAngry().Init(ChConfig.Def_NormalNPCAngryCount)
    return
 
def OnNPCReborn(curNPC):
    curNPC.SetIsNeedProcess(True)
    return
 
## Ö´ÐÐAI
#  @param curNPC µ±Ç°npc
#  @param tick µ±Ç°Ê±¼ä
#  @return None
#  @remarks º¯ÊýÏêϸ˵Ã÷.
def ProcessAI(curNPC, tick):
    
    npcControl = NPCCommon.NPCControl(curNPC)
    if curNPC.GetCurAction() == IPY_GameWorld.laNPCDie or not curNPC.IsAlive():
        #NPCËÀÍö, ½øÈëËÀÍöµ¹¼ÆÊ±
        if npcControl.DieTick(tick) == 0:
            return
    
    if curNPC.GetCurAction() == IPY_GameWorld.laNPCMove:
        #GameWorld.DebugLog("ÒÆ¶¯Öв»´¦Àí£¡(%s,%s)" % (curNPC.GetPosX(), curNPC.GetPosY()))
        return
    
    #Ë¢ÐÂ×Ô¼º³ðºÞ¶ÈÁбí
    curTag = None
    npcControl.RefreshAngryList(tick)
    curNPCAngry = npcControl.GetMaxAngryTag()
    if curNPCAngry:
        #³ðºÞ¶ÔÏóÀàÐÍ,³ðºÞ¶ÔÏóID
        curNPCAngryType = curNPCAngry.GetObjType()
        curNPCAngryID = curNPCAngry.GetObjID()
        curTag = GameWorld.GetObj(curNPCAngryID, curNPCAngryType)
        
    if curTag == None:
        # µØÍ¼ÄÚËæ»úÒÆ¶¯
        __RandMove(curNPC)
    else:
        __RunawayFromAngryObj(curNPC, curTag)
        
    return
 
def __RunawayFromAngryObj(curNPC, curNPCAngry):
    ## Ô¶Àë³ðºÞÍæ¼Ò
    moveArea = curNPC.GetMoveArea() # µ¥´ÎÒÆ¶¯·¶Î§
    posX = curNPC.GetPosX()
    posY = curNPC.GetPosY()
    angryObjPosX, angryObjPosY = curNPCAngry.GetPosX(), curNPCAngry.GetPosY()
    diffX, diffY = posX - angryObjPosX, posY - angryObjPosY # Á½Õß×ø±ê²î
    dirX = 1 if diffX >= 0 else - 1 # x×ø±ê²î·½Ïò
    dirY = 1 if diffY >= 0 else - 1 # y×ø±ê²î·½Ïò
    
    #for _ in xrange(moveArea * moveArea):
    randDist = max(3, random.randint(moveArea / 2, moveArea)) # Ëæ»úÒÆ¶¯¾àÀë
    distX = randDist * dirX
    distY = randDist * dirY
    
    newPosX = posX + distX
    newPosY = posY + distY
    
    if (posX == newPosX and posY == newPosY) or not GameWorld.GetMap().CanMove(newPosX, newPosY):
        # Á½Õß×ø±ê²îÔÚNPCÒÆ¶¯·¶Î§ÄÚ£¬Ôò³¯³ðºÞÄ¿±êºó·½Òƶ¯, ·ñÔòÔò·´ÏòËæ»úÒÆ¶¯
        if abs(diffX) <= 5 and abs(diffY) <= 5:
            newPosX = angryObjPosX - 3 * dirX
            newPosY = angryObjPosY - 3 * dirY
            #GameWorld.DebugLog("³¯Íæ¼Òºó·½Òƶ¯: (%s,%s),angry(%s,%s),diff(%s,%s),dir(%s,%s),moveArea=%s,new(%s,%s)" 
            #                   % (posX, posY, angryObjPosX, angryObjPosY, diffX, diffY, dirX, dirY, moveArea, newPosX, newPosY))
        else:
            __RandMove(curNPC)
            return
            
    if posX == newPosX or posY == newPosY:
        return
    #GameWorld.DebugLog("ÌÓÀëÒÆ¶¯: (%s,%s) to (%s,%s)" % (posX, posY, newPosX, newPosY))
    curNPC.Move(newPosX, newPosY)
    return
 
def __RandMove(curNPC):
    ## Ëæ»úÒÆ¶¯
    moveArea = curNPC.GetMoveArea()
    posX = curNPC.GetPosX()
    posY = curNPC.GetPosY()
    gameMap = GameWorld.GetMap()
    
    for _ in xrange(moveArea * moveArea):
        randX = posX + random.randint(-moveArea, moveArea)
        randY = posY + random.randint(-moveArea, moveArea)
        if posX == randX and posY == randY:
            continue
        if gameMap.CanMove(randX, randY):
            #GameWorld.DebugLog("Ëæ»úÒÆ¶¯: (%s,%s) to (%s,%s)" % (posX, posY, randX, randY))
            curNPC.Move(randX, randY)
            return
    return