-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmonster.inc
175 lines (150 loc) · 3.6 KB
/
monster.inc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{ monster management }
Procedure GenerateMonsters;
Var
P: Real;
I: Integer;
Valid: Boolean;
Begin
MDist := -1;
MonsterI := 0;
Valid := False;
For I := 0 To RoomI Do
Begin
P := Random;
If P > 0.6 Then
Begin
Repeat
With Rooms[I], Monsters[MonsterI] Do
Begin
DX := Random(X2 - X1 - 1) + X1 + 1;
DY := Random(Y2 - Y1 - 1) + Y1 + 1;
X := DX;
Y := DY;
Room := I; { monsters stay in room }
Valid := (X <> CPlayer.X) And (Y <> CPlayer.Y) And
(HitItem(X, Y) = -1);
End;
Until Valid;
MonsterI := MonsterI + 1;
End;
End;
MonsterI := MonsterI - 1;
End;
{ rules: }
{ in a room with a player: }
{ a monster will move away from player with light }
{ a monster will move towards player without light }
{ in a room without a player }
{ a monster will move in a random direction }
Procedure MoveRandom(I: Integer);
Var
Tries: Integer; { 4 tries/ cardinalities }
Valid : Boolean;
D: Integer; { direction to try }
TX, TY: Integer;
Begin
With Monsters[I] Do
Begin
{ if (M.Room <> CPlayer.Room) then begin }
D := Random(4);
Tries := 0;
Repeat
Tries := Tries + 1;
If (Tries <> 1) Then
Begin
D := (D + 1) Mod 4; { try next cardinality }
End;
TX := X;
TY := Y;
Case D Of
0: TX := TX - 1;
1: TX := TX + 1;
2: TY := TY - 1;
3: TY := TY + 1;
End;
Valid := (Not HitWall(TX, TY)) And (HitItem(TX, TY) = -1);
Until Valid Or (Tries > 4);
If Valid Then
Begin
X := TX;
Y := TY;
End;
{ end; }
End;
End;
{ Attempt to move closer to the player char }
{ D: 1 - towards, -1 away }
Procedure MoveToPlayer(I: Integer; D: Integer);
Var
CX, CY: Integer; { change X/Y }
TX, TY: Integer; { try X/Y }
Begin
With Monsters[I] Do
Begin
CX := X - CPlayer.X;
CY := Y - CPlayer.Y;
If Not ((CX = 0) And (CY = 0)) Then
Begin
{ work out which is the greater disparity and move closer towards the player }
TX := X;
TY := Y;
If Abs(CX) > Abs(CY) Then
If CX > 0 Then TX := X - D
Else TX := X + D
Else
If CY > 0 Then TY := Y - D
Else TY := Y + D;
If (Not HitWall(TX, TY)) And (HitItem(TX, TY) = -1) Then
Begin
X := TX;
Y := TY;
End
Else MoveRandom(I);
End;
End;
End;
Procedure MoveMonsters;
Var
I: Integer;
D: Integer; { Dir of monster movement }
Begin
If L = 0 Then D := 1
Else D := -1;
For I := 0 To MonsterI Do
If Monsters[I].Room = CPlayer.Room Then MoveToPlayer(I, D)
Else MoveRandom(I)
End;
{ check for collisions with monsters }
{ return distance to monster if < 4 }
Function HitMonster(X1, Y1: Integer): Integer;
Var
I: Integer;
CX, CY: Integer;
Found: Boolean;
Begin
Found := False;
I := 0;
Repeat
Begin
With Monsters[I] Do
Begin
{ only care if same room }
If HitRoom(X1, Y1) = Room Then
Begin
CX := Abs(X1 - X);
CY := Abs(Y1 - Y);
If (CX < 4) And (CY < 4) Then
Found := True
Else
I := I + 1;
End
Else
I := I + 1;
End;
End;
Until (I > MonsterI) Or Found;
If Found Then
HitMonster := Max(CX, CY)
Else
HitMonster := -1;
End;