summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/CombatEvent.cpp
blob: a42dbffd1a1c3bc916e0902441c38b1d26a20c82 (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
/*  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
    ========
    A significant (newsworthy) event in the dynamic campaign.
*/

#include "CombatEvent.h"
#include "CombatGroup.h"
#include "Campaign.h"
#include "Player.h"
#include "ShipDesign.h"
#include "Ship.h"

#include "ParseUtil.h"
#include "FormatUtil.h"
#include "DataLoader.h"

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

CombatEvent::CombatEvent(Campaign* c, int typ, int tim, int tem,
int src, const char* rgn)
: campaign(c), type(typ), time(tim), team(tem), source(src),
region(rgn), points(0), visited(false)
{ }

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

const char*
CombatEvent::SourceName() const
{
    return SourceName(source);
}

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

const char*
CombatEvent::TypeName() const
{
    return TypeName(type);
}

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

const char*
CombatEvent::SourceName(int n)
{
    switch (n) {
    case FORCOM:      return "FORCOM";
    case TACNET:      return "TACNET";
    case INTEL:       return "SECURE";
    case MAIL:        return "Mail";
    case NEWS:        return "News";
    }

    return "Unknown";
}

int
CombatEvent::SourceFromName(const char* n)
{
    for (int i = FORCOM; i <= NEWS; i++)
    if (!_stricmp(n, SourceName(i)))
    return i;

    return -1;
}

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

const char*
CombatEvent::TypeName(int n)
{
    switch (n) {
    case ATTACK:            return "ATTACK";
    case DEFEND:            return "DEFEND";
    case MOVE_TO:           return "MOVE_TO";
    case CAPTURE:           return "CAPTURE";
    case STRATEGY:          return "STRATEGY";
    case STORY:             return "STORY";
    case CAMPAIGN_START:    return "CAMPAIGN_START";
    case CAMPAIGN_END:      return "CAMPAIGN_END";
    case CAMPAIGN_FAIL:     return "CAMPAIGN_FAIL";
    }

    return "Unknown";
}

int
CombatEvent::TypeFromName(const char* n)
{
    for (int i = ATTACK; i <= CAMPAIGN_FAIL; i++)
    if (!_stricmp(n, TypeName(i)))
    return i;

    return -1;
}

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

void
CombatEvent::Load()
{
    DataLoader* loader   = DataLoader::GetLoader();

    if (!campaign || !loader)
    return;

    loader->SetDataPath(campaign->Path());

    if (file.length() > 0) {
        const char* filename = file.data();
        BYTE*       block    = 0;

        loader->LoadBuffer(filename, block, true);
        info = (const char*) block;
        loader->ReleaseBuffer(block);

        if (info.contains('$')) {
            Player*        player = Player::GetCurrentPlayer();
            CombatGroup*   group  = campaign->GetPlayerGroup();

            if (player) {
                info = FormatTextReplace(info, "$NAME",  player->Name().data());
                info = FormatTextReplace(info, "$RANK",  Player::RankName(player->Rank()));
            }

            if (group) {
                info = FormatTextReplace(info, "$GROUP", group->GetDescription());
            }

            char timestr[32];
            FormatDayTime(timestr, campaign->GetTime(), true);
            info = FormatTextReplace(info, "$TIME", timestr);
        }
    }

    if (type < CAMPAIGN_END && image_file.length() > 0) {
        loader->LoadBitmap(image_file, image);
    }

    loader->SetDataPath(0);
}