author | unc0rr |
Wed, 10 Mar 2010 06:50:06 +0000 | |
changeset 2972 | 82828fd23dea |
parent 2948 | 3f21a9dc93d0 |
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; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
29 |
procedure free_uTriggers; |
615 | 30 |
procedure AddTriggerSpawner(id, Ticks, Lives: Longword; GearType: TGearType; X, Y: LongInt; GearTriggerId: Longword); |
31 |
procedure AddTriggerSuccess(id, Ticks, Lives: Longword); |
|
32 |
procedure AddTriggerFail(id, Ticks, Lives: Longword); |
|
593 | 33 |
procedure TickTrigger(id: Longword); |
589 | 34 |
|
35 |
implementation |
|
610 | 36 |
uses uGears, uFloat, uMisc, uWorld; |
589 | 37 |
type PTrigger = ^TTrigger; |
38 |
TTrigger = record |
|
39 |
id: Longword; |
|
593 | 40 |
Ticks: Longword; |
594 | 41 |
Lives: Longword; |
42 |
TicksPerLife: LongWord; |
|
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
43 |
Action: TTrigAction; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
44 |
X, Y: LongInt; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
45 |
SpawnGearType: TGearType; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
46 |
SpawnGearTriggerId: Longword; |
589 | 47 |
Next: PTrigger; |
48 |
end; |
|
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
49 |
var TriggerList: PTrigger; |
589 | 50 |
|
615 | 51 |
function AddTrigger(id, Ticks, Lives: Longword): PTrigger; |
613 | 52 |
var tmp: PTrigger; |
53 |
begin |
|
54 |
new(tmp); |
|
55 |
FillChar(tmp^, sizeof(TTrigger), 0); |
|
615 | 56 |
|
57 |
tmp^.id:= id; |
|
58 |
tmp^.Ticks:= Ticks; |
|
59 |
tmp^.TicksPerLife:= Ticks; |
|
60 |
tmp^.Lives:= Lives; |
|
61 |
||
613 | 62 |
if TriggerList <> nil then tmp^.Next:= TriggerList; |
63 |
TriggerList:= tmp; |
|
64 |
AddTrigger:= tmp |
|
65 |
end; |
|
66 |
||
615 | 67 |
procedure AddTriggerSpawner(id, Ticks, Lives: Longword; GearType: TGearType; X, Y: LongInt; GearTriggerId: Longword); |
589 | 68 |
var tmp: PTrigger; |
69 |
begin |
|
594 | 70 |
if (Ticks = 0) or (Lives = 0) then exit; |
589 | 71 |
|
615 | 72 |
tmp:= AddTrigger(id, Ticks, Lives); |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
73 |
tmp^.Action:= taSpawnGear; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
74 |
tmp^.X:= X; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
75 |
tmp^.Y:= Y; |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
76 |
tmp^.SpawnGearType:= GearType; |
613 | 77 |
tmp^.SpawnGearTriggerId:= GearTriggerId |
78 |
end; |
|
79 |
||
615 | 80 |
procedure AddTriggerSuccess(id, Ticks, Lives: Longword); |
613 | 81 |
begin |
615 | 82 |
with AddTrigger(id, Ticks, Lives)^ do |
613 | 83 |
Action:= taSuccessFinish |
615 | 84 |
end; |
85 |
||
86 |
procedure AddTriggerFail(id, Ticks, Lives: Longword); |
|
87 |
begin |
|
88 |
with AddTrigger(id, Ticks, Lives)^ do |
|
89 |
Action:= taFailFinish |
|
589 | 90 |
end; |
91 |
||
593 | 92 |
procedure TickTriggerT(Trigger: PTrigger); |
93 |
begin |
|
615 | 94 |
{$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
|
95 |
with Trigger^ do |
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
96 |
case Action of |
610 | 97 |
taSpawnGear: begin |
98 |
FollowGear:= AddGear(X, Y, SpawnGearType, 0, _0, _0, 0); |
|
99 |
FollowGear^.TriggerId:= SpawnGearTriggerId |
|
613 | 100 |
end; |
101 |
taSuccessFinish: begin |
|
102 |
GameState:= gsExit |
|
615 | 103 |
end; |
104 |
taFailFinish: begin |
|
105 |
GameState:= gsExit |
|
610 | 106 |
end |
595
5ee863f2f568
Triggers PoC: targets are spawned right after the previous damaged
unc0rr
parents:
594
diff
changeset
|
107 |
end |
593 | 108 |
end; |
109 |
||
110 |
procedure TickTrigger(id: Longword); |
|
594 | 111 |
var t, pt, nt: PTrigger; |
589 | 112 |
begin |
593 | 113 |
t:= TriggerList; |
594 | 114 |
pt:= nil; |
593 | 115 |
|
116 |
while (t <> nil) do |
|
117 |
begin |
|
594 | 118 |
nt:= t^.Next; |
119 |
if (t^.id = id) then |
|
593 | 120 |
begin |
121 |
dec(t^.Ticks); |
|
122 |
if (t^.Ticks = 0) then |
|
123 |
begin |
|
124 |
TickTriggerT(t); |
|
594 | 125 |
dec(t^.Lives); |
126 |
t^.Ticks:= t^.TicksPerLife; |
|
127 |
if (t^.Lives = 0) then |
|
128 |
begin |
|
615 | 129 |
if t = TriggerList then |
130 |
begin |
|
131 |
TriggerList:= nt; |
|
132 |
Dispose(t) |
|
133 |
end |
|
134 |
else |
|
135 |
begin |
|
136 |
pt^.Next:= nt; |
|
137 |
Dispose(t); |
|
138 |
t:= pt |
|
139 |
end |
|
594 | 140 |
end |
593 | 141 |
end |
594 | 142 |
end; |
143 |
pt:= t; |
|
144 |
t:= nt |
|
593 | 145 |
end |
589 | 146 |
end; |
147 |
||
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
148 |
procedure init_uTriggers; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
149 |
begin |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2716
diff
changeset
|
150 |
TriggerList:= nil; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
151 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
152 |
|
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
153 |
procedure free_uTriggers; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
154 |
begin |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
155 |
|
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
156 |
end; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
157 |
|
2599 | 158 |
end. |