/
Port.cpp
358 lines (290 loc) · 8.93 KB
/
Port.cpp
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
/*
Port.cpp
(c)1998 Palestar, Richard Lyle
*/
#define UNIVERSECONTEXTPORT_DLL
#include "stdafx.h"
#include "Port.h"
#include "PortView.h"
#include "ChildFrame.h"
#include "Resource.h"
#include "File/FileDisk.h"
#include "Resource/Collection.h"
#include "Render3D/NodeParticleField.h"
#include "DarkSpace/NounJumpGate.h"
#include "DarkSpace/NounSpawnShip.h"
#include "DarkSpace/TraitOrbit.h"
#include "DarkSpace/GameZone.h"
#include "Tools/ResourcerDoc/ResourcerDoc.h"
#include "Tools/ScenePort/Port.h"
#include "Tools/TextPort/Port.h"
#include "Tools/NodeParticleField/Port.h"
#include "Tools/ShipContextPort/Port.h"
#include "Tools/NounJumpGatePort/Port.h"
#include "Tools/NounPlanetPort/Port.h"
#include "Tools/NounBodyPort/Port.h"
#include "Tools/NounSpawnPort/Port.h"
//----------------------------------------------------------------------------
IMPLEMENT_FACTORY( UniverseContextPort, Port );
REGISTER_FACTORY_KEY( UniverseContextPort, 4037388579725782390 );
BEGIN_PROPERTY_LIST( UniverseContextPort, Port )
ADD_PROPERTY( m_Name );
ADD_PROPERTY( m_Description );
ADD_PROPERTY( m_Type );
ADD_PORT_PROPERTY( m_Script, TextPort );
ADD_PORT_PROPERTY( m_Scenery, ScenePort );
ADD_PROPERTY( m_Position );
ADD_PROPERTY( m_Zones );
ADD_PROPERTY( m_Teams );
ADD_PROPERTY( m_Conditions );
ADD_PROPERTY( m_TimeLimit );
// deprecated
ADD_PORT_PROPERTY( m_UniversePort, ScenePort );
END_PROPERTY_LIST();
UniverseContextPort::UniverseContextPort() : Port()
{
m_Name = "Context Name";
m_Description = "Context Description";
m_Type = 0;
m_Position = Vector3( 0.0f, 0.0f, 0.0f );
m_TimeLimit = TICKS_PER_HOUR * 2;
}
//----------------------------------------------------------------------------
const int VERSION_082100 = 82100;
const int VERSION_022301 = 22301;
const int VERSION_040501 = 40501;
const int VERSION_073101 = 73101;
const int VERSION_082701 = 82701;
const int VERSION_092001 = 92001;
const int VERSION_102401 = 102401;
bool UniverseContextPort::read( const InStream &input )
{
if (! Port::read( input ) )
return false;
if ( m_UniversePort.length() > 0 )
{
Zone zone;
zone.sScene = m_UniversePort;
zone.vPosition = m_Position;
m_Zones.push( zone );
m_UniversePort = "";
m_Position = Vector3( 0.0f, 0.0f, 0.0f );
}
return true;
}
//----------------------------------------------------------------------------
bool UniverseContextPort::autoLoad()
{
return false;
}
void UniverseContextPort::dependencies( DependentArray & dep )
{
dep.push( m_Scenery );
dep.push( m_Script );
for(int i=0;i<m_Zones.size();++i)
dep.push( m_Zones[i].sScene );
for(int i=0;i<m_Teams.size();i++)
{
dep.push( m_Teams[i].logo );
dep.push( m_Teams[i].ships );
}
for(int i=0;i<m_Conditions.size();i++)
dep.push( m_Conditions[i].next );
}
Resource::Ref UniverseContextPort::createResource()
{
// turn off jump gate linking while building the resource.
NounJumpGate::sm_bBuildMode = true;
// load the scenery scene
Scene::Link pScenery = Scene::Link( portResource( m_Scenery ) );
Text::Link pScript = Text::Link( portResource( m_Script ) );
BoxHull UniverseHull;
// the list of all nouns to get added into the universe..
Array< NounBody::Ref > nouns;
for(int i=0;i<m_Zones.size();++i)
{
const Zone & zone = m_Zones[ i ];
Scene::Link pScene = Scene::Link( portResource( zone.sScene ) );
if (! pScene.valid() )
{
report( CharString().format( "Failed to load zone %s", zone.sScene ) );
continue;
}
Vector3 vOffset( zone.vPosition * GALACTIC_UNIT_SIZE );
BoxHull SceneHull( pScene->calculateHull() * 2.0f, vOffset );
UniverseHull.addBox( SceneHull );
if ( UniverseHull.radius() > 5000000.0f )
report( CharString().format( "%s; Scene is increased map radius to %f gu.", zone.sScene.cstr(), UniverseHull.radius() ) );
// push all nouns in this zone into a master list, update the position of each noun into would space..
std::list< BaseNode * > nodes;
nodes.push_back( pScene->node() );
while( nodes.size() > 0 )
{
BaseNode * pNode = nodes.front();
nodes.pop_front();
if (! pNode )
continue;
// push all children into the stack..
for(int j=0;j<pNode->childCount();++j)
nodes.push_back( pNode->child(j) );
NounBody * pNoun = WidgetCast<NounBody>( pNode );
if (! pNoun )
continue;
pNoun->setPosition( pNoun->worldPosition() + vOffset );
pNoun->setup();
nouns.push( pNoun );
}
}
TRACE( CharString().format("UniverseContextPort::createResource() - hull = %s/%s, nouns = %d",
ConvertType<CharString>( UniverseHull.min ),
ConvertType<CharString>( UniverseHull.max ),
nouns.size() ) );
// create the universeContext object
GameContext::Ref pContext = new GameContext();
pContext->setPosition( m_Position * GALACTIC_UNIT_SIZE );
pContext->setName( m_Name );
pContext->setDescription( m_Description );
pContext->setType( (GameContext::ContextType)m_Type );
pContext->setScript( pScript.valid() ? CharString( pScript->text() ) : "" );
for(int i=0;i<m_Teams.size();i++)
{
const Team & t = m_Teams[i];
WorldContext::Team team;
team.nTeamId = i + 1;
team.clanId = 0;
team.factionId = t.faction;
team.flags = t.flags;
team.name = t.name;
team.longName = t.name;
team.about = t.description;
team.pTemplates = Collection::Link( portResource( t.ships ) );
pContext->addTeam( team );
}
for(int i=0;i<m_Conditions.size();i++)
{
GameContext::Condition newCondition;
newCondition.conditionId = i + 1;
newCondition.script = "";
newCondition.message = m_Conditions[i].message;
newCondition.next = portResource( m_Conditions[i].next ).key();
for(int j=0;j<m_Conditions[i].objectives.size();j++)
{
GameContext::Objective newObjective;
newObjective.description = m_Conditions[i].objectives[j].description;
newObjective.factionId = m_Conditions[i].objectives[j].faction;
newObjective.type = m_Conditions[i].objectives[j].type;
CharString sObjectiveName = m_Conditions[i].objectives[j].noun;
bool bObjectiveFound = false;
for(int k=0;k<nouns.size() && !bObjectiveFound;++k)
{
if ( _stricmp( sObjectiveName, nouns[k]->name() ) == 0 )
{
bObjectiveFound = true;
newObjective.noun = nouns[k]->key();
newCondition.objectives.push( newObjective );
}
}
if (! bObjectiveFound )
report( CharString().format("Failed to find objective %s, ignoring objective!",sObjectiveName) );
}
if ( newCondition.objectives.size() > 0 )
pContext->addCondition( newCondition );
}
pContext->setTimeLimit( m_TimeLimit );
if (! pContext->createZones( UniverseHull, pScenery ) )
report( "Failed to create zones!" );
// lastly, add the nouns to the universe...
for(int k=0;k<nouns.size();++k)
{
Noun * pNoun = nouns[k];
if (! pNoun )
continue;
TRACE( CharString().format( "UniverseContextPort::createResource() - Adding noun %s at %s",
pNoun->name(), ConvertType<CharString>( pNoun->position() ) ) );
if (! pContext->attachNoun( pNoun ) )
report( CharString().format( "Failed to attach noun %s...", pNoun->name() ) );
}
report( CharString().format( "%d Zones, %d Nouns", pContext->zoneCount(), nouns.size() ) );
// restore build mode..
NounJumpGate::sm_bBuildMode = false;
return pContext.pointer();
}
CFrameWnd * UniverseContextPort::createView()
{
static CMultiDocTemplate docTemplate( IDR_PORT, NULL,
RUNTIME_CLASS(CChildFrame), RUNTIME_CLASS(CPortView) );
CChildFrame *pFrame = dynamic_cast<CChildFrame *>( docTemplate.CreateNewFrame( this, NULL ) );
ASSERT( pFrame );
docTemplate.InitialUpdateFrame(pFrame, this);
return( pFrame );
}
// Import a DSM file from the editor...
const int VERSION_110901 = 110901;
bool UniverseContextPort::importFile( const char * fileName )
{
return false; // return true of successful import, false on failure
}
void UniverseContextPort::shellOpen() const
{
MessageBox( NULL, _T("shellOpen() is not valid for this port"), NULL, MB_OK);
}
int UniverseContextPort::factionCount()
{
return FACTION_COUNT;
}
const char * UniverseContextPort::factionName( int faction )
{
static char * FACTION_NAMES[] =
{
"Unknown", // 0
"Neutral", // 1
"UGTO", // 2
"ICC", // 3
"K'Luth", // 4
"Human Alliance", // 5
"Pirate", // 6
"MI", // 7
"Teros", // 8
};
return FACTION_NAMES[ faction ];
}
Tech UniverseContextPort::factionTech( int faction )
{
switch( faction )
{
case FACTION_NEUTRAL:
return TECH_NEUTRAL;
case FACTION_UGTO:
return TECH_UGTO;
case FACTION_ICC:
return TECH_ICC;
case FACTION_KLUTH:
return TECH_KLUTH;
case FACTION_PIRATE:
return TECH_PIRATE;
case FACTION_MI:
return TECH_MI;
case FACTION_TEROS:
return TECH_TEROS;
}
return TECH_UNKNOWN;
}
Race UniverseContextPort::factionRace( int faction )
{
switch( faction )
{
case FACTION_UGTO:
case FACTION_ICC:
case FACTION_HUMAN_ALLIANCE:
case FACTION_PIRATE:
case FACTION_TEROS:
return RACE_HUMAN;
case FACTION_KLUTH:
return RACE_KLUTH;
case FACTION_MI:
return RACE_MI;
}
return RACE_NONE;
}
//-------------------------------------------------------------------------------
// EOF