1 /***************************************************************************
2                           action.c  -  description
3                              -------------------
4     begin                : Mon Apr 1 2002
5     copyright            : (C) 2001 by Michael Speck
6     email                : kulkanie@gmx.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "lgeneral.h"
19 #include "unit.h"
20 #include "action.h"
21 
22 static List *actions = 0;
23 
24 /*
25 ====================================================================
26 Locals
27 ====================================================================
28 */
29 
30 /*
31 ====================================================================
32 Create basic action.
33 ====================================================================
34 */
action_create(int type)35 static Action *action_create( int type )
36 {
37     Action *action = calloc( 1, sizeof( Action ) );
38     action->type = type;
39     return action;
40 }
41 /*
42 ====================================================================
43 Queue an action.
44 ====================================================================
45 */
action_queue(Action * action)46 static void action_queue( Action *action )
47 {
48     list_add( actions, action );
49 }
50 
51 /*
52 ====================================================================
53 Publics
54 ====================================================================
55 */
56 
57 /*
58 ====================================================================
59 Create/delete engine action queue
60 ====================================================================
61 */
actions_create()62 void actions_create()
63 {
64     actions_delete();
65     actions = list_create( LIST_NO_AUTO_DELETE, LIST_NO_CALLBACK );
66 }
actions_delete()67 void actions_delete()
68 {
69     if ( actions ) {
70         actions_clear();
71         list_delete( actions );
72         actions = 0;
73     }
74 }
75 
76 /*
77 ====================================================================
78 Get next action or clear all actions. The returned action struct
79 must be cleared by engine after usage.
80 ====================================================================
81 */
actions_dequeue()82 Action* actions_dequeue()
83 {
84     return list_dequeue( actions );
85 }
actions_clear()86 void actions_clear()
87 {
88     Action *action = 0;
89     while ( ( action = list_dequeue( actions ) ) )
90         free( action );
91 }
92 
93 /*
94 ====================================================================
95 Returns topmost action in queue or 0 if none available.
96 ====================================================================
97 */
actions_top(void)98 Action *actions_top(void)
99 {
100     return list_last( actions );
101 }
102 
103 /*
104 ====================================================================
105 Remove the last action in queue (cancelled confirmation)
106 ====================================================================
107 */
action_remove_last()108 void action_remove_last()
109 {
110     Action *action = 0;
111     if ( actions->count == 0 ) return;
112     action = list_last( actions );
113     list_delete_item( actions, action );
114     free( action );
115 }
116 
117 /*
118 ====================================================================
119 Get number of queued actions
120 ====================================================================
121 */
actions_count()122 int actions_count()
123 {
124     return actions->count;
125 }
126 
127 /*
128 ====================================================================
129 Create an engine action and automatically queue it. The engine
130 will perform security checks before handling an action to prevent
131 illegal actions.
132 ====================================================================
133 */
action_queue_none()134 void action_queue_none()
135 {
136     Action *action = action_create( ACTION_NONE );
137     action_queue( action );
138 }
action_queue_end_turn()139 void action_queue_end_turn()
140 {
141     Action *action = action_create( ACTION_END_TURN );
142     action_queue( action );
143 }
action_queue_move(Unit * unit,int x,int y)144 void action_queue_move( Unit *unit, int x, int y )
145 {
146     Action *action = action_create( ACTION_MOVE );
147     action->unit = unit;
148     action->x = x; action->y = y;
149     action_queue( action );
150 }
action_queue_attack(Unit * unit,Unit * target)151 void action_queue_attack( Unit *unit, Unit *target )
152 {
153     Action *action = action_create( ACTION_ATTACK );
154     action->unit = unit;
155     action->target = target;
156     action_queue( action );
157 }
action_queue_strategic_attack(Unit * unit)158 void action_queue_strategic_attack( Unit *unit)
159 {
160     Action *action = action_create( ACTION_STRATEGIC_ATTACK );
161     action->unit = unit;
162     action_queue( action );
163 }
action_queue_supply(Unit * unit)164 void action_queue_supply( Unit *unit )
165 {
166     Action *action = action_create( ACTION_SUPPLY );
167     action->unit = unit;
168     action_queue( action );
169 }
action_queue_embark_sea(Unit * unit,int x,int y)170 void action_queue_embark_sea( Unit *unit, int x, int y )
171 {
172     Action *action = action_create( ACTION_EMBARK_SEA );
173     action->unit = unit;
174     action->x = x; action->y = y;
175     action_queue( action );
176 }
action_queue_debark_sea(Unit * unit,int x,int y)177 void action_queue_debark_sea( Unit *unit, int x, int y )
178 {
179     Action *action = action_create( ACTION_DEBARK_SEA );
180     action->unit = unit;
181     action->x = x; action->y = y;
182     action_queue( action );
183 }
action_queue_embark_air(Unit * unit,int x,int y)184 void action_queue_embark_air( Unit *unit, int x, int y )
185 {
186     Action *action = action_create( ACTION_EMBARK_AIR );
187     action->unit = unit;
188     action->x = x; action->y = y;
189     action_queue( action );
190 }
action_queue_debark_air(Unit * unit,int x,int y,int normal_landing)191 void action_queue_debark_air( Unit *unit, int x, int y, int normal_landing )
192 {
193     Action *action = action_create( ACTION_DEBARK_AIR );
194     action->unit = unit;
195     action->x = x; action->y = y;
196     action->id = normal_landing;
197     action_queue( action );
198 }
action_queue_merge(Unit * unit,Unit * partner)199 void action_queue_merge( Unit *unit, Unit *partner )
200 {
201     Action *action = action_create( ACTION_MERGE );
202     action->unit = unit;
203     action->target = partner;
204     action_queue( action );
205 }
action_queue_split(Unit * unit,int str,int x,int y,Unit * partner)206 void action_queue_split( Unit *unit, int str, int x, int y, Unit *partner )
207 {
208     Action *action = action_create( ACTION_SPLIT );
209     action->unit = unit;
210     action->target = partner;
211     action->x = x; action->y = y;
212     action->str = str;
213     action_queue( action );
214 }
action_queue_disband(Unit * unit)215 void action_queue_disband( Unit *unit )
216 {
217     Action *action = action_create( ACTION_DISBAND );
218     action->unit = unit;
219     action_queue( action );
220 }
action_queue_deploy(Unit * unit,int x,int y)221 void action_queue_deploy( Unit *unit, int x, int y )
222 {
223     Action *action = action_create( ACTION_DEPLOY );
224     action->unit = unit;
225     action->x = x;
226     action->y = y;
227     action_queue( action );
228 }
action_queue_draw_map()229 void action_queue_draw_map()
230 {
231     Action *action = action_create( ACTION_DRAW_MAP );
232     action_queue( action );
233 }
action_queue_set_spot_mask()234 void action_queue_set_spot_mask()
235 {
236     Action *action = action_create( ACTION_SET_SPOT_MASK );
237     action_queue( action );
238 }
action_queue_set_vmode(int w,int h,int fullscreen)239 void action_queue_set_vmode( int w, int h, int fullscreen )
240 {
241     Action *action = action_create( ACTION_SET_VMODE );
242     action->w = w; action->h = h;
243     action->full = fullscreen;
244     action_queue( action );
245 }
action_queue_quit()246 void action_queue_quit()
247 {
248     Action *action = action_create( ACTION_QUIT );
249     action_queue( action );
250 }
action_queue_restart()251 void action_queue_restart()
252 {
253     Action *action = action_create( ACTION_RESTART );
254     action_queue( action );
255 }
action_queue_load(int id)256 void action_queue_load( int id )
257 {
258     Action *action = action_create( ACTION_LOAD );
259     action->id = id;
260     action_queue( action );
261 }
action_queue_overwrite(int id)262 void action_queue_overwrite( int id )
263 {
264     Action *action = action_create( ACTION_OVERWRITE );
265     action->id = id;
266     action_queue( action );
267 }
action_queue_start_scen()268 void action_queue_start_scen()
269 {
270     Action *action = action_create( ACTION_START_SCEN );
271     action_queue( action );
272 }
action_queue_start_camp()273 void action_queue_start_camp()
274 {
275     Action *action = action_create( ACTION_START_CAMP );
276     action_queue( action );
277 }
278