File: tqueue.cc

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (328 lines) | stat: -rw-r--r-- 8,506 bytes parent folder | download | duplicates (2)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 *  tqueue.cc - A queue of time-based events for animation.
 *
 *  Copyright (C) 2000-2022  The Exult Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include "tqueue.h"

#include "actors.h"
#include "cheat.h"
#include "gamewin.h"

#include <algorithm>

#ifdef __GNUC__
#	pragma GCC diagnostic push
#	pragma GCC diagnostic ignored "-Wold-style-cast"
#	pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif    // __GNUC__
#include <SDL.h>
#ifdef __GNUC__
#	pragma GCC diagnostic pop
#endif    // __GNUC__

/*
 *  Remove all entries.
 */

void Time_queue::clear() {
	for (auto& ent : data) {
		ent.handler->dequeue();
	}
	data.clear();
}

/*
 *  Add an entry to the queue.
 */

void Time_queue::add(
		uint32 t, std::shared_ptr<Time_sensitive> obj, uintptr ud) {
	obj->queue_cnt++;    // It's going in, no matter what.
	Queue_entry newent;
	if (paused && !obj->always) {    // Paused?
		// Messy, but we need to fix time.
		t -= SDL_GetTicks() - pause_time;
	}
	newent.set(t, nullptr, ud, obj);
	auto insertionPoint = std::upper_bound(data.begin(), data.end(), newent);
	data.insert(insertionPoint, newent);
}

void Time_queue::add(
		uint32          t,      // When entry is to be activated.
		Time_sensitive* obj,    // Object to be added.
		uintptr         ud      // User data.
) {
	obj->queue_cnt++;    // It's going in, no matter what.
	Queue_entry newent;
	if (paused && !obj->always) {    // Paused?
		// Messy, but we need to fix time.
		t -= SDL_GetTicks() - pause_time;
	}
	newent.set(t, obj, ud, nullptr);
	auto insertionPoint = std::upper_bound(data.begin(), data.end(), newent);
	data.insert(insertionPoint, newent);
}

bool operator<(const Queue_entry& q1, const Queue_entry& q2) {
	return q1.time < q2.time;
}

/*
 *  Remove first entry containing a given object.
 *
 *  Output: 1 if found, else 0.
 */

bool Time_queue::remove(Time_sensitive* obj) {
	auto toRemove
			= std::find_if(data.begin(), data.end(), [obj](const auto& el) {
				  return el.handler == obj || el.sp_handler.get() == obj;
			  });
	auto found = toRemove != data.end();
	if (found) {
		toRemove->handler->queue_cnt--;
		data.erase(toRemove);
	}
	return found;
}

bool Time_queue::remove(std::shared_ptr<Time_sensitive> obj) {
	auto toRemove
			= std::find_if(data.begin(), data.end(), [obj](const auto& el) {
				  return el.handler == obj.get() || el.sp_handler == obj;
			  });
	auto found = toRemove != data.end();
	if (found) {
		toRemove->handler->queue_cnt--;
		data.erase(toRemove);
	}
	return found;
}

/*
 *  Remove first entry containing a given object and data.
 *
 *  Output: 1 if found, else 0.
 */

bool Time_queue::remove(Time_sensitive* obj, uintptr udata) {
	auto       it = std::find_if(data.begin(), data.end(), [&](const auto& el) {
        return el.handler == obj && el.udata == udata;
    });
	const bool found = it != data.end();
	if (found) {
		obj->queue_cnt--;
		data.erase(it);
	}
	return found;
}

bool Time_queue::remove(std::shared_ptr<Time_sensitive> obj, uintptr udata) {
	auto       it = std::find_if(data.begin(), data.end(), [&](const auto& el) {
        return (el.handler == obj.get() || el.sp_handler == obj)
               && el.udata == udata;
    });
	const bool found = it != data.end();
	if (found) {
		obj->queue_cnt--;
		data.erase(it);
	}
	return found;
}

/*
 *  See if a given entry is in the queue.
 *
 *  Output: true if found, else false.
 */

bool Time_queue::find(const Time_sensitive* obj) const {
	return std::find_if(
				   data.begin(), data.end(),
				   [&](const auto& el) {
					   return el.handler == obj || el.sp_handler.get() == obj;
				   })
		   != data.end();
}

/*
 *  Find when an entry is due.
 *
 *  Output: delay in msecs. when due, or -1 if not in queue.
 */

long Time_queue::find_delay(const Time_sensitive* obj, uint32 curtime) const {
	auto found = std::find_if(data.begin(), data.end(), [&](const auto& el) {
		return obj == el.handler || obj == el.sp_handler.get();
	});
	if (found == data.end()) {
		return -1;
	}
	if (pause_time) {    // Watch for case when paused.
		curtime = pause_time;
	}
	const long delay = found->time - curtime;
	return delay >= 0 ? delay : 0;
}

void Time_queue::activate(uint32 curtime) {
	if (cheat.in_map_editor()) {
		activate_mapedit(curtime);
	} else if (paused > 0) {
		activate_always(curtime);
	} else if (!data.empty() && !(curtime < data.front().time)) {
		activate0(curtime);
	}
}

/*
 *  Remove & activate entries that are due, starting with head (already
 *  known to be due).
 */

void Time_queue::activate0(uint32 curtime    // Current time.
) {
	do {
		Queue_entry                     ent    = data.front();
		Time_sensitive*                 obj    = ent.handler;
		std::shared_ptr<Time_sensitive> sp_obj = ent.sp_handler;
		const uintptr                   udata  = ent.udata;
		data.pop_front();    // Remove from chain.

		if (!obj && sp_obj) {
			obj = sp_obj.get();
		}
		if (obj) {
			obj->queue_cnt--;
			obj->handle_event(curtime, udata);
		}

	} while (!data.empty() && !(curtime < data.front().time));
}

/*
 *  Remove & activate entries marked 'always'.  This is called when
 *  the queue is paused.
 */

void Time_queue::activate_always(uint32 curtime    // Current time.
) {
	if (data.empty()) {
		return;
	}
	for (auto it = data.begin(); it != data.end() && !(curtime < it->time);) {
		auto next = it;
		++next;    // Get ->next in case we erase.
		Queue_entry                     ent    = *it;
		std::shared_ptr<Time_sensitive> sp_obj = ent.sp_handler;
		Time_sensitive*                 obj    = ent.handler;
		if (obj == nullptr && sp_obj) {
			obj = sp_obj.get();
		}
		if (obj != nullptr && obj->always) {
			obj->queue_cnt--;
			const uintptr udata = ent.udata;
			data.erase(it);
			obj->handle_event(curtime, udata);
		}
		it = next;
	}
}

/*
 *  Remove & activate only the avatar.  This is called when
 *  map edit mode is enabled.
 */

void Time_queue::activate_mapedit(uint32 curtime    // Current time.
) {
	if (data.empty()) {
		return;
	}

	const Main_actor* const avatar
			= Game_window::get_instance()->get_main_actor();
	for (auto it = data.begin(); it != data.end() && !(curtime < it->time);) {
		auto next = it;
		++next;    // Get ->next in case we erase.
		Queue_entry                     ent    = *it;
		std::shared_ptr<Time_sensitive> sp_obj = ent.sp_handler;
		Time_sensitive*                 obj    = ent.handler;
		if (obj == nullptr && sp_obj) {
			obj = sp_obj.get();
		}
		if (obj != nullptr && (obj == avatar || obj->always)) {
			obj->queue_cnt--;
			const uintptr udata = ent.udata;
			data.erase(it);
			obj->handle_event(curtime, udata);
		}
		it = next;
	}
}

/*
 *  Resume after a pause.
 */

void Time_queue::resume(uint32 curtime) {
	if (paused == 0 || --paused > 0) {    // Only unpause when stack empty.
		return;                           // Not paused.
	}
	const int diff = curtime - pause_time;
	pause_time     = 0;
	if (diff < 0) {    // Should not happen.
		return;
	}
	for (auto& it : data) {
		if (!it.handler->always) {
			it.time += diff;    // Push entries ahead.
		}
	}
}

/*
 *  Get next element in queue.
 */

bool Time_queue_iterator::operator()(
		Time_sensitive*& obj,    // Main object.
		uintptr&         data    // Data that was added with it.
) {
	auto remain = iter;
	iter        = this_obj == nullptr
						  ? remain
						  : std::find_if(
                             iter, tqueue->data.end(), [&](const auto& el) {
                                 return el.handler == this_obj
                                        || el.sp_handler.get() == this_obj;
                             });
	if (iter == tqueue->data.end()) {
		return false;
	}
	obj  = iter->handler;    // Return fields.
	data = iter->udata;
	++iter;    // On to the next.
	return true;
}