-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.pas
138 lines (112 loc) · 3.91 KB
/
PriorityQueue.pas
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
(*
Copyright (C) 2024 Jeffrey Getzin.
Licensed under the GNU General Public License v3.0 with additional terms.
See the LICENSE file in the repository root for details.
*)
[Inherit('Types'),Environment]Module PriorityQueue;
Type
Group_Choice = [Byte]1..5; { Group 5 is the party }
Group_Type = 0..4;
Individual_Type = 0..6;
AttackerType = Record
Priority: Integer; { order of attacks }
Attacker_Position: [Word]1..999; { Which monsters }
Caster_Level: Integer; { For spell casting }
Case Group: Group_Choice of { Which monster group }
5: (Action: Option_Type; { What action }
Target_Group: Group_Type; { Who is being attacked }
Target_Individual: Individual_Type; { " " " " }
WhatSpell: Spell_Name;
Old_Item,New_Item: Integer)
End;
Party_Commands_Type = Array [1..6] of AttackerType;
PriorityQueue = Record
Contents: Array [1..4008] of AttackerType;
Last: Integer;
End;
(******************************************************************************)
[Global]Function Empty (A: PriorityQueue): Boolean;
Begin
Empty:=(A.Last=0)
End;
(******************************************************************************)
[Global]Procedure MakeNull (Var A: PriorityQueue);
Begin
A:=Zero;
End;
(******************************************************************************)
[Global]Function P (A: AttackerType): Integer;
Begin
P:=A.Priority;
End;
(******************************************************************************)
[Global]Procedure Insert (X: AttackerType; Var A: PriorityQueue);
Var
NotDone: Boolean;
i: Integer;
Temp: AttackerType;
Begin
If A.Last>4007 then
HALT
Else
Begin
A.Last:=A.Last + 1;
A.Contents[A.Last] := x;
i := A.Last; { i is index of current position of x }
If I>1 then NotDone:=(P(A.Contents[i])<P(A.Contents[i div 2]))
Else NotDone:=False;
While NotDone do
Begin { Push x up the tree by exchanging it with its parent of larger priority. Recall p computes the priority of a
AttackerType element }
Temp:=A.Contents[i];
A.Contents[i]:=A.Contents[i div 2];
A.Contents[i div 2]:=Temp;
i:=i div 2;
If I>1 then
NotDone:=(P(A.Contents[i])<P(A.Contents[i div 2]))
Else
NotDone:=False
End
End
End;
(******************************************************************************)
[Global]Function DeleteMin (Var A: PriorityQueue): AttackerType;
Var
i,j: Integer;
Temp,minimum: AttackerType;
Begin
If A.last>0 then
Begin
Minimum:=A.Contents[1];
A.Contents[1]:=A.Contents[A.Last];
A.Last:=A.Last-1;
i:=1;
While (i <= (A.Last div 2)) do
Begin
If 2*i=A.last then J:=2*i
Else If P(A.Contents[2*i])<P(A.Contents[2*i+1]) then
j:=2*i
Else
j:=2*i+1;
If P(A.Contents[i]) > P(A.Contents[j]) then
Begin
Temp:=A.Contents[i];
A.Contents[i]:=A.Contents[j];
A.Contents[j]:=Temp;
i:=j;
End
Else
Begin
DeleteMin:=Minimum;
i:=(A.Last div 2)+1;
End
End;
DeleteMin:=Minimum;
End
Else
Begin
Temp.Group:=0;
DeleteMin:=Temp;
End;
End;
End.