summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/RadioTraffic.cpp
blob: 13fd7e99e4b7fc9cf4140c980d542be31bbeb6f3 (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
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Radio message handler class implementation
*/

#include "RadioTraffic.h"
#include "RadioMessage.h"
#include "RadioView.h"
#include "RadioVox.h"
#include "Instruction.h"
#include "Ship.h"
#include "Contact.h"
#include "Element.h"
#include "Sim.h"
#include "NetGame.h"
#include "NetData.h"

#include "Game.h"
#include "ContentBundle.h"
#include "Text.h"

// +----------------------------------------------------------------------+

RadioTraffic*  RadioTraffic::radio_traffic = 0;

// +----------------------------------------------------------------------+

RadioTraffic::RadioTraffic()
{
    radio_traffic = this;
}

RadioTraffic::~RadioTraffic()
{
    traffic.destroy();
}

// +----------------------------------------------------------------------+

void
RadioTraffic::Initialize()
{
    if (!radio_traffic)
    radio_traffic = new RadioTraffic;
}

void
RadioTraffic::Close()
{
    delete radio_traffic;
    radio_traffic = 0;
}

// +--------------------------------------------------------------------+

void
RadioTraffic::SendQuickMessage(Ship* ship, int action)
{
    if (!ship) return;

    Element* elem  = ship->GetElement();

    if (elem) {
        if (action >= RadioMessage::REQUEST_PICTURE) {
            Ship* controller = ship->GetController();

            if (controller && !ship->IsStarship()) {
                RadioMessage* msg = new RadioMessage(controller, ship, action);
                Transmit(msg);
            }
        }
        else if (action >= RadioMessage::SPLASH_1 && action <= RadioMessage::DISTRESS) {
            RadioMessage* msg = new RadioMessage((Element*) 0, ship, action);
            Transmit(msg);
        }
        else {
            RadioMessage* msg = new RadioMessage(elem, ship, action);

            if (action == RadioMessage::ATTACK || action == RadioMessage::ESCORT)
            msg->AddTarget(ship->GetTarget());

            Transmit(msg);
        }
    }
}

// +----------------------------------------------------------------------+

void
RadioTraffic::Transmit(RadioMessage* msg)
{
    if (msg && radio_traffic) {
        NetGame* net_game = NetGame::GetInstance();
        if (net_game) {
            NetCommMsg net_msg;
            net_msg.SetRadioMessage(msg);
            net_game->SendData(&net_msg);
        }

        radio_traffic->SendMessage(msg);
    }
}

// +----------------------------------------------------------------------+

void
RadioTraffic::SendMessage(RadioMessage* msg)
{
    if (!msg) return;

    Sim*  sim    = Sim::GetSim();
    Ship* player = sim->GetPlayerShip();
    int   iff    = 0;

    if (player)
    iff = player->GetIFF();

    if (msg->DestinationShip()) {
        traffic.append(msg);

        if (msg->Channel() == 0 || msg->Channel() == iff)
        DisplayMessage(msg);

        if (!NetGame::IsNetGameClient())
        msg->DestinationShip()->HandleRadioMessage(msg);
    }

    else if (msg->DestinationElem()) {
        traffic.append(msg);

        if (msg->Channel() == 0 || msg->Channel() == iff)
        DisplayMessage(msg);

        if (!NetGame::IsNetGameClient())
        msg->DestinationElem()->HandleRadioMessage(msg);
    }

    else {
        if (msg->Channel() == 0 || msg->Channel() == iff)
        DisplayMessage(msg);

        delete msg;
    }
}

// +----------------------------------------------------------------------+

Text
RadioTraffic::TranslateVox(const char* phrase)
{
    Text vox = "vox.";
    vox += phrase;
    vox.toLower();
    vox = ContentBundle::GetInstance()->GetText(vox);

    if (vox.contains("vox."))  // failed to translate
    return Text(phrase);    // return the original text

    return vox;
}

void
RadioTraffic::DisplayMessage(RadioMessage* msg)
{
    if (!msg) return;

    char txt_buf[256];   txt_buf[0] = 0;
    char msg_buf[128];   msg_buf[0] = 0;
    char src_buf[64];    src_buf[0] = 0;
    char dst_buf[64];    dst_buf[0] = 0;
    char act_buf[64];    act_buf[0] = 0;
    int  vox_channel = 0;

    Ship*    dst_ship = msg->DestinationShip();
    Element* dst_elem = msg->DestinationElem();

    // BUILD SRC AND DST BUFFERS -------------------

    if (msg->Sender()) {
        const Ship* sender = msg->Sender();

        // orders to self?
        if (dst_elem && dst_elem->NumShips() == 1 && dst_elem->GetShip(1) == sender) {
            if (msg->Action() >= RadioMessage::CALL_ENGAGING) {
                sprintf_s(src_buf, "%s", sender->Name());

                if (sender->IsStarship())
                vox_channel = (sender->Identity()%3) + 5;
            }
        }

        // orders to other ships:
        else {
            if (sender->IsStarship()) {
                vox_channel = (sender->Identity()%3) + 5;
            }
            else {
                vox_channel = sender->GetElementIndex();
            }

            if (msg->Action() >= RadioMessage::CALL_ENGAGING) {
                sprintf_s(src_buf, "%s", sender->Name());
            }
            else {
                sprintf_s(src_buf, "This is %s", sender->Name());

                if (dst_ship) {
                    // internal announcement
                    if (dst_ship->GetElement() == sender->GetElement()) {
                        dst_elem  = sender->GetElement();
                        int index = sender->GetElementIndex();

                        if (index > 1 && dst_elem) {
                            sprintf_s(dst_buf, "%s Leader",     (const char*) dst_elem->Name());
                            sprintf_s(src_buf, "this is %s %d", (const char*) dst_elem->Name(), index);
                        }
                        else {
                            sprintf_s(src_buf, "this is %s leader", (const char*) dst_elem->Name());
                        }
                    }

                    else {
                        strcpy_s(dst_buf, (const char*) dst_ship->Name());
                        src_buf[0] = tolower(src_buf[0]);
                    }
                }

                else if (dst_elem) {
                    // flight
                    if (dst_elem->NumShips() > 1) {
                        sprintf_s(dst_buf, "%s Flight", (const char*) dst_elem->Name());

                        // internal announcement
                        if (sender->GetElement() == dst_elem) {
                            int index = sender->GetElementIndex();

                            if (index > 1) {
                                sprintf_s(dst_buf, "%s Leader",     (const char*) dst_elem->Name());
                                sprintf_s(src_buf, "this is %s %d", (const char*) dst_elem->Name(), index);
                            }
                            else {
                                sprintf_s(src_buf, "this is %s leader", (const char*) dst_elem->Name());
                            }
                        }
                    }

                    // solo
                    else {
                        strcpy_s(dst_buf, (const char*) dst_elem->Name());
                        src_buf[0] = tolower(src_buf[0]);
                    }
                }
            }
        }
    }

    // BUILD ACTION AND TARGET BUFFERS -------------------

    SimObject* target = 0;

    strcpy_s(act_buf, RadioMessage::ActionName(msg->Action()));

    if (msg->TargetList().size() > 0)
    target = msg->TargetList()[0];

    if (msg->Action() == RadioMessage::ACK ||
            msg->Action() == RadioMessage::NACK) {

        if (dst_ship == msg->Sender()) {
            src_buf[0] = 0;
            dst_buf[0] = 0;

            if (msg->Action() == RadioMessage::ACK)
            sprintf_s(msg_buf, "%s.", TranslateVox("Acknowledged").data());
            else
            sprintf_s(msg_buf, "%s.", TranslateVox("Unable").data());
        }
        else if (msg->Sender()) {
            dst_buf[0] = 0;

            if (msg->Info().length()) {
                sprintf_s(msg_buf, "%s. %s",
                TranslateVox(act_buf).data(),
                (const char*) msg->Info());
            }
            else {
                sprintf_s(msg_buf, "%s.", TranslateVox(act_buf).data());
            }
        }
        else {
            if (msg->Info().length()) {
                sprintf_s(msg_buf, "%s. %s",
                TranslateVox(act_buf).data(),
                (const char*) msg->Info());
            }
            else {
                sprintf_s(msg_buf, "%s.", TranslateVox(act_buf).data());
            }
        }
    }

    else if (msg->Action() == RadioMessage::MOVE_PATROL) {
        sprintf_s(msg_buf, "%s.", TranslateVox("Move patrol.").data());
    }

    else if (target && dst_ship && msg->Sender()) {
        Contact* c = msg->Sender()->FindContact(target);

        if (c && c->GetIFF(msg->Sender()) > 10) {
            sprintf_s(msg_buf, "%s %s.", TranslateVox(act_buf).data(), TranslateVox("unknown contact").data());
        }

        else {
            sprintf_s(msg_buf, "%s %s.",
            TranslateVox(act_buf).data(),
            target->Name());
        }
    }

    else if (target) {
        sprintf_s(msg_buf, "%s %s.",
        TranslateVox(act_buf).data(),
        target->Name());
    }

    else if (msg->Info().length()) {
        sprintf_s(msg_buf, "%s %s",
        TranslateVox(act_buf).data(),
        (const char*) msg->Info());
    }

    else {
        strcpy_s(msg_buf, TranslateVox(act_buf).data());
    }

    char last_char = msg_buf[strlen(msg_buf)-1];
    if (last_char != '!' && last_char != '.' && last_char != '?')
    strcat_s(msg_buf, ".");

    // final format:
    if (dst_buf[0] && src_buf[0]) {
        sprintf_s(txt_buf, "%s %s. %s", TranslateVox(dst_buf).data(), TranslateVox(src_buf).data(), msg_buf);
        txt_buf[0] = toupper(txt_buf[0]);
    }

    else if (src_buf[0]) {
        sprintf_s(txt_buf, "%s. %s", TranslateVox(src_buf).data(), msg_buf);
        txt_buf[0] = toupper(txt_buf[0]);
    }

    else if (dst_buf[0]) {
        sprintf_s(txt_buf, "%s %s", TranslateVox(dst_buf).data(), msg_buf);
        txt_buf[0] = toupper(txt_buf[0]);
    }

    else {
        strcpy_s(txt_buf, msg_buf);
    }

    // vox:
    const char* path[8] = { "1", "1", "2", "3", "4", "5", "6", "7" };

    RadioVox* vox = new RadioVox(vox_channel, path[vox_channel], txt_buf);

    if (vox) {
        vox->AddPhrase(dst_buf);
        vox->AddPhrase(src_buf);
        vox->AddPhrase(act_buf);

        if (!vox->Start()) {
            RadioView::Message(txt_buf);
            delete vox;
        }
    }
}

// +----------------------------------------------------------------------+

void
RadioTraffic::DiscardMessages()
{
    if (radio_traffic)
    radio_traffic->traffic.destroy();
}