1 //
2 //
3 
4 #include "particle.h"
5 #include "vecmath.h"
6 #include "object.h"
7 
8 namespace scripting {
9 namespace api {
10 
particle_h()11 particle_h::particle_h() {
12 }
particle_h(const particle::WeakParticlePtr & part_p)13 particle_h::particle_h(const particle::WeakParticlePtr& part_p) {
14 	this->part = part_p;
15 }
Get()16 particle::WeakParticlePtr particle_h::Get() {
17 	return this->part;
18 }
isValid()19 bool particle_h::isValid() {
20 	return !part.expired();
21 }
22 
23 
24 //**********HANDLE: Particle
25 ADE_OBJ(l_Particle, particle_h, "particle", "Handle to a particle");
26 
27 ADE_VIRTVAR(Position, l_Particle, "vector", "The current position of the particle (world vector)", "vector", "The current position")
28 {
29 	particle_h *ph = NULL;
30 	vec3d newVec = vmd_zero_vector;
31 	if (!ade_get_args(L, "o|o", l_Particle.GetPtr(&ph), l_Vector.Get(&newVec)))
32 		return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
33 
34 	if (ph == NULL)
35 		return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
36 
37 	if (!ph->isValid())
38 		return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
39 
40 	if (ADE_SETTING_VAR)
41 	{
42 		ph->Get().lock()->pos = newVec;
43 	}
44 
45 	return ade_set_args(L, "o", l_Vector.Set(ph->Get().lock()->pos));
46 }
47 
48 ADE_VIRTVAR(Velocity, l_Particle, "vector", "The current velocity of the particle (world vector)", "vector", "The current velocity")
49 {
50 	particle_h *ph = NULL;
51 	vec3d newVec = vmd_zero_vector;
52 	if (!ade_get_args(L, "o|o", l_Particle.GetPtr(&ph), l_Vector.Get(&newVec)))
53 		return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
54 
55 	if (ph == NULL)
56 		return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
57 
58 	if (!ph->isValid())
59 		return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
60 
61 	if (ADE_SETTING_VAR)
62 	{
63 		ph->Get().lock()->velocity = newVec;
64 	}
65 
66 	return ade_set_args(L, "o", l_Vector.Set(ph->Get().lock()->velocity));
67 }
68 
69 ADE_VIRTVAR(Age, l_Particle, "number", "The time this particle already lives", "number", "The current age or -1 on error")
70 {
71 	particle_h *ph = NULL;
72 	float newAge = -1.0f;
73 	if (!ade_get_args(L, "o|f", l_Particle.GetPtr(&ph), &newAge))
74 		return ade_set_error(L, "f", -1.0f);
75 
76 	if (ph == NULL)
77 		return ade_set_error(L, "f", -1.0f);
78 
79 	if (!ph->isValid())
80 		return ade_set_error(L, "f", -1.0f);
81 
82 	if (ADE_SETTING_VAR)
83 	{
84 		if (newAge >= 0)
85 			ph->Get().lock()->age = newAge;
86 	}
87 
88 	return ade_set_args(L, "f", ph->Get().lock()->age);
89 }
90 
91 ADE_VIRTVAR(MaximumLife, l_Particle, "number", "The time this particle can live", "number", "The maximal life or -1 on error")
92 {
93 	particle_h *ph = NULL;
94 	float newLife = -1.0f;
95 	if (!ade_get_args(L, "o|f", l_Particle.GetPtr(&ph), &newLife))
96 		return ade_set_error(L, "f", -1.0f);
97 
98 	if (ph == NULL)
99 		return ade_set_error(L, "f", -1.0f);
100 
101 	if (!ph->isValid())
102 		return ade_set_error(L, "f", -1.0f);
103 
104 	if (ADE_SETTING_VAR)
105 	{
106 		if (newLife >= 0)
107 			ph->Get().lock()->max_life = newLife;
108 	}
109 
110 	return ade_set_args(L, "f", ph->Get().lock()->max_life);
111 }
112 
113 ADE_VIRTVAR(Looping, l_Particle, "boolean",
114             "The looping status of the particle. If a particle loops then it will not be removed when its max_life "
115             "value has been reached. Instead its animation will be reset to the start. When the particle should "
116             "finally be removed then set this to false and set MaxLife to 0.",
117             "boolean", "The looping status")
118 {
119 	particle_h* ph = nullptr;
120 	bool newloop   = false;
121 	if (!ade_get_args(L, "o|b", l_Particle.GetPtr(&ph), &newloop))
122 		return ADE_RETURN_FALSE;
123 
124 	if (ph == nullptr)
125 		return ADE_RETURN_FALSE;
126 
127 	if (!ph->isValid())
128 		return ADE_RETURN_FALSE;
129 
130 	if (ADE_SETTING_VAR) {
131 		ph->Get().lock()->looping = newloop;
132 	}
133 
134 	return ade_set_args(L, "b", ph->Get().lock()->looping);
135 }
136 
137 ADE_VIRTVAR(Radius, l_Particle, "number", "The radius of the particle", "number", "The radius or -1 on error")
138 {
139 	particle_h *ph = NULL;
140 	float newRadius = -1.0f;
141 	if (!ade_get_args(L, "o|f", l_Particle.GetPtr(&ph), &newRadius))
142 		return ade_set_error(L, "f", -1.0f);
143 
144 	if (ph == NULL)
145 		return ade_set_error(L, "f", -1.0f);
146 
147 	if (!ph->isValid())
148 		return ade_set_error(L, "f", -1.0f);
149 
150 	if (ADE_SETTING_VAR)
151 	{
152 		if (newRadius >= 0)
153 			ph->Get().lock()->radius = newRadius;
154 	}
155 
156 	return ade_set_args(L, "f", ph->Get().lock()->radius);
157 }
158 
159 ADE_VIRTVAR(TracerLength, l_Particle, "number", "The tracer legth of the particle", "number", "The radius or -1 on error")
160 {
161 	particle_h *ph = NULL;
162 	float newTracer = -1.0f;
163 	if (!ade_get_args(L, "o|f", l_Particle.GetPtr(&ph), &newTracer))
164 		return ade_set_error(L, "f", -1.0f);
165 
166 	if (ph == NULL)
167 		return ade_set_error(L, "f", -1.0f);
168 
169 	if (!ph->isValid())
170 		return ade_set_error(L, "f", -1.0f);
171 
172 	// tracer_length has been deprecated
173 	return ade_set_args(L, "f", -1.0f);
174 }
175 
176 ADE_VIRTVAR(AttachedObject, l_Particle, "object", "The object this particle is attached to. If valid the position will be relativ to this object and the velocity will be ignored.", "object", "Attached object or invalid object handle on error")
177 {
178 	particle_h *ph = NULL;
179 	object_h *newObj = nullptr;
180 	if (!ade_get_args(L, "o|o", l_Particle.GetPtr(&ph), l_Object.GetPtr(&newObj)))
181 		return ade_set_error(L, "o", l_Object.Set(object_h()));
182 
183 	if (ph == NULL)
184 		return ade_set_error(L, "o", l_Object.Set(object_h()));
185 
186 	if (!ph->isValid())
187 		return ade_set_error(L, "o", l_Object.Set(object_h()));
188 
189 	if (ADE_SETTING_VAR)
190 	{
191 		if (newObj != nullptr && newObj->IsValid())
192 			ph->Get().lock()->attached_objnum = newObj->objp->signature;
193 	}
194 
195 	return ade_set_args(L, "o", l_Object.Set(object_h(&Objects[ph->Get().lock()->attached_objnum])));
196 }
197 
198 ADE_FUNC(isValid, l_Particle, NULL, "Detects whether this handle is valid", "boolean", "true if valid false if not")
199 {
200 	particle_h *ph = NULL;
201 	if (!ade_get_args(L, "o", l_Particle.GetPtr(&ph)))
202 		return ADE_RETURN_FALSE;
203 
204 	if (ph == NULL)
205 		return ADE_RETURN_FALSE;
206 
207 	return ade_set_args(L, "b", ph->isValid());
208 }
209 
210 
211 }
212 }
213 
214