File: WaitAction.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (48 lines) | stat: -rw-r--r-- 1,248 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

#include "WaitAction.h"

#include "io/timer.h"
#include "parse/parselo.h"

namespace actions {
namespace types {

flagset<ProgramContextFlags> WaitAction::getRequiredExecutionContextFlags()
{
	return flagset<ProgramContextFlags>{};
}

WaitAction::WaitAction(expression::TypedActionExpression<float> waitTimeExpression)
	: _waitTimeExpression(std::move(waitTimeExpression))
{
}
WaitAction::~WaitAction() = default;

ActionResult WaitAction::execute(actions::ProgramLocals& locals) const
{
	if (!locals.waitTimestamp.isValid()) {
		auto waitTime = _waitTimeExpression.execute(locals.variables);

		// Catch errors in the expression
		if (waitTime < 0.001f) {
			waitTime = 0.001f;
		}

		locals.waitTimestamp = _timestamp(fl2i(waitTime * MILLISECONDS_PER_SECOND));
	}
	if (!timestamp_elapsed(locals.waitTimestamp)) {
		// Wait until the timestamp is elapsed
		return ActionResult::Wait;
	}
	// Timestamp is elapsed. The timestamp is elapsed so that timestamp_valid above returns false for the next wait
	// action
	locals.waitTimestamp = TIMESTAMP::invalid();
	return ActionResult::Finished;
}
std::unique_ptr<Action> WaitAction::clone() const
{
	return std::unique_ptr<Action>(new WaitAction(*this));
}

} // namespace types
} // namespace actions