summaryrefslogtreecommitdiffstats
path: root/src/object/auto/auto.cpp
blob: 9e593a93477799bfafcae12a4fa728aa6edc5881 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.


#include "object/auto/auto.h"


#include "app/app.h"

#include "common/event.h"
#include "common/iman.h"

#include "script/cmdtoken.h"

#include "ui/interface.h"
#include "ui/gauge.h"
#include "ui/window.h"

#include <stdio.h>
#include <string.h>


// Object's constructor.

CAuto::CAuto(CObject* object)
{
    m_iMan = CInstanceManager::GetInstancePointer();

    m_object      = object;
    m_engine      = Gfx::CEngine::GetInstancePointer();
    m_main        = CRobotMain::GetInstancePointer();
    m_eventQueue  = CApplication::GetInstancePointer()->GetEventQueue();
    m_sound       = CApplication::GetInstancePointer()->GetSound();
    m_particle    = m_engine->GetParticle();
    m_terrain     = m_engine->GetTerrain();
    m_water       = m_engine->GetWater();
    m_cloud       = m_engine->GetCloud();
    m_planet      = m_engine->GetPlanet();
    m_lightning   = m_engine->GetLightning();
    m_camera      = m_main->GetCamera();
    m_interface   = m_main->GetInterface();

    m_type = m_object->GetType();
    m_time = 0.0f;
    m_lastUpdateTime = 0.0f;
    m_bMotor = false;
    m_progressTime = 0.0f;
    m_progressTotal = 1.0f;

    Init();
}

// Object's destructor.

CAuto::~CAuto()
{
    m_iMan = nullptr;

    m_object      = nullptr;
    m_engine      = nullptr;
    m_main        = nullptr;
    m_eventQueue  = nullptr;
    m_sound       = nullptr;
    m_particle    = nullptr;
    m_terrain     = nullptr;
    m_water       = nullptr;
    m_cloud       = nullptr;
    m_planet      = nullptr;
    m_lightning   = nullptr;
    m_camera      = nullptr;
    m_interface   = nullptr;
}


// Destroys the object.

void CAuto::DeleteObject(bool bAll)
{
}


// Initialize the object.

void CAuto::Init()
{
    m_bBusy = false;
}

// Starts the object.

void CAuto::Start(int param)
{
}

// Starts an action

Error CAuto::StartAction(int param)
{
    return ERR_GENERIC;
}

// Gete a type.

bool CAuto::SetType(ObjectType type)
{
    return false;
}

// Getes a value.

bool CAuto::SetValue(int rank, float value)
{
    return false;
}

// Getes the string.

bool CAuto::SetString(char *string)
{
    return false;
}


// Management of an event.

bool CAuto::EventProcess(const Event &event)
{
    if ( event.type == EVENT_FRAME &&
         !m_engine->GetPause() )
    {
        m_time += event.rTime;
        UpdateInterface(event.rTime);
    }

    if ( !m_object->GetSelect() )  // robot not selected?
    {
        return true;
    }

    return true;
}

// Indicates whether the controller has finished its activity.

Error CAuto::IsEnded()
{
    return ERR_CONTINUE;
}

// Stops the controller

bool CAuto::Abort()
{
    return false;
}


// Creates all the interface when the object is selected.

bool CAuto::CreateInterface(bool bSelect)
{
    Ui::CWindow*    pw;
    Math::Point     pos, dim, ddim;
    float       ox, oy, sx, sy;

    pw = static_cast<Ui::CWindow*>(m_interface->SearchControl(EVENT_WINDOW0));
    if ( pw != nullptr )
    {
        pw->Flush();  // destroys the window buttons
        m_interface->DeleteControl(EVENT_WINDOW0);  // destroys the window
    }

    if ( !bSelect )  return true;

    pos.x = 0.0f;
    pos.y = 0.0f;
    dim.x = 540.0f/640.0f;
//? dim.y = 70.0f/480.0f;
    dim.y = 86.0f/480.0f;
    m_interface->CreateWindows(pos, dim, 3, EVENT_WINDOW0);
    pw = static_cast<Ui::CWindow*>(m_interface->SearchControl(EVENT_WINDOW0));
    if ( pw == 0 )  return false;

    std::string name;
    m_object->GetTooltipName(name);
    pos.x = 0.0f;
    pos.y = 64.0f/480.0f;
    ddim.x = 540.0f/640.0f;
    ddim.y = 16.0f/480.0f;
    pw->CreateLabel(pos, ddim, 0, EVENT_LABEL0, name);

    dim.x = 33.0f/640.0f;
    dim.y = 33.0f/480.0f;
    ox = 3.0f/640.0f;
    oy = 3.0f/480.0f;
    sx = 33.0f/640.0f;
    sy = 33.0f/480.0f;

    pos.x = ox+sx*7.0f;
    pos.y = oy+sy*0.6f;
    ddim.x = 160.0f/640.0f;
    ddim.y =  26.0f/480.0f;
    pw->CreateGauge(pos, ddim, 0, EVENT_OBJECT_GPROGRESS);

    if ( m_type != OBJECT_BASE   &&
         m_type != OBJECT_SAFE   &&
         m_type != OBJECT_HUSTON )
    {
        pos.x = ox+sx*2.1f;
        pos.y = oy+sy*0;
        ddim.x = dim.x*0.6f;
        ddim.y = dim.y*0.6f;
        pw->CreateButton(pos, ddim, 12, EVENT_OBJECT_DELETE);
    }

#if 0
    pos.x = ox+sx*12.4f;
    pos.y = oy+sy*1;
    pw->CreateButton(pos, dim, 63, EVENT_OBJECT_BHELP);

    pos.x = ox+sx*12.4f;
    pos.y = oy+sy*0;
    pw->CreateButton(pos, dim, 19, EVENT_OBJECT_HELP);

    if ( m_main->GetSceneSoluce() )
    {
        pos.x = ox+sx*13.4f;
        pos.y = oy+sy*1;
        pw->CreateButton(pos, dim, 20, EVENT_OBJECT_SOLUCE);
    }

    pos.x = ox+sx*13.4f;
    pos.y = oy+sy*0;
    pw->CreateButton(pos, dim, 10, EVENT_OBJECT_DESELECT);
#else
    pos.x = ox+sx*12.3f;
    pos.y = oy+sy*-0.1f;
    ddim.x = dim.x*1.0f;
    ddim.y = dim.y*2.1f;
    pw->CreateGroup(pos, ddim, 20, EVENT_NULL);  // solid blue background

    pos.x = ox+sx*12.3f;
    pos.y = oy+sy*1;
    pw->CreateGroup(pos, dim, 19, EVENT_NULL);  // sign SatCom

    pos.x = ox+sx*12.4f;
    pos.y = oy+sy*0.5f;
    ddim.x = dim.x*0.8f;
    ddim.y = dim.y*0.5f;
    pw->CreateButton(pos, ddim, 18, EVENT_OBJECT_BHELP);
    pos.y = oy+sy*0.0f;
    pw->CreateButton(pos, ddim, 19, EVENT_OBJECT_HELP);

    pos.x = ox+sx*13.4f;
    pos.y = oy+sy*0;
    pw->CreateButton(pos, dim, 10, EVENT_OBJECT_DESELECT);
#endif

    pos.x = ox+sx*14.9f;
    pos.y = oy+sy*0;
    ddim.x = 14.0f/640.0f;
    ddim.y = 66.0f/480.0f;
    pw->CreateGauge(pos, ddim, 3, EVENT_OBJECT_GSHIELD);

    UpdateInterface();
    m_lastUpdateTime = 0.0f;
    UpdateInterface(0.0f);

    return true;
}

// Change the state of a button interface.

void CAuto::CheckInterface(Ui::CWindow *pw, EventType event, bool bState)
{
    Ui::CControl*   control;

    control = pw->SearchControl(event);
    if ( control == nullptr )  return;

    control->SetState(Ui::STATE_CHECK, bState);
}

// Change the state of a button interface.

void CAuto::EnableInterface(Ui::CWindow *pw, EventType event, bool bState)
{
    Ui::CControl*   control;

    control = pw->SearchControl(event);
    if ( control == nullptr )  return;

    control->SetState(Ui::STATE_ENABLE, bState);
}

// Change the state of a button interface.

void CAuto::VisibleInterface(Ui::CWindow *pw, EventType event, bool bState)
{
    Ui::CControl*   control;

    control = pw->SearchControl(event);
    if ( control == nullptr )  return;

    control->SetState(Ui::STATE_VISIBLE, bState);
}

// Change the state of a button interface.

void CAuto::DeadInterface(Ui::CWindow *pw, EventType event, bool bState)
{
    Ui::CControl*   control;

    control = pw->SearchControl(event);
    if ( control == nullptr )  return;

    control->SetState(Ui::STATE_DEAD, !bState);
}

// Change the state of a button interface.

void CAuto::UpdateInterface()
{
    Ui::CWindow*    pw;

    if ( !m_object->GetSelect() )  return;

    pw = static_cast<Ui::CWindow*>(m_interface->SearchControl(EVENT_WINDOW0));
    if ( pw == nullptr )  return;

    VisibleInterface(pw, EVENT_OBJECT_GPROGRESS, m_bBusy);
}

// Updates the state of all buttons on the interface,
// following the time that elapses ...

void CAuto::UpdateInterface(float rTime)
{
    Ui::CWindow*    pw;
    Ui::CGauge*     pg;

    if ( m_time < m_lastUpdateTime+0.1f )  return;
    m_lastUpdateTime = m_time;

    if ( !m_object->GetSelect() )  return;

    pw = static_cast<Ui::CWindow*>(m_interface->SearchControl(EVENT_WINDOW0));
    if ( pw == nullptr )  return;

    pg = static_cast<Ui::CGauge*>(pw->SearchControl(EVENT_OBJECT_GSHIELD));
    if ( pg != nullptr )
    {
        pg->SetLevel(m_object->GetShield());
    }

    pg = static_cast<Ui::CGauge*>(pw->SearchControl(EVENT_OBJECT_GPROGRESS));
    if ( pg != nullptr )
    {
        pg->SetLevel(m_progressTime);
    }
}


// Returns an error due the state of the automation.

Error CAuto::GetError()
{
    return ERR_OK;
}


// Management of the occupation.

bool CAuto::GetBusy()
{
    return m_bBusy;
}

void CAuto::SetBusy(bool bBusy)
{
    m_bBusy = bBusy;
}

void CAuto::InitProgressTotal(float total)
{
    m_progressTime = 0.0f;
    m_progressTotal = total;
}

void CAuto::EventProgress(float rTime)
{
    m_progressTime += rTime/m_progressTotal;
}


// Engine management.

bool CAuto::GetMotor()
{
    return m_bMotor;
}

void CAuto::SetMotor(bool bMotor)
{
    m_bMotor = bMotor;
}


// Saves all parameters of the controller.

bool CAuto::Write(char *line)
{
    char    name[100];

    sprintf(name, " aType=%d", m_type);
    strcat(line, name);

    sprintf(name, " aBusy=%d", m_bBusy);
    strcat(line, name);

    sprintf(name, " aTime=%.2f", m_time);
    strcat(line, name);

    sprintf(name, " aProgressTime=%.2f", m_progressTime);
    strcat(line, name);

    sprintf(name, " aProgressTotal=%.2f", m_progressTotal);
    strcat(line, name);

    return false;
}

// Return all settings to the controller.

bool CAuto::Read(char *line)
{
    m_type = static_cast<ObjectType>(OpInt(line, "aType", OBJECT_NULL));
    m_bBusy = OpInt(line, "aBusy", 0);
    m_time = OpFloat(line, "aTime", 0.0f);
    m_progressTime = OpFloat(line, "aProgressTime", 0.0f);
    m_progressTotal = OpFloat(line, "aProgressTotal", 0.0f);

    return false;
}