File: modelanimation_driver.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (119 lines) | stat: -rw-r--r-- 4,164 bytes parent folder | download
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
#include "modelanimation_driver.h"

#include "hud/hudets.h"
#include "model/animation/modelanimation.h"
#include "ship/ship.h"

namespace animation {
	std::function<float(polymodel_instance*)> parse_generic_property_driver_source() {
		switch(optional_string_one_of(1,
				"Random"
				)){
			case 0: {
				float min, max;
				stuff_float(&min);
				stuff_float(&max);
				const int seed = util::Random::next();
				return [min, max, seed](polymodel_instance *pmi) -> float {
					//We want something that is as random as possible, however, if the same driver
					// is called from the same object, the same result should be produced.
					//But since driver objects are shared between animation sets (i.e. ship classes),
					// just assigning a random to each driver object is not sufficient.
					return static_cast<float>(std::mt19937(seed ^ pmi->objnum)()) / static_cast<float>(std::mt19937::max()) * (max - min) + min;
				};
			}
			default:
				return {};
		}
	}

	template<typename property, property object::* property_ptr, float property::* subproperty_ptr>
	static float get_object_subproperty_float(polymodel_instance* pmi){
		Assertion(pmi->objnum >= 0, "Invalid object used in animation property driver!");
		return Objects[pmi->objnum].*property_ptr.*subproperty_ptr;
	}

	template<matrix object::* property_ptr, float angles::* angle>
	static float get_object_matrix_angle(polymodel_instance* pmi){
		Assertion(pmi->objnum >= 0, "Invalid object used in animation property driver!");
		angles a;
		vm_extract_angles_matrix(&a, &(Objects[pmi->objnum].*property_ptr));
		return a.*angle;
	}

	std::function<float(polymodel_instance*)> parse_object_property_driver_source() {
		auto precedence = parse_generic_property_driver_source();
		if (precedence)
			return precedence;

		switch(optional_string_one_of(5,
				"SpeedForward",
				"Speed",
				"Pitch",
				"Bank",
				"Heading"
				)){
			case 0:
				return get_object_subproperty_float<physics_info, &object::phys_info, &physics_info::fspeed>;
			case 1:
				return get_object_subproperty_float<physics_info, &object::phys_info, &physics_info::speed>;
			case 2:
				return get_object_matrix_angle<&object::orient, &angles::p>;
			case 3:
				return get_object_matrix_angle<&object::orient, &angles::b>;
			case 4:
				return get_object_matrix_angle<&object::orient, &angles::h>;
			default:
				return {};
		}
	}

	template<typename property, property ship::* property_ptr, float property::* subproperty_ptr>
	static float get_ship_subproperty_float(polymodel_instance* pmi){
		Assertion(pmi->objnum >= 0, "Invalid object used in animation property driver!");
		Assertion(Objects[pmi->objnum].type == OBJ_SHIP, "Non-ship object used in ship animation property driver!");
		return Ships[Objects[pmi->objnum].instance].*property_ptr.*subproperty_ptr;
	}

	template<int ship::* ets_property>
	static float get_ship_ets_property(polymodel_instance* pmi){
		Assertion(pmi->objnum >= 0, "Invalid object used in animation property driver!");
		Assertion(Objects[pmi->objnum].type == OBJ_SHIP, "Non-ship object used in ship animation property driver!");
		return Energy_levels[Ships[Objects[pmi->objnum].instance].*ets_property];
	}

	std::function<float(polymodel_instance*)> parse_ship_property_driver_source() {
		auto precedence = parse_object_property_driver_source();
		if (precedence)
			return precedence;

		switch(optional_string_one_of(3,
				"ETSShield",
				"ETSEngine",
				"ETSWeapons"
				)){
			case 0:
				return get_ship_ets_property<&ship::shield_recharge_index>;
			case 1:
				return get_ship_ets_property<&ship::engine_recharge_index>;
			case 2:
				return get_ship_ets_property<&ship::weapon_recharge_index>;
			default:
				return {};
		}
	}

	ModelAnimationPropertyDriverTarget parse_property_driver_target() {
		switch(optional_string_one_of(2,
				"Speed",
				"Time" //Is dangerous as a property driver target, but makes sense as a startup driver
				)){
			case 0:
				return {&ModelAnimation::instance_data::speed, tl::nullopt};
			case 1:
				return {&ModelAnimation::instance_data::time, &ModelAnimation::instance_data::duration};
			default:
				return {};
		}
	}
}