summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/SystemDesign.cpp
blob: a0f877d328b87253ee5b5ce77a7c646845f5d3f9 (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
/*  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
    ========
    Weapon Design parameters class
*/

#include "SystemDesign.h"
#include "Component.h"

#include "Game.h"
#include "Bitmap.h"
#include "DataLoader.h"
#include "ParseUtil.h"

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

List<SystemDesign> SystemDesign::catalog;

#define GET_DEF_TEXT(p,d,x) if(p->name()->value()==(#x))GetDefText(d->x,p,filename)
#define GET_DEF_NUM(p,d,x)  if(p->name()->value()==(#x))GetDefNumber(d->x,p,filename)

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

SystemDesign::SystemDesign()
{ }

SystemDesign::~SystemDesign()
{
    components.destroy();
}

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

void
SystemDesign::Initialize(const char* filename)
{
    Print("Loading System Designs '%s'\n", filename);

    // Load Design File:
    DataLoader* loader = DataLoader::GetLoader();
    BYTE* block;

    int blocklen = loader->LoadBuffer(filename, block, true);
    Parser parser({reinterpret_cast<const char*>(block), blocklen});
    Term*  term = parser.ParseTerm();

    if (!term) {
        Print("ERROR: could not parse '%s'\n", filename);
        exit(-3);
    }
    else {
        TermText* file_type = term->isText();
        if (!file_type || file_type->value() != "SYSTEM") {
            Print("ERROR: invalid system design file '%s'\n", filename);
            exit(-4);
        }
    }

    int type = 1;

    do {
        delete term;

        term = parser.ParseTerm();

        if (term) {
            TermDef* def = term->isDef();
            if (def) {
                if (def->name()->value() == "system") {

                    if (!def->term() || !def->term()->isStruct()) {
                        Print("WARNING: system structure missing in '%s'\n", filename);
                    }
                    else {
                        TermStruct*   val    = def->term()->isStruct();
                        SystemDesign* design = new SystemDesign;

                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                GET_DEF_TEXT(pdef, design, name);

                                else if (pdef->name()->value()==("component")) {
                                    if (!pdef->term() || !pdef->term()->isStruct()) {
                                        Print("WARNING: component structure missing in system '%s' in '%s'\n", (const char*) design->name, filename);
                                    }
                                    else {
                                        TermStruct*   val2    = pdef->term()->isStruct();
                                        ComponentDesign* comp_design = new ComponentDesign;

                                        for (int i = 0; i < val2->elements()->size(); i++) {
                                            TermDef* pdef2 = val2->elements()->at(i)->isDef();
                                            if (pdef2) {
                                                GET_DEF_TEXT(pdef2, comp_design, name);
                                                else GET_DEF_TEXT(pdef2, comp_design, abrv);
                                                else GET_DEF_NUM (pdef2, comp_design, repair_time);
                                                else GET_DEF_NUM (pdef2, comp_design, replace_time);
                                                else GET_DEF_NUM (pdef2, comp_design, spares);
                                                else GET_DEF_NUM (pdef2, comp_design, affects);

                                                else {
                                                    Print("WARNING: parameter '%s' ignored in '%s'\n",
                                                    pdef2->name()->value().data(), filename);
                                                }
                                            }
                                        }

                                        design->components.append(comp_design);
                                    }
                                }

                                else {
                                    Print("WARNING: parameter '%s' ignored in '%s'\n",
                                    pdef->name()->value().data(), filename);
                                }
                            }
                            else {
                                Print("WARNING: term ignored in '%s'\n", filename);
                                val->elements()->at(i)->print();
                            }
                        }

                        catalog.append(design);
                    }
                }

                else
                Print("WARNING: unknown definition '%s' in '%s'\n",
                def->name()->value().data(), filename);
            }
            else {
                Print("WARNING: term ignored in '%s'\n", filename);
                term->print();
            }
        }
    }
    while (term);

    loader->ReleaseBuffer(block);
}

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

void
SystemDesign::Close()
{
    catalog.destroy();
}

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

SystemDesign*
SystemDesign::Find(const char* name)
{
    SystemDesign  test;
    test.name = name;
    return catalog.find(&test);
}