-
Notifications
You must be signed in to change notification settings - Fork 0
/
npc_walking.zs
122 lines (115 loc) · 2.7 KB
/
npc_walking.zs
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
/* SUGGESTED VALUES
Random rate: 120
Halt Rate: 64
Step Speed: 1
Args <d3, Duration of Halt>: 40
*/
npc script walking
{
int comboT(npc T)
{
return (Graphics->GetPixel(bitmaps.overscan_type,scroll.xPos+T->X + (T->TileWidth*0.5), scroll.yPos+T->Y+56+(T->TileHeight*0.5)) * 10000);
}
void run(bool constantwalk, bool ignoreSlowWalk, int haltFrames) //.d0, the cooldown time between moves
{
const int HALT_FRAMES = 32;
haltFrames = ( haltFrames > 0 ) ? haltFrames : HALT_FRAMES;
int f;
this->Dir = Rand(4);
int dirclk = this->Rate;
int curx, cury, stuckclk; //used to check if we are stuck.
curx = this->X; cury = this->Y;
int randomclk = this->Rate;
int haltclk = this->Haltrate;
int curstep = this->Step;
while(this->isValid())
{
++f;
//logic
if (comboT(this) == CT_SLOWWALK && !ignoreSlowWalk)
{
curstep = ( (f&1) ) ? curstep : 0;
}
else curstep = this->Step;
//move
//halt for a bit if not constantwalk
//check if it is time to change direction
//if it is, then we do a coinflip. heads, change, tails, do not change, reset the clk (random)
if ( randomclk > 0 )
{
--randomclk;
}
else
{
//change direction.
this->Dir = Rand(4);
randomclk = this->Rate;
}
if ( haltclk > 0 )
{
--haltclk;
//LogPrint("NPC Can Walk? %s \n", (npcs::canWalk(this, curstep, this->Dir)) ? "true" : "false!");
if (npcs::canWalk(this, curstep, this->Dir))
{
switch(this->Dir)
{
case DIR_UP: this->Y -= curstep; break;
case DIR_DOWN: this->Y += curstep; break;
case DIR_LEFT: this->X -= curstep; break;
case DIR_RIGHT: this->X += curstep; break;
}
}
else //change dir
{
//LogPrint("npc %d could not move \n", <int>this);
int validDirs[3];
switch(this->Dir)
{
case DIR_UP:
{
validDirs[0] = DIR_DOWN;
validDirs[1] = DIR_LEFT;
validDirs[2] = DIR_RIGHT;
break;
}
case DIR_LEFT:
{
validDirs[0] = DIR_DOWN;
validDirs[1] = DIR_UP;
validDirs[2] = DIR_RIGHT;
break;
}
case DIR_DOWN:
{
validDirs[0] = DIR_LEFT;
validDirs[1] = DIR_UP;
validDirs[2] = DIR_RIGHT;
break;
}
case DIR_RIGHT:
{
validDirs[0] = DIR_LEFT;
validDirs[1] = DIR_UP;
validDirs[2] = DIR_DOWN;
break;
}
default: break;
}
//change direction
this->Dir = validDirs[Rand(3)];
}
}
else
{
int halfwaypoint = haltFrames*0.5;
for ( int q = 0; q < haltFrames; ++q )
{
if ( q == halfwaypoint ) this->Attack();
Waitframe();
}
haltclk = this->Haltrate;
}
Waitframe();
}
}
}