forked from eu07/maszyna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemCell.cpp
193 lines (175 loc) · 5.62 KB
/
MemCell.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
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
/*
MaSzyna EU07 locomotive simulator
Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others
*/
#include "system.hpp"
#include "classes.hpp"
#pragma hdrstop
#include "Driver.h"
#include "mctools.hpp"
#include "MemCell.h"
#include "Event.h"
#include "parser.h"
#include "Usefull.h"
#include "Globals.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
TMemCell::TMemCell(vector3 *p)
{
fValue1 = fValue2 = 0;
szText = new char[256]; // musi byæ dla automatycznie tworzonych komórek dla odcinków
// izolowanych
vPosition =
p ? *p : vector3(0, 0, 0); // ustawienie wspó³rzêdnych, bo do TGroundNode nie ma dostêpu
bCommand = false; // komenda wys³ana
OnSent = NULL;
}
TMemCell::~TMemCell()
{
SafeDeleteArray(szText);
}
void TMemCell::Init()
{
}
void TMemCell::UpdateValues(char *szNewText, double fNewValue1, double fNewValue2, int CheckMask)
{
if (CheckMask & update_memadd)
{ // dodawanie wartoœci
if (TestFlag(CheckMask, update_memstring))
strcat(szText, szNewText);
if (TestFlag(CheckMask, update_memval1))
fValue1 += fNewValue1;
if (TestFlag(CheckMask, update_memval2))
fValue2 += fNewValue2;
}
else
{
if (TestFlag(CheckMask, update_memstring))
strcpy(szText, szNewText);
if (TestFlag(CheckMask, update_memval1))
fValue1 = fNewValue1;
if (TestFlag(CheckMask, update_memval2))
fValue2 = fNewValue2;
}
if (TestFlag(CheckMask, update_memstring))
CommandCheck(); // jeœli zmieniony tekst, próbujemy rozpoznaæ komendê
}
TCommandType TMemCell::CommandCheck()
{ // rozpoznanie komendy
if (strcmp(szText, "SetVelocity") == 0) // najpopularniejsze
{
eCommand = cm_SetVelocity;
bCommand = false; // ta komenda nie jest wysy³ana
}
else if (strcmp(szText, "ShuntVelocity") == 0) // w tarczach manewrowych
{
eCommand = cm_ShuntVelocity;
bCommand = false; // ta komenda nie jest wysy³ana
}
else if (strcmp(szText, "Change_direction") == 0) // zdarza siê
{
eCommand = cm_ChangeDirection;
bCommand = true; // do wys³ania
}
else if (strcmp(szText, "OutsideStation") == 0) // zdarza siê
{
eCommand = cm_OutsideStation;
bCommand = false; // tego nie powinno byæ w komórce
}
else if (strncmp(szText, "PassengerStopPoint:", 19) == 0) // porównanie pocz¹tków
{
eCommand = cm_PassengerStopPoint;
bCommand = false; // tego nie powinno byæ w komórce
}
else if (strcmp(szText, "SetProximityVelocity") == 0) // nie powinno tego byæ
{
eCommand = cm_SetProximityVelocity;
bCommand = false; // ta komenda nie jest wysy³ana
}
else
{
eCommand = cm_Unknown; // ci¹g nierozpoznany (nie jest komend¹)
bCommand = true; // do wys³ania
}
return eCommand;
}
bool TMemCell::Load(cParser *parser)
{
std::string token;
parser->getTokens(1, false); // case sensitive
*parser >> token;
SafeDeleteArray(szText);
szText = new char[256]; // musi byæ bufor do ³¹czenia tekstów
strcpy(szText, token.c_str());
parser->getTokens();
*parser >> fValue1;
parser->getTokens();
*parser >> fValue2;
parser->getTokens();
*parser >> token;
if (token.compare("none") != 0) // gdy ró¿ne od "none"
asTrackName = AnsiString(token.c_str()); // sprawdzane przez IsEmpty()
parser->getTokens();
*parser >> token;
if (token.compare("endmemcell") != 0)
Error("endmemcell statement missing");
CommandCheck();
return true;
}
void TMemCell::PutCommand(TController *Mech, vector3 *Loc)
{ // wys³anie zawartoœci komórki do AI
if (Mech)
Mech->PutCommand(szText, fValue1, fValue2, Loc);
}
bool TMemCell::Compare(char *szTestText, double fTestValue1, double fTestValue2, int CheckMask)
{ // porównanie zawartoœci komórki pamiêci z podanymi wartoœciami
if (TestFlag(CheckMask, conditional_memstring))
{ // porównaæ teksty
char *pos = StrPos(szTestText, "*"); // zwraca wskaŸnik na pozycjê albo NULL
if (pos)
{ // porównanie fragmentu ³añcucha
int i = pos - szTestText; // iloœæ porównywanych znaków
if (i) // jeœli nie jest pierwszym znakiem
if (AnsiString(szTestText, i) != AnsiString(szText, i))
return false; // pocz¹tki o d³ugoœci (i) s¹ ró¿ne
}
else if (AnsiString(szTestText) != AnsiString(szText))
return false; //³¹ñcuchy s¹ ró¿ne
}
// tekst zgodny, porównaæ resztê
return ((!TestFlag(CheckMask, conditional_memval1) || (fValue1 == fTestValue1)) &&
(!TestFlag(CheckMask, conditional_memval2) || (fValue2 == fTestValue2)));
};
bool TMemCell::Render()
{
return true;
}
bool TMemCell::IsVelocity()
{ // sprawdzenie, czy event odczytu tej komórki ma byæ do skanowania, czy do kolejkowania
if (eCommand == cm_SetVelocity)
return true;
if (eCommand == cm_ShuntVelocity)
return true;
return (eCommand == cm_SetProximityVelocity);
};
void TMemCell::StopCommandSent()
{ //
if (!bCommand)
return;
bCommand = false;
if (OnSent) // jeœli jest event
Global::AddToQuery(OnSent, NULL);
};
void TMemCell::AssignEvents(TEvent *e)
{ // powi¹zanie eventu
OnSent = e;
};