author | koda |
Thu, 05 Nov 2009 16:23:09 +0000 | |
changeset 2599 | c7153d2348f3 |
parent 1066 | 1f1b3686a2b0 |
child 2630 | 079ef82eac75 |
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 |
||
19 |
unit uTriggers; |
|
20 |
||
21 |
interface |
|
22 |
uses SDLh, uConsts; |
|
2599 | 23 |
{$INCLUDE "options.inc"} |
593 | 24 |
const trigTurns = $80000001; |
589 | 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 |
|
615 | 28 |
procedure AddTriggerSpawner(id, Ticks, Lives: Longword; GearType: TGearType; X, Y: LongInt; GearTriggerId: Longword); |
29 |
procedure AddTriggerSuccess(id, Ticks, Lives: Longword); |
|
30 |
procedure AddTriggerFail(id, Ticks, Lives: Longword); |
|
593 | 31 |
procedure TickTrigger(id: Longword); |
589 | 32 |
|
33 |
implementation |
|
610 | 34 |
uses uGears, uFloat, uMisc, uWorld; |
589 | 35 |
type PTrigger = ^TTrigger; |
36 |
TTrigger = record |
|
37 |
id: Longword; |
|
593 | 38 |
Ticks: Longword; |
594 | 39 |
Lives: Longword; |
40 |
TicksPerLife: LongWord; |
|
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
41 |
Action: TTrigAction; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
42 |
X, Y: LongInt; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
43 |
SpawnGearType: TGearType; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
44 |
SpawnGearTriggerId: Longword; |
589 | 45 |
Next: PTrigger; |
46 |
end; |
|
47 |
var TriggerList: PTrigger = nil; |
|
48 |
||
615 | 49 |
function AddTrigger(id, Ticks, Lives: Longword): PTrigger; |
613 | 50 |
var tmp: PTrigger; |
51 |
begin |
|
52 |
new(tmp); |
|
53 |
FillChar(tmp^, sizeof(TTrigger), 0); |
|
615 | 54 |
|
55 |
tmp^.id:= id; |
|
56 |
tmp^.Ticks:= Ticks; |
|
57 |
tmp^.TicksPerLife:= Ticks; |
|
58 |
tmp^.Lives:= Lives; |
|
59 |
||
613 | 60 |
if TriggerList <> nil then tmp^.Next:= TriggerList; |
61 |
TriggerList:= tmp; |
|
62 |
AddTrigger:= tmp |
|
63 |
end; |
|
64 |
||
615 | 65 |
procedure AddTriggerSpawner(id, Ticks, Lives: Longword; GearType: TGearType; X, Y: LongInt; GearTriggerId: Longword); |
589 | 66 |
var tmp: PTrigger; |
67 |
begin |
|
594 | 68 |
if (Ticks = 0) or (Lives = 0) then exit; |
589 | 69 |
|
615 | 70 |
tmp:= AddTrigger(id, Ticks, Lives); |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
71 |
tmp^.Action:= taSpawnGear; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
72 |
tmp^.X:= X; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
73 |
tmp^.Y:= Y; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
74 |
tmp^.SpawnGearType:= GearType; |
613 | 75 |
tmp^.SpawnGearTriggerId:= GearTriggerId |
76 |
end; |
|
77 |
||
615 | 78 |
procedure AddTriggerSuccess(id, Ticks, Lives: Longword); |
613 | 79 |
begin |
615 | 80 |
with AddTrigger(id, Ticks, Lives)^ do |
613 | 81 |
Action:= taSuccessFinish |
615 | 82 |
end; |
83 |
||
84 |
procedure AddTriggerFail(id, Ticks, Lives: Longword); |
|
85 |
begin |
|
86 |
with AddTrigger(id, Ticks, Lives)^ do |
|
87 |
Action:= taFailFinish |
|
589 | 88 |
end; |
89 |
||
593 | 90 |
procedure TickTriggerT(Trigger: PTrigger); |
91 |
begin |
|
615 | 92 |
{$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
|
93 |
with Trigger^ do |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
94 |
case Action of |
610 | 95 |
taSpawnGear: begin |
96 |
FollowGear:= AddGear(X, Y, SpawnGearType, 0, _0, _0, 0); |
|
97 |
FollowGear^.TriggerId:= SpawnGearTriggerId |
|
613 | 98 |
end; |
99 |
taSuccessFinish: begin |
|
100 |
GameState:= gsExit |
|
615 | 101 |
end; |
102 |
taFailFinish: begin |
|
103 |
GameState:= gsExit |
|
610 | 104 |
end |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
105 |
end |
593 | 106 |
end; |
107 |
||
108 |
procedure TickTrigger(id: Longword); |
|
594 | 109 |
var t, pt, nt: PTrigger; |
589 | 110 |
begin |
593 | 111 |
t:= TriggerList; |
594 | 112 |
pt:= nil; |
593 | 113 |
|
114 |
while (t <> nil) do |
|
115 |
begin |
|
594 | 116 |
nt:= t^.Next; |
117 |
if (t^.id = id) then |
|
593 | 118 |
begin |
119 |
dec(t^.Ticks); |
|
120 |
if (t^.Ticks = 0) then |
|
121 |
begin |
|
122 |
TickTriggerT(t); |
|
594 | 123 |
dec(t^.Lives); |
124 |
t^.Ticks:= t^.TicksPerLife; |
|
125 |
if (t^.Lives = 0) then |
|
126 |
begin |
|
615 | 127 |
if t = TriggerList then |
128 |
begin |
|
129 |
TriggerList:= nt; |
|
130 |
Dispose(t) |
|
131 |
end |
|
132 |
else |
|
133 |
begin |
|
134 |
pt^.Next:= nt; |
|
135 |
Dispose(t); |
|
136 |
t:= pt |
|
137 |
end |
|
594 | 138 |
end |
593 | 139 |
end |
594 | 140 |
end; |
141 |
pt:= t; |
|
142 |
t:= nt |
|
593 | 143 |
end |
589 | 144 |
end; |
145 |
||
2599 | 146 |
end. |