author | nemo |
Sun, 24 Jan 2010 16:25:10 +0000 | |
changeset 2708 | 84bf57349d46 |
parent 2699 | 249adefa9c1c |
child 2716 | b9ca1bfca24f |
permissions | -rw-r--r-- |
589 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
589 | 3 |
* Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com> |
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
2630 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
589 | 21 |
unit uTriggers; |
22 |
||
23 |
interface |
|
24 |
uses SDLh, uConsts; |
|
25 |
||
615 | 26 |
type TTrigAction = (taSpawnGear, taSuccessFinish, taFailFinish); |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
27 |
|
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
28 |
procedure init_uTriggers; |
615 | 29 |
procedure AddTriggerSpawner(id, Ticks, Lives: Longword; GearType: TGearType; X, Y: LongInt; GearTriggerId: Longword); |
30 |
procedure AddTriggerSuccess(id, Ticks, Lives: Longword); |
|
31 |
procedure AddTriggerFail(id, Ticks, Lives: Longword); |
|
593 | 32 |
procedure TickTrigger(id: Longword); |
589 | 33 |
|
34 |
implementation |
|
610 | 35 |
uses uGears, uFloat, uMisc, uWorld; |
589 | 36 |
type PTrigger = ^TTrigger; |
37 |
TTrigger = record |
|
38 |
id: Longword; |
|
593 | 39 |
Ticks: Longword; |
594 | 40 |
Lives: Longword; |
41 |
TicksPerLife: LongWord; |
|
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
42 |
Action: TTrigAction; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
43 |
X, Y: LongInt; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
44 |
SpawnGearType: TGearType; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
45 |
SpawnGearTriggerId: Longword; |
589 | 46 |
Next: PTrigger; |
47 |
end; |
|
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
48 |
var TriggerList: PTrigger; |
589 | 49 |
|
615 | 50 |
function AddTrigger(id, Ticks, Lives: Longword): PTrigger; |
613 | 51 |
var tmp: PTrigger; |
52 |
begin |
|
53 |
new(tmp); |
|
54 |
FillChar(tmp^, sizeof(TTrigger), 0); |
|
615 | 55 |
|
56 |
tmp^.id:= id; |
|
57 |
tmp^.Ticks:= Ticks; |
|
58 |
tmp^.TicksPerLife:= Ticks; |
|
59 |
tmp^.Lives:= Lives; |
|
60 |
||
613 | 61 |
if TriggerList <> nil then tmp^.Next:= TriggerList; |
62 |
TriggerList:= tmp; |
|
63 |
AddTrigger:= tmp |
|
64 |
end; |
|
65 |
||
615 | 66 |
procedure AddTriggerSpawner(id, Ticks, Lives: Longword; GearType: TGearType; X, Y: LongInt; GearTriggerId: Longword); |
589 | 67 |
var tmp: PTrigger; |
68 |
begin |
|
594 | 69 |
if (Ticks = 0) or (Lives = 0) then exit; |
589 | 70 |
|
615 | 71 |
tmp:= AddTrigger(id, Ticks, Lives); |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
72 |
tmp^.Action:= taSpawnGear; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
73 |
tmp^.X:= X; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
74 |
tmp^.Y:= Y; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
75 |
tmp^.SpawnGearType:= GearType; |
613 | 76 |
tmp^.SpawnGearTriggerId:= GearTriggerId |
77 |
end; |
|
78 |
||
615 | 79 |
procedure AddTriggerSuccess(id, Ticks, Lives: Longword); |
613 | 80 |
begin |
615 | 81 |
with AddTrigger(id, Ticks, Lives)^ do |
613 | 82 |
Action:= taSuccessFinish |
615 | 83 |
end; |
84 |
||
85 |
procedure AddTriggerFail(id, Ticks, Lives: Longword); |
|
86 |
begin |
|
87 |
with AddTrigger(id, Ticks, Lives)^ do |
|
88 |
Action:= taFailFinish |
|
589 | 89 |
end; |
90 |
||
593 | 91 |
procedure TickTriggerT(Trigger: PTrigger); |
92 |
begin |
|
615 | 93 |
{$IFDEF DEBUGFILE}AddFileLog('Tick trigger (type: ' + inttostr(LongWord(Trigger^.Action)) + ')');{$ENDIF} |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
94 |
with Trigger^ do |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
95 |
case Action of |
610 | 96 |
taSpawnGear: begin |
97 |
FollowGear:= AddGear(X, Y, SpawnGearType, 0, _0, _0, 0); |
|
98 |
FollowGear^.TriggerId:= SpawnGearTriggerId |
|
613 | 99 |
end; |
100 |
taSuccessFinish: begin |
|
101 |
GameState:= gsExit |
|
615 | 102 |
end; |
103 |
taFailFinish: begin |
|
104 |
GameState:= gsExit |
|
610 | 105 |
end |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
106 |
end |
593 | 107 |
end; |
108 |
||
109 |
procedure TickTrigger(id: Longword); |
|
594 | 110 |
var t, pt, nt: PTrigger; |
589 | 111 |
begin |
593 | 112 |
t:= TriggerList; |
594 | 113 |
pt:= nil; |
593 | 114 |
|
115 |
while (t <> nil) do |
|
116 |
begin |
|
594 | 117 |
nt:= t^.Next; |
118 |
if (t^.id = id) then |
|
593 | 119 |
begin |
120 |
dec(t^.Ticks); |
|
121 |
if (t^.Ticks = 0) then |
|
122 |
begin |
|
123 |
TickTriggerT(t); |
|
594 | 124 |
dec(t^.Lives); |
125 |
t^.Ticks:= t^.TicksPerLife; |
|
126 |
if (t^.Lives = 0) then |
|
127 |
begin |
|
615 | 128 |
if t = TriggerList then |
129 |
begin |
|
130 |
TriggerList:= nt; |
|
131 |
Dispose(t) |
|
132 |
end |
|
133 |
else |
|
134 |
begin |
|
135 |
pt^.Next:= nt; |
|
136 |
Dispose(t); |
|
137 |
t:= pt |
|
138 |
end |
|
594 | 139 |
end |
593 | 140 |
end |
594 | 141 |
end; |
142 |
pt:= t; |
|
143 |
t:= nt |
|
593 | 144 |
end |
589 | 145 |
end; |
146 |
||
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
147 |
procedure init_uTriggers; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
148 |
begin |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
149 |
TriggerList:= nil; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
150 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
151 |
|
2599 | 152 |
end. |