1 /*
2 * This file is part of the Colobot: Gold Edition source code
3 * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4 * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see http://gnu.org/licenses
18 */
19
20
21 #include "object/auto/autolabo.h"
22
23 #include "common/make_unique.h"
24
25 #include "level/robotmain.h"
26
27 #include "level/parser/parserline.h"
28 #include "level/parser/parserparam.h"
29
30 #include "math/geometry.h"
31
32 #include "object/object_manager.h"
33 #include "object/old_object.h"
34
35 #include "object/interface/powered_object.h"
36
37 #include "sound/sound.h"
38
39 #include "ui/controls/interface.h"
40 #include "ui/controls/window.h"
41
42
43
44 const float LABO_DELAY = 20.0f; // duration of the analysis
45
46
47
48
49 // Object's constructor.
50
CAutoLabo(COldObject * object)51 CAutoLabo::CAutoLabo(COldObject* object) : CAuto(object)
52 {
53 for (int i = 0; i < 3; i++)
54 {
55 m_partiRank[i] = -1;
56 }
57 m_partiSphere = -1;
58
59 m_soundChannel = -1;
60 Init();
61
62 assert(m_object->Implements(ObjectInterfaceType::Powered));
63 }
64
65 // Object's destructor.
66
~CAutoLabo()67 CAutoLabo::~CAutoLabo()
68 {
69 }
70
71
72 // Destroys the object.
73
DeleteObject(bool bAll)74 void CAutoLabo::DeleteObject(bool bAll)
75 {
76 for (int i = 0; i < 3; i++)
77 {
78 if ( m_partiRank[i] != -1 )
79 {
80 m_particle->DeleteParticle(m_partiRank[i]);
81 m_partiRank[i] = -1;
82 }
83 }
84
85 if ( m_partiSphere != -1 )
86 {
87 m_particle->DeleteParticle(m_partiSphere);
88 m_partiSphere = -1;
89 }
90
91 if ( m_soundChannel != -1 )
92 {
93 m_sound->FlushEnvelope(m_soundChannel);
94 m_sound->AddEnvelope(m_soundChannel, 0.0f, 1.0f, 1.0f, SOPER_STOP);
95 m_soundChannel = -1;
96 }
97
98 CAuto::DeleteObject(bAll);
99 }
100
101
102 // Initialize the object.
103
Init()104 void CAutoLabo::Init()
105 {
106 m_time = 0.0f;
107 m_timeVirus = 0.0f;
108 m_lastParticle = 0.0f;
109
110 m_phase = ALAP_WAIT; // waiting ...
111 m_progress = 0.0f;
112 m_speed = 1.0f/2.0f;
113
114 CAuto::Init();
115 }
116
117
118 // Starts an action
119
StartAction(int param)120 Error CAutoLabo::StartAction(int param)
121 {
122 if ( m_phase != ALAP_WAIT )
123 {
124 return ERR_OBJ_BUSY;
125 }
126
127 m_research = static_cast<ResearchType>(param);
128
129 if ( m_main->IsResearchDone(m_research, m_object->GetTeam()) )
130 {
131 return ERR_LABO_ALREADY;
132 }
133
134 CObject* power = m_object->GetPower();
135 if (power == nullptr)
136 {
137 return ERR_LABO_NULL;
138 }
139 if ( m_research != RESEARCH_TARGET && power->GetType() != OBJECT_BULLET )
140 {
141 return ERR_LABO_BAD;
142 }
143 if ( m_research == RESEARCH_TARGET && power->GetType() != OBJECT_TNT )
144 {
145 return ERR_LABO_BAD;
146 }
147
148 SetBusy(true);
149 InitProgressTotal(1.0f+1.5f+1.5f+LABO_DELAY+1.5f+1.5f+1.0f);
150 UpdateInterface();
151
152 power->SetLock(true); // ball longer usable
153
154 SoundManip(1.0f, 1.0f, 1.0f);
155 m_phase = ALAP_OPEN1;
156 m_progress = 0.0f;
157 m_speed = 1.0f/1.0f;
158 return ERR_OK;
159 }
160
161
162 // Management of an event.
163
EventProcess(const Event & event)164 bool CAutoLabo::EventProcess(const Event &event)
165 {
166 CObject* power;
167 Math::Vector pos, goal, speed;
168 Math::Point dim, rot;
169 float angle;
170 int i;
171
172 CAuto::EventProcess(event);
173
174 if ( m_engine->GetPause() ) return true;
175
176 if ( event.type == EVENT_UPDINTERFACE )
177 {
178 if ( m_object->GetSelect() ) CreateInterface(true);
179 }
180
181 if ( m_object->GetSelect() ) // center selected?
182 {
183 Error err = ERR_UNKNOWN;
184 if ( event.type == EVENT_OBJECT_RTARGET ) err = StartAction(RESEARCH_TARGET);
185 if ( event.type == EVENT_OBJECT_RiPAW ) err = StartAction(RESEARCH_iPAW);
186 if ( event.type == EVENT_OBJECT_RiGUN ) err = StartAction(RESEARCH_iGUN);
187
188 if( err != ERR_OK && err != ERR_UNKNOWN )
189 m_main->DisplayError(err, m_object);
190
191 if( err != ERR_UNKNOWN )
192 return false;
193 }
194
195 if ( event.type != EVENT_FRAME ) return true;
196
197 m_progress += event.rTime*m_speed;
198 m_timeVirus -= event.rTime;
199
200 if ( m_object->GetVirusMode() ) // contaminated by a virus?
201 {
202 if ( m_timeVirus <= 0.0f )
203 {
204 m_timeVirus = 0.1f+Math::Rand()*0.3f;
205 }
206 return true;
207 }
208
209 EventProgress(event.rTime);
210
211 if ( m_phase == ALAP_WAIT )
212 {
213 if ( m_progress >= 1.0f )
214 {
215 m_phase = ALAP_WAIT; // still waiting ...
216 m_progress = 0.0f;
217 m_speed = 1.0f/2.0f;
218 }
219 }
220
221 if ( m_phase == ALAP_OPEN1 )
222 {
223 if ( m_progress < 1.0f )
224 {
225 angle = 80.0f-(35.0f*m_progress);
226 m_object->SetPartRotationZ(3, angle*Math::PI/180.0f);
227 m_object->SetPartRotationZ(4, angle*Math::PI/180.0f);
228 m_object->SetPartRotationZ(5, angle*Math::PI/180.0f);
229 }
230 else
231 {
232 m_object->SetPartRotationZ(3, 45.0f*Math::PI/180.0f);
233 m_object->SetPartRotationZ(4, 45.0f*Math::PI/180.0f);
234 m_object->SetPartRotationZ(5, 45.0f*Math::PI/180.0f);
235
236 SoundManip(1.5f, 1.0f, 0.7f);
237 m_phase = ALAP_OPEN2;
238 m_progress = 0.0f;
239 m_speed = 1.0f/1.5f;
240 }
241 }
242
243 if ( m_phase == ALAP_OPEN2 )
244 {
245 if ( m_progress < 1.0f )
246 {
247 pos.x = -9.0f;
248 pos.y = 3.0f+m_progress*10.0f;
249 pos.z = 0.0f;
250 m_object->SetPartPosition(1, pos);
251 }
252 else
253 {
254 m_object->SetPartPosition(1, Math::Vector(-9.0f, 13.0f, 0.0f));
255
256 SoundManip(1.5f, 1.0f, 0.5f);
257 m_phase = ALAP_OPEN3;
258 m_progress = 0.0f;
259 m_speed = 1.0f/1.5f;
260 }
261 }
262
263 if ( m_phase == ALAP_OPEN3 )
264 {
265 if ( m_progress < 1.0f )
266 {
267 angle = (1.0f-m_progress)*Math::PI/2.0f;
268 m_object->SetPartRotationZ(1, angle);
269 }
270 else
271 {
272 m_object->SetPartRotationZ(1, 0.0f);
273
274 goal = m_object->GetPosition();
275 goal.y += 3.0f;
276 pos = goal;
277 pos.x -= 4.0f;
278 pos.y += 4.0f;
279 for ( i=0 ; i<3 ; i++ )
280 {
281 m_partiRank[i] = m_particle->CreateRay(pos, goal,
282 Gfx::PARTIRAY2,
283 Math::Point(2.9f, 2.9f),
284 LABO_DELAY);
285 }
286
287 m_soundChannel = m_sound->Play(SOUND_LABO, m_object->GetPosition(), 0.0f, 0.25f, true);
288 m_sound->AddEnvelope(m_soundChannel, 1.0f, 0.60f, 2.0f, SOPER_CONTINUE);
289 m_sound->AddEnvelope(m_soundChannel, 1.0f, 2.00f, 8.0f, SOPER_CONTINUE);
290 m_sound->AddEnvelope(m_soundChannel, 1.0f, 0.60f, 8.0f, SOPER_CONTINUE);
291 m_sound->AddEnvelope(m_soundChannel, 0.0f, 0.25f, 2.0f, SOPER_STOP);
292
293 pos = m_object->GetPosition();
294 pos.y += 4.0f;
295 speed = Math::Vector(0.0f, 0.0f, 0.0f);
296 dim.x = 4.0f;
297 dim.y = dim.x;
298 m_partiSphere = m_particle->CreateParticle(pos, speed,
299 dim, Gfx::PARTISPHERE2, LABO_DELAY, 0.0f, 0.0f);
300
301 m_phase = ALAP_ANALYSE;
302 m_progress = 0.0f;
303 m_speed = 1.0f/LABO_DELAY;
304 }
305 }
306
307 if ( m_phase == ALAP_ANALYSE )
308 {
309 if ( m_progress < 1.0f )
310 {
311 power = m_object->GetPower();
312 if ( power != nullptr )
313 {
314 power->SetScale(1.0f-m_progress);
315 }
316
317 angle = m_object->GetPartRotationY(2);
318 if ( m_progress < 0.5f )
319 {
320 angle -= event.rTime*m_progress*20.0f;
321 }
322 else
323 {
324 angle -= event.rTime*(20.0f-m_progress*20.0f);
325 }
326 m_object->SetPartRotationY(2, angle); // rotates the analyzer
327
328 angle += m_object->GetRotationY();
329 for ( i=0 ; i<3 ; i++ )
330 {
331 rot = Math::RotatePoint(-angle, -4.0f);
332 pos = m_object->GetPosition();
333 pos.x += rot.x;
334 pos.z += rot.y;
335 pos.y += 3.0f+4.0f;;
336 m_particle->SetPosition(m_partiRank[i], pos); // adjusts ray
337
338 angle += Math::PI*2.0f/3.0f;
339 }
340
341 if ( m_lastParticle+m_engine->ParticleAdapt(0.05f) <= m_time )
342 {
343 m_lastParticle = m_time;
344
345 if ( m_progress > 0.25f &&
346 m_progress < 0.80f )
347 {
348 pos = m_object->GetPosition();
349 pos.y += 3.0f;
350 pos.x += (Math::Rand()-0.5f)*2.0f;
351 pos.z += (Math::Rand()-0.5f)*2.0f;
352 speed.y = Math::Rand()*5.0f+5.0f;
353 speed.x = (Math::Rand()-0.5f)*10.0f;
354 speed.z = (Math::Rand()-0.5f)*10.0f;
355 dim.x = Math::Rand()*0.4f*m_progress+1.0f;
356 dim.y = dim.x;
357 m_particle->CreateTrack(pos, speed, dim, Gfx::PARTITRACK2,
358 2.0f+2.0f*m_progress, 10.0f, 1.5f, 1.4f);
359 }
360 }
361 }
362 else
363 {
364 m_main->MarkResearchDone(m_research, m_object->GetTeam()); // research done
365
366 m_eventQueue->AddEvent(Event(EVENT_UPDINTERFACE));
367 UpdateInterface();
368
369 power = m_object->GetPower();
370 if ( power != nullptr )
371 {
372 m_object->SetPower(nullptr);
373 CObjectManager::GetInstancePointer()->DeleteObject(power);
374 }
375
376 m_main->DisplayError(INFO_LABO, m_object);
377
378 SoundManip(1.5f, 1.0f, 0.5f);
379 m_phase = ALAP_CLOSE1;
380 m_progress = 0.0f;
381 m_speed = 1.0f/1.5f;
382 }
383 }
384
385 if ( m_phase == ALAP_CLOSE1 )
386 {
387 if ( m_progress < 1.0f )
388 {
389 angle = m_progress*Math::PI/2.0f;
390 m_object->SetPartRotationZ(1, angle);
391 }
392 else
393 {
394 m_object->SetPartRotationZ(1, Math::PI/2.0f);
395
396 SoundManip(1.5f, 1.0f, 0.7f);
397 m_phase = ALAP_CLOSE2;
398 m_progress = 0.0f;
399 m_speed = 1.0f/1.5f;
400 }
401 }
402
403 if ( m_phase == ALAP_CLOSE2 )
404 {
405 if ( m_progress < 1.0f )
406 {
407 pos.x = -9.0f;
408 pos.y = 3.0f+(1.0f-m_progress)*10.0f;;
409 pos.z = 0.0f;
410 m_object->SetPartPosition(1, pos);
411 }
412 else
413 {
414 m_object->SetPartPosition(1, Math::Vector(-9.0f, 3.0f, 0.0f));
415
416 SoundManip(1.0f, 1.0f, 1.0f);
417 m_phase = ALAP_CLOSE3;
418 m_progress = 0.0f;
419 m_speed = 1.0f/1.0f;
420 }
421 }
422
423 if ( m_phase == ALAP_CLOSE3 )
424 {
425 if ( m_progress < 1.0f )
426 {
427 angle = 45.0f+(35.0f*m_progress);
428 m_object->SetPartRotationZ(3, angle*Math::PI/180.0f);
429 m_object->SetPartRotationZ(4, angle*Math::PI/180.0f);
430 m_object->SetPartRotationZ(5, angle*Math::PI/180.0f);
431 }
432 else
433 {
434 m_object->SetPartRotationZ(3, 80.0f*Math::PI/180.0f);
435 m_object->SetPartRotationZ(4, 80.0f*Math::PI/180.0f);
436 m_object->SetPartRotationZ(5, 80.0f*Math::PI/180.0f);
437
438 SetBusy(false);
439 UpdateInterface();
440
441 m_phase = ALAP_WAIT;
442 m_progress = 0.0f;
443 m_speed = 1.0f/2.0f;
444 }
445 }
446
447 return true;
448 }
449
450
451 // Returns an error due the state of the automation.
452
GetError()453 Error CAutoLabo::GetError()
454 {
455 if ( m_object->GetVirusMode() )
456 {
457 return ERR_BAT_VIRUS;
458 }
459
460 CObject* obj = m_object->GetPower();
461 if (obj == nullptr) return ERR_LABO_NULL;
462 ObjectType type = obj->GetType();
463 if ( type != OBJECT_BULLET && type != OBJECT_TNT ) return ERR_LABO_BAD;
464
465 return ERR_OK;
466 }
467
468
469 // Creates all the interface when the object is selected.
470
CreateInterface(bool bSelect)471 bool CAutoLabo::CreateInterface(bool bSelect)
472 {
473 Ui::CWindow* pw;
474 Math::Point pos, dim, ddim;
475 float ox, oy, sx, sy;
476
477 CAuto::CreateInterface(bSelect);
478
479 if ( !bSelect ) return true;
480
481 pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
482 if ( pw == nullptr ) return false;
483
484 dim.x = 33.0f/640.0f;
485 dim.y = 33.0f/480.0f;
486 ox = 3.0f/640.0f;
487 oy = 3.0f/480.0f;
488 sx = 33.0f/640.0f;
489 sy = 33.0f/480.0f;
490 if( !m_object->GetTrainer() )
491 {
492 pos.x = ox+sx*6.0f;
493 pos.y = oy+sy*0.5f;
494 pw->CreateButton(pos, dim, 192+8, EVENT_OBJECT_RTARGET);
495
496 pos.x = ox+sx*7.0f;
497 pos.y = oy+sy*0.5f;
498 pw->CreateButton(pos, dim, 64+45, EVENT_OBJECT_RiPAW);
499
500 pos.x = ox+sx*8.0f;
501 pos.y = oy+sy*0.5f;
502 pw->CreateButton(pos, dim, 64+46, EVENT_OBJECT_RiGUN);
503 }
504
505 pos.x = ox+sx*0.0f;
506 pos.y = oy+sy*0;
507 ddim.x = 66.0f/640.0f;
508 ddim.y = 66.0f/480.0f;
509 pw->CreateGroup(pos, ddim, 111, EVENT_OBJECT_TYPE);
510
511 UpdateInterface();
512
513 return true;
514 }
515
516 // Updates the status of all interface buttons.
517
UpdateInterface()518 void CAutoLabo::UpdateInterface()
519 {
520 Ui::CWindow* pw;
521
522 if ( !m_object->GetSelect() ) return;
523
524 CAuto::UpdateInterface();
525
526 pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
527 if ( pw == nullptr ) return;
528
529 DeadInterface(pw, EVENT_OBJECT_RTARGET, m_main->IsResearchEnabled(RESEARCH_TARGET));
530 DeadInterface(pw, EVENT_OBJECT_RiPAW, m_main->IsResearchEnabled(RESEARCH_iPAW));
531 DeadInterface(pw, EVENT_OBJECT_RiGUN, m_main->IsResearchEnabled(RESEARCH_iGUN));
532
533 OkayButton(pw, EVENT_OBJECT_RTARGET);
534 OkayButton(pw, EVENT_OBJECT_RiPAW);
535 OkayButton(pw, EVENT_OBJECT_RiGUN);
536
537 VisibleInterface(pw, EVENT_OBJECT_RTARGET, !m_bBusy);
538 VisibleInterface(pw, EVENT_OBJECT_RiPAW, !m_bBusy);
539 VisibleInterface(pw, EVENT_OBJECT_RiGUN, !m_bBusy);
540 }
541
542 // Indicates the research conducted for a button.
543
OkayButton(Ui::CWindow * pw,EventType event)544 void CAutoLabo::OkayButton(Ui::CWindow *pw, EventType event)
545 {
546 Ui::CControl* control;
547
548 control = pw->SearchControl(event);
549 if ( control == nullptr ) return;
550
551 control->SetState(Ui::STATE_OKAY, TestResearch(event));
552 }
553
554
555 // Test whether a search has already been done.
556
TestResearch(EventType event)557 bool CAutoLabo::TestResearch(EventType event)
558 {
559 if ( event == EVENT_OBJECT_RTARGET ) return m_main->IsResearchDone(RESEARCH_TARGET, m_object->GetTeam());
560 if ( event == EVENT_OBJECT_RiPAW ) return m_main->IsResearchDone(RESEARCH_iPAW, m_object->GetTeam());
561 if ( event == EVENT_OBJECT_RiGUN ) return m_main->IsResearchDone(RESEARCH_iGUN, m_object->GetTeam());
562
563 return m_main;
564 }
565
566 // Plays the sound of the manipulator arm.
567
SoundManip(float time,float amplitude,float frequency)568 void CAutoLabo::SoundManip(float time, float amplitude, float frequency)
569 {
570 int i;
571
572 i = m_sound->Play(SOUND_MANIP, m_object->GetPosition(), 0.0f, 0.3f*frequency, true);
573 m_sound->AddEnvelope(i, 0.5f*amplitude, 1.0f*frequency, 0.1f, SOPER_CONTINUE);
574 m_sound->AddEnvelope(i, 0.5f*amplitude, 1.0f*frequency, time-0.1f, SOPER_CONTINUE);
575 m_sound->AddEnvelope(i, 0.0f, 0.3f*frequency, 0.1f, SOPER_STOP);
576 }
577
578
579 // Saves all parameters of the controller.
580
Write(CLevelParserLine * line)581 bool CAutoLabo::Write(CLevelParserLine* line)
582 {
583 if ( m_phase == ALAP_WAIT ) return false;
584
585 line->AddParam("aExist", MakeUnique<CLevelParserParam>(true));
586 CAuto::Write(line);
587 line->AddParam("aPhase", MakeUnique<CLevelParserParam>(static_cast<int>(m_phase)));
588 line->AddParam("aProgress", MakeUnique<CLevelParserParam>(m_progress));
589 line->AddParam("aSpeed", MakeUnique<CLevelParserParam>(m_speed));
590 line->AddParam("aResearch", MakeUnique<CLevelParserParam>(static_cast<int>(m_research)));
591
592 return true;
593 }
594
595 // Restores all parameters of the controller.
596
Read(CLevelParserLine * line)597 bool CAutoLabo::Read(CLevelParserLine* line)
598 {
599 if ( !line->GetParam("aExist")->AsBool(false) ) return false;
600
601 CAuto::Read(line);
602 m_phase = static_cast< AutoLaboPhase >(line->GetParam("aPhase")->AsInt(ALAP_WAIT));
603 m_progress = line->GetParam("aProgress")->AsFloat(0.0f);
604 m_speed = line->GetParam("aSpeed")->AsFloat(1.0f);
605 m_research = static_cast< ResearchType >(line->GetParam("aResearch")->AsInt(0));
606
607 m_lastParticle = 0.0f;
608
609 return true;
610 }
611