summaryrefslogtreecommitdiffstats
path: root/src/autoegg.cpp
blob: a8eec72b6e1da931392fbf3a3ccb607b6a13bf40 (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// autoegg.cpp

#define STRICT
#define D3D_OVERLOADS

#include <windows.h>
#include <stdio.h>
#include <d3d.h>

#include "struct.h"
#include "D3DEngine.h"
#include "D3DMath.h"
#include "event.h"
#include "misc.h"
#include "iman.h"
#include "math3d.h"
#include "particule.h"
#include "terrain.h"
#include "camera.h"
#include "object.h"
#include "pyro.h"
#include "cmdtoken.h"
#include "auto.h"
#include "autoegg.h"



// Constructeur de l'objet.

CAutoEgg::CAutoEgg(CInstanceManager* iMan, CObject* object)
				   : CAuto(iMan, object)
{
	CAuto::CAuto(iMan, object);

	m_type = OBJECT_NULL;
	m_value = 0.0f;
	m_string[0] = 0;

	m_param = 0;
	m_phase = AEP_NULL;
	Init();
}

// Destructeur de l'objet.

CAutoEgg::~CAutoEgg()
{
	CAuto::~CAuto();
}


// D�truit l'objet.

void CAutoEgg::DeleteObject(BOOL bAll)
{
	CObject*	alien;

	CAuto::DeleteObject(bAll);

	if ( !bAll )
	{
		alien = SearchAlien();
		if ( alien != 0 )
		{
			if ( alien->RetZoom(0) == 1.0f )
			{
				alien->SetLock(FALSE);
				alien->SetActivity(TRUE);  // l'insect n� est actif
			}
			else
			{
				alien->DeleteObject();
				delete alien;
			}
		}
	}
}


// Initialise l'objet.

void CAutoEgg::Init()
{
	CObject*	alien;

	alien = SearchAlien();
	if ( alien == 0 )
	{
		m_phase    = AEP_NULL;
		m_progress = 0.0f;
		m_speed    = 1.0f/5.0f;
		m_time     = 0.0f;
		return;
	}

	m_phase    = AEP_INCUB;
	m_progress = 0.0f;
	m_speed    = 1.0f/5.0f;
	m_time     = 0.0f;

	m_type = alien->RetType();

	if ( m_type == OBJECT_ANT    ||
		 m_type == OBJECT_SPIDER ||
		 m_type == OBJECT_BEE    )
	{
		alien->SetZoom(0, 0.2f);
	}
	if ( m_type == OBJECT_WORM )
	{
		alien->SetZoom(0, 0.01f);  // invisible !
	}
	alien->SetLock(TRUE);
	alien->SetActivity(FALSE);
}


// Donne une valeur.

BOOL CAutoEgg::SetType(ObjectType type)
{
	m_type = type;
	return TRUE;
}

// Donne une valeur.

BOOL CAutoEgg::SetValue(int rank, float value)
{
	if ( rank != 0 )  return FALSE;
	m_value = value;
	return TRUE;
}

// Donne la string.

BOOL CAutoEgg::SetString(char *string)
{
	strcpy(m_string, string);
	return TRUE;
}


// D�marre l'objet.

void CAutoEgg::Start(int param)
{
	if ( m_type == OBJECT_NULL )  return;
	if ( m_value == 0.0f )  return;

	m_phase    = AEP_DELAY;
	m_progress = 0.0f;
	m_speed    = 1.0f/m_value;

	m_param = param;
}


// Gestion d'un �v�nement.

BOOL CAutoEgg::EventProcess(const Event &event)
{
	CObject*	alien;

	CAuto::EventProcess(event);

	if ( m_engine->RetPause() )  return TRUE;

	if ( event.event != EVENT_FRAME )  return TRUE;
	if ( m_phase == AEP_NULL )  return TRUE;

	if ( m_phase == AEP_DELAY )
	{
		m_progress += event.rTime*m_speed;
		if ( m_progress < 1.0f )  return TRUE;

		alien = new CObject(m_iMan);
		if ( !alien->CreateInsect(m_object->RetPosition(0), m_object->RetAngleY(0), m_type) )
		{
			delete alien;
			m_phase    = AEP_DELAY;
			m_progress = 0.0f;
			m_speed    = 1.0f/2.0f;
			return TRUE;
		}
		alien->SetActivity(FALSE);
		alien->ReadProgram(0, m_string);
		alien->RunProgram(0);
		Init();
	}

	alien = SearchAlien();
	if ( alien == 0 )  return TRUE;
	alien->SetActivity(FALSE);

	m_progress += event.rTime*m_speed;

	if ( m_phase == AEP_ZOOM )
	{
		if ( m_type == OBJECT_ANT    ||
			 m_type == OBJECT_SPIDER ||
			 m_type == OBJECT_BEE    )
		{
			alien->SetZoom(0, 0.2f+m_progress*0.8f);  // �a pousse
		}
	}

	return TRUE;
}

// Indique si l'automate a termin� son activit�.

Error CAutoEgg::IsEnded()
{
	CObject*	alien;
	CPyro*		pyro;

	if ( m_phase == AEP_DELAY )
	{
		return ERR_CONTINUE;
	}

	alien = SearchAlien();
	if ( alien == 0 )  return ERR_STOP;

	if ( m_phase == AEP_INCUB )
	{
		if ( m_progress < 1.0f )  return ERR_CONTINUE;

		m_phase    = AEP_ZOOM;
		m_progress = 0.0f;
		m_speed    = 1.0f/5.0f;
	}

	if ( m_phase == AEP_ZOOM )
	{
		if ( m_progress < 1.0f )  return ERR_CONTINUE;

		pyro = new CPyro(m_iMan);
		pyro->Create(PT_EGG, m_object);  // explosion de l'oeuf

		alien->SetZoom(0, 1.0f);  // c'est un grand gar�on, maintenant

		m_phase    = AEP_WAIT;
		m_progress = 0.0f;
		m_speed    = 1.0f/3.0f;
	}

	if ( m_phase == AEP_WAIT )
	{
		if ( m_progress < 1.0f )  return ERR_CONTINUE;

		alien->SetLock(FALSE);
		alien->SetActivity(TRUE);  // l'insect n� est actif
	}

	return ERR_STOP;
}


// Retourne une erreur li�e � l'�tat de l'automate.

Error CAutoEgg::RetError()
{
	return ERR_OK;
}


// Cherche l'insect qui prend naissance dans l'oeuf.

CObject* CAutoEgg::SearchAlien()
{
	CObject*	pObj;
	CObject*	pBest;
	D3DVECTOR	cPos, oPos;
	ObjectType	type;
	float		dist, min;
	int			i;

	cPos = m_object->RetPosition(0);
	min = 100000.0f;
	pBest = 0;
	for ( i=0 ; i<1000000 ; i++ )
	{
		pObj = (CObject*)m_iMan->SearchInstance(CLASS_OBJECT, i);
		if ( pObj == 0 )  break;

		if ( pObj->RetTruck() != 0 )  continue;

		type = pObj->RetType();
		if ( type != OBJECT_ANT    &&
			 type != OBJECT_BEE    &&
			 type != OBJECT_SPIDER &&
			 type != OBJECT_WORM   )  continue;

		oPos = pObj->RetPosition(0);
		dist = Length2d(oPos, cPos);
		if ( dist < 8.0f && dist < min )
		{
			min = dist;
			pBest = pObj;
		}
	}
	return pBest;
}


// Sauve tous les param�tres de l'automate.

BOOL CAutoEgg::Write(char *line)
{
	char	name[100];

	if ( m_phase == AEP_NULL )  return FALSE;

	sprintf(name, " aExist=%d", 1);
	strcat(line, name);

	CAuto::Write(line);

	sprintf(name, " aPhase=%d", m_phase);
	strcat(line, name);

	sprintf(name, " aProgress=%.2f", m_progress);
	strcat(line, name);

	sprintf(name, " aSpeed=%.5f", m_speed);
	strcat(line, name);

	sprintf(name, " aParamType=%s", GetTypeObject(m_type));
	strcat(line, name);

	sprintf(name, " aParamValue1=%.2f", m_value);
	strcat(line, name);

	sprintf(name, " aParamString=\"%s\"", m_string);
	strcat(line, name);

	return TRUE;
}

// Restitue tous les param�tres de l'automate.

BOOL CAutoEgg::Read(char *line)
{
	if ( OpInt(line, "aExist", 0) == 0 )  return FALSE;

	CAuto::Read(line);

	m_phase = (AutoEggPhase)OpInt(line, "aPhase", AEP_NULL);
	m_progress = OpFloat(line, "aProgress", 0.0f);
	m_speed = OpFloat(line, "aSpeed", 1.0f);
	m_type = OpTypeObject(line, "aParamType", OBJECT_NULL);
	m_value = OpFloat(line, "aParamValue1", 0.0f);
	OpString(line, "aParamString", m_string);

	return TRUE;
}