| 1 |
#include "stdafx.h" |
| 2 |
#include "RailMap.h" |
| 3 |
#include "CSimpleDialog.h" |
| 4 |
#include "CRailWay.h" |
| 5 |
#include "CScene.h" |
| 6 |
#include "CTrain.h" |
| 7 |
#include "CTrainGroup.h" |
| 8 |
#include "CTrainPlugin.h" |
| 9 |
#include "CSkinPlugin.h" |
| 10 |
#include "CListView.h" |
| 11 |
#include "CSimulationMode.h" |
| 12 |
|
| 13 |
/* |
| 14 |
* コンストラクタ (読込用) |
| 15 |
*/ |
| 16 |
CTrain::CTrain( |
| 17 |
CTrainGroup *group // 編成 |
| 18 |
){ |
| 19 |
m_Reverse = false; |
| 20 |
m_Warping = m_WarpingOld = false; |
| 21 |
m_OldPos[0] = m_OldPos[1] = m_TiltDir = V3ZERO; |
| 22 |
m_Group = group; |
| 23 |
m_TrainPlugin = NULL; |
| 24 |
m_ListElement = NULL; |
| 25 |
m_Next = NULL; |
| 26 |
} |
| 27 |
|
| 28 |
/* |
| 29 |
* コンストラクタ |
| 30 |
*/ |
| 31 |
CTrain::CTrain( |
| 32 |
CTrainPlugin *tpi, // 車輌プラグイン |
| 33 |
CTrainGroup *group, // 編成 |
| 34 |
bool rev // 反転フラグ |
| 35 |
): |
| 36 |
CModelInst(tpi) // 基本クラス |
| 37 |
{ |
| 38 |
m_Reverse = rev; |
| 39 |
m_Warping = m_WarpingOld = false; |
| 40 |
m_OldPos[0] = m_OldPos[1] = m_TiltDir = V3ZERO; |
| 41 |
m_Group = group; |
| 42 |
m_TrainPlugin = tpi; |
| 43 |
m_TrainPlugin->SetAxleList(this); |
| 44 |
m_ListElement = NULL; |
| 45 |
m_Next = NULL; |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* デストラクタ |
| 50 |
*/ |
| 51 |
CTrain::~CTrain(){ |
| 52 |
DELETE_V(m_Next); |
| 53 |
} |
| 54 |
|
| 55 |
/* |
| 56 |
* 車輌反転 |
| 57 |
*/ |
| 58 |
void CTrain::Reverse(){ |
| 59 |
if(m_Group->IsSet()){ |
| 60 |
EnqueueCommonDialog(new CSimpleDialog(lang(CannotChangeWhileTrainSet), lang(Error))); |
| 61 |
g_Skin->Error(); |
| 62 |
return; |
| 63 |
} |
| 64 |
m_Reverse = !m_Reverse; |
| 65 |
if(m_ListElement) m_ListElement->SetString(1, m_Reverse ? lang(Yes) : lang(No)); |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* 車輌長を求める |
| 70 |
*/ |
| 71 |
float CTrain::GetLength(){ |
| 72 |
return m_TrainPlugin->m_Length; |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* 車輌配置準備 |
| 77 |
*/ |
| 78 |
float CTrain::SetSetBuffer( |
| 79 |
float ofs, // オフセット |
| 80 |
list<CTrainSetBuffer> *buflist // リスト |
| 81 |
){ |
| 82 |
IAxlePosture ia = m_AxleList.begin(); |
| 83 |
for(; ia!=m_AxleList.end(); ia++){ |
| 84 |
float sumlen = m_Reverse |
| 85 |
? ofs-m_TrainPlugin->m_TailLimit+ia->GetZPos() |
| 86 |
: ofs+m_TrainPlugin->m_FrontLimit-ia->GetZPos(); |
| 87 |
ia->m_Rail = NULL; |
| 88 |
ia->m_Distance = -sumlen; |
| 89 |
buflist->push_back(CTrainSetBuffer(sumlen, m_Reverse, &*ia)); |
| 90 |
} |
| 91 |
ofs += GetLength(); |
| 92 |
m_WarpingOld = false; |
| 93 |
if(m_Next) return m_Next->SetSetBuffer(ofs, buflist); |
| 94 |
else return ofs; |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* シーン取得 |
| 99 |
*/ |
| 100 |
CScene *CTrain::GetScene(){ |
| 101 |
if(m_Warping || !m_AxleList.size()) return NULL; |
| 102 |
CRailWay *rail = m_AxleList.begin()->m_Rail; |
| 103 |
return rail ? rail->GetScene() : NULL; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* シーンチェック |
| 108 |
*/ |
| 109 |
bool CTrain::CheckScene(){ |
| 110 |
CScene *scene = GetScene(); |
| 111 |
if(scene){ |
| 112 |
scene->SetSeason(); |
| 113 |
if(scene!=g_Scene) return false; |
| 114 |
} |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* 車軸適用 |
| 120 |
*/ |
| 121 |
void CTrain::ApplyAxle( |
| 122 |
bool sim // シミュレーションフラグ |
| 123 |
){ |
| 124 |
IAxlePosture ia = m_AxleList.begin(); |
| 125 |
bool oldwarp = m_WarpingOld; |
| 126 |
if(sim){ |
| 127 |
m_OldPos[1] = m_OldPos[0]; |
| 128 |
m_OldPos[0] = m_Pos; |
| 129 |
m_WarpingOld = m_Warping; |
| 130 |
} |
| 131 |
if(m_AxleList.size()){ |
| 132 |
IAxlePosture ia2 = --m_AxleList.end(); |
| 133 |
m_Pos = 0.5f*(ia->m_Pos+ia2->m_Pos); |
| 134 |
m_Dir = ia->m_Dir+ia2->m_Dir; |
| 135 |
m_Up = ia->m_Up+ia2->m_Up; |
| 136 |
V3NormAxis(&m_Right, &m_Up, &m_Dir); |
| 137 |
if(sim){ |
| 138 |
if(oldwarp){ |
| 139 |
ResetTilt(); |
| 140 |
}else{ |
| 141 |
float tiltspeed = m_TrainPlugin->m_TiltSpeed; |
| 142 |
m_TiltDir = (1.0f-tiltspeed)*m_TiltDir |
| 143 |
+tiltspeed*((m_OldPos[0]-m_OldPos[1])-(m_OldPos[1]-m_Pos)); |
| 144 |
} |
| 145 |
} |
| 146 |
CBodyObject::SetTiltDir(m_TiltDir); |
| 147 |
} |
| 148 |
for(; ia!=m_AxleList.end(); ia++) ia->Apply(); |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* 振り子リセット |
| 153 |
*/ |
| 154 |
void CTrain::ResetTilt(){ |
| 155 |
m_OldPos[0] = m_OldPos[1] = m_Pos; |
| 156 |
m_TiltDir = V3ZERO; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* 速度情報表示 |
| 161 |
*/ |
| 162 |
void CTrain::PrintInfo(){ |
| 163 |
m_Group->PrintInfo(); |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* 速度操作 |
| 168 |
*/ |
| 169 |
CModelInst *CTrain::Control(){ |
| 170 |
return m_Group->Control(this); |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
* 運転席視点設定 |
| 175 |
*/ |
| 176 |
void CTrain::SetCabinView( |
| 177 |
bool rev, // 逆方向フラグ |
| 178 |
VEC3 pos, // 座標オフセット |
| 179 |
VEC3 dir // 方向オフセット |
| 180 |
){ |
| 181 |
if(m_Warping) return; |
| 182 |
if(GetScene()!=g_Scene){ |
| 183 |
CCamera cam = *CCamera::GetCurrentCamera(); |
| 184 |
g_Scene = GetScene(); |
| 185 |
*g_Scene->GetCamera() = cam; |
| 186 |
g_Scene->Enter(true); |
| 187 |
*g_Scene->GetCamera() = cam; |
| 188 |
} |
| 189 |
if(m_Reverse) rev = !rev; |
| 190 |
VEC3 vpos, vright, vup, vdir; |
| 191 |
CObjectJoint3D *joint = rev ? &m_TrainPlugin->m_TailCabin : &m_TrainPlugin->m_FrontCabin; |
| 192 |
if(joint->IsLinked()){ |
| 193 |
// m_TrainPlugin->SetSwitch(this); |
| 194 |
// m_TrainPlugin->SetPartsInst(this); |
| 195 |
// ApplyAxle(false); |
| 196 |
m_TrainPlugin->SetPartsInst(this); |
| 197 |
m_TrainPlugin->AttachPartsObject(); |
| 198 |
vpos = joint->GetPos(); |
| 199 |
vup = joint->GetUp(); |
| 200 |
vdir = joint->GetDir(); |
| 201 |
}else{ |
| 202 |
vpos = GetPos()+(rev ? -0.5f : 0.5f)*GetDir()*m_TrainPlugin->m_Length; |
| 203 |
vup = GetUp(); |
| 204 |
vdir = rev ? -GetDir() : GetDir(); |
| 205 |
} |
| 206 |
V3NormAxis(&vright, &vup, &vdir); |
| 207 |
vpos += V3LocalToWorld(&pos, &vright, &vup, &vdir); |
| 208 |
SetView(vpos, vpos+V3LocalToWorld(&dir, &vright, &vup, &vdir), vup); |
| 209 |
} |
| 210 |
|
| 211 |
/* |
| 212 |
* 入力チェック |
| 213 |
*/ |
| 214 |
void CTrain::ScanInput( |
| 215 |
int mode, // モード (0: link) |
| 216 |
VEC3 &rect1, // 領域始点 |
| 217 |
VEC3 &rect2 // 領域終点 |
| 218 |
){ |
| 219 |
if(!CheckScene()) return; |
| 220 |
ms_CurrentInst = this; |
| 221 |
m_TrainPlugin->SetSwitch(this); |
| 222 |
m_TrainPlugin->SetPartsInst(this); |
| 223 |
ApplyAxle(false); |
| 224 |
m_TrainPlugin->SetPartsInst(this); // リセットが要る! |
| 225 |
m_TrainPlugin->ScanInput(this); |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* レンダリング |
| 230 |
*/ |
| 231 |
void CTrain::Render(){ |
| 232 |
if(!CheckScene() || m_Warping) return; |
| 233 |
m_TrainPlugin->SetSwitch(this); |
| 234 |
m_TrainPlugin->SetPartsInst(this); |
| 235 |
// ApplyAxle(false); // ApplyAxle 不要? |
| 236 |
// m_TrainPlugin->SetPartsInst(this); // リセットが要る! |
| 237 |
m_TrainPlugin->Render(this); |
| 238 |
if(g_MapDrawNeeded){ |
| 239 |
float front, tail; |
| 240 |
if(m_Reverse){ |
| 241 |
front = m_TrainPlugin->m_TailLimit; |
| 242 |
tail = m_TrainPlugin->m_FrontLimit; |
| 243 |
}else{ |
| 244 |
front = m_TrainPlugin->m_FrontLimit; |
| 245 |
tail = m_TrainPlugin->m_TailLimit; |
| 246 |
} |
| 247 |
RailMapLine(m_Pos+front*m_Dir, 0xffff0000, |
| 248 |
m_Pos+tail*m_Dir, 0xffff0000, true, true); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/* |
| 253 |
* シミュレート進行 |
| 254 |
*/ |
| 255 |
void CTrain::SimulateModelInst(){ |
| 256 |
CheckScene(); |
| 257 |
if(m_Warping) StopSound(); |
| 258 |
m_TrainPlugin->SetSwitch(this); |
| 259 |
m_TrainPlugin->SetPartsInst(this); |
| 260 |
ApplyAxle(true); |
| 261 |
// m_TrainPlugin->SetPartsInst(this); // CAxlePosture は処理済なのでリセットは不要 |
| 262 |
m_TrainPlugin->Simulate(this); |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* 読込 |
| 267 |
*/ |
| 268 |
char *CTrain::Read( |
| 269 |
char *str, // 対象文字列 |
| 270 |
CTrain ***root // 格納先 |
| 271 |
){ |
| 272 |
char *eee; |
| 273 |
if(!(str = BeginBlock(str, "Train"))){ |
| 274 |
delete this; |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
string pid; |
| 278 |
if(!(str = AsgnString(eee = str, "TrainPlugin", &pid))) throw CSynErr(eee); |
| 279 |
m_ModelPlugin = m_TrainPlugin = g_TrainPluginList->FindPlugin(pid.c_str(), true); |
| 280 |
if(m_TrainPlugin) m_TrainPlugin->SetAxleList(this); |
| 281 |
if(!(str = ReadModelInst(eee = str, true))) throw CSynErr(eee); |
| 282 |
if(!(str = AsgnYesNo(eee = str, "Reverse", &m_Reverse))) throw CSynErr(eee); |
| 283 |
if(!(str = Assignment(eee = str, "Warping"))) throw CSynErr(eee); |
| 284 |
if(!(str = BoolYesNo(eee = str, &m_Warping))) throw CSynErr(eee); |
| 285 |
if(!(str = Character2(eee = str, ','))) throw CSynErr(eee); |
| 286 |
if(!(str = BoolYesNo(eee = str, &m_WarpingOld))) throw CSynErr(eee); |
| 287 |
if(!(str = Character2(eee = str, ';'))) throw CSynErr(eee); |
| 288 |
if(!(str = AsgnVector3D(eee = str, "OldPos", m_OldPos, 2, false))) throw CSynErr(eee); |
| 289 |
if(!(str = AsgnVector3D(eee = str, "TiltDir", &m_TiltDir))) throw CSynErr(eee); |
| 290 |
if(!(str = EndBlock(eee = str))) throw CSynErr(eee, ERR_ENDBLOCK); |
| 291 |
**root = this; |
| 292 |
*root = &m_Next; |
| 293 |
return str; |
| 294 |
} |
| 295 |
|
| 296 |
/* |
| 297 |
* 保存 |
| 298 |
*/ |
| 299 |
void CTrain::Save( |
| 300 |
FILE *df // ファイル |
| 301 |
){ |
| 302 |
fprintf(df, "\t\tTrain{\n"); |
| 303 |
fprintf(df, "\t\t\tTrainPlugin = \"%s\";\n", CheckPluginID(m_TrainPlugin)); |
| 304 |
SaveModelInst(df, "\t\t\t", true); |
| 305 |
fprintf(df, "\t\t\tReverse = %s;\n", YESNO[m_Reverse]); |
| 306 |
fprintf(df, "\t\t\tWarping = %s, %s;\n", YESNO[m_Warping], YESNO[m_WarpingOld]); |
| 307 |
fprintf(df, "\t\t\tOldPos = "); |
| 308 |
V3Save(df, m_OldPos[0], ", "); V3Save(df, m_OldPos[1], ";\n"); |
| 309 |
fprintf(df, "\t\t\tTiltDir = "); |
| 310 |
V3Save(df, m_TiltDir, ";\n"); |
| 311 |
fprintf(df, "\t\t}\n"); |
| 312 |
} |