
var renderClass = "jp.ngt.rtm.render.VehiclePartsRenderer";
importPackage(Packages.org.lwjgl.opengl);
importPackage(Packages.jp.ngt.rtm.render);
importPackage(Packages.jp.ngt.ngtlib.math);
importPackage(Packages.jp.ngt.rtm)
importPackage(Packages.jp.ngt.rtm.entity.train.util);
importPackage(Packages.jp.ngt.ngtlib.io);
importPackage(Packages.jp.ngt.rtm.entity.vehicle);
importPackage(Packages.jp.ngt.ngtlib.util);
importPackage(Packages.jp.ngt.ngtlib.renderer);
importPackage(Packages.net.minecraft.util);//ResourceLocation
//データ保存処理(グローバル関数)
var LocalData = new java.util.HashMap();

//# オブジェクト定義 ###
function init(par1, par2) {
	//車体
	body = renderer.registerParts(new Parts("body", "body-s", "parts", "skirt", "connect", "under", "int", "int-parts", "seat", "seat-s", "tsurikawa"));
	alpha = renderer.registerParts(new Parts("window"));
	door_LF = renderer.registerParts(new Parts("door_LF"));
	door_RF = renderer.registerParts(new Parts("door_RF"));
	door_LB = renderer.registerParts(new Parts("door_LB"));
	door_RB = renderer.registerParts(new Parts("door_RB"));
	door_LFa = renderer.registerParts(new Parts("door_LFa"));
	door_RFa = renderer.registerParts(new Parts("door_RFa"));
	door_LBa = renderer.registerParts(new Parts("door_LBa"));
	door_RBa = renderer.registerParts(new Parts("door_RBa"));

	//灯具類
	F_Fon = renderer.registerParts(new Parts("Flight-Fon"));//前方前照灯点灯
	F_Fof = renderer.registerParts(new Parts("Flight-Fof"));//前方前照灯消灯
	F_Bon = renderer.registerParts(new Parts("Flight-Bon"));//前方尾灯点灯
	F_Bof = renderer.registerParts(new Parts("Flight-Bof"));//前方尾灯消灯

	B_Fon = renderer.registerParts(new Parts("Blight-Fon"));//後方前照灯点灯
	B_Fof = renderer.registerParts(new Parts("Blight-Fof"));//後方前照灯消灯
	B_Bon = renderer.registerParts(new Parts("Blight-Bon"));//後方尾灯点灯
	B_Bof = renderer.registerParts(new Parts("Blight-Bof"));//後方尾灯消灯

	//幌
	hoodF = renderer.registerParts(new Parts("hood-F"));
	hoodB = renderer.registerParts(new Parts("hood-B"));

	//カスタムボタン系
	tankA = renderer.registerParts(new Parts("tankA")); //タンク有
	tankB = renderer.registerParts(new Parts("tankB")); //タンク無
	cooler = renderer.registerParts(new Parts("cooler", "cooler-s")); //クーラーあり
	cooler_none = renderer.registerParts(new Parts("cooler_none")); //クーラーなし
	T1 = renderer.registerParts(new Parts("T1_a", "T1_b")); //トイレ窓あり
	T2 = renderer.registerParts(new Parts("T2")); //トイレ窓なし
	plaw_true = renderer.registerParts(new Parts("plaw_true", "plaw_true_s")); //プラウあり
	plaw_false = renderer.registerParts(new Parts("plaw_false")); //プラウなし
	plaw_bar = renderer.registerParts(new Parts("plaw_bar")); //棒
	bench_true = renderer.registerParts(new Parts("bench_true")); //ベンチレーターあり
	bench_false = renderer.registerParts(new Parts("bench_false")); //ベンチレーターなし

	//ジャンパ栓
	jumper_F1 = renderer.registerParts(new Parts("jumper-F1"));
	jumper_F2 = renderer.registerParts(new Parts("jumper-F2"));
	jumper_F3 = renderer.registerParts(new Parts("jumper-F3"));
	jumper_B1 = renderer.registerParts(new Parts("jumper-B1"));
	jumper_B2 = renderer.registerParts(new Parts("jumper-B2"));
	jumper_B3 = renderer.registerParts(new Parts("jumper-B3"));

	//運転室
	Fcab_off = renderer.registerParts(new Parts("Fcab-of", "Fcab-of-s"));
	Fcab_on = renderer.registerParts(new Parts("Fcab-on", "Fcab-on-s"));
	Bcab_off = renderer.registerParts(new Parts("Bcab-of", "Bcab-of-s"));
	Bcab_on = renderer.registerParts(new Parts("Bcab-on", "Bcab-on-s"));

	//貫通路
	Fdoor_C = renderer.registerParts(new Parts("FDC"));
	Fdoor_C_of = renderer.registerParts(new Parts("FDC-of", "FDC-ofs"));
	Fdoor_C_on = renderer.registerParts(new Parts("FDC-on", "FDC-ons"));
	Fdoor_O = renderer.registerParts(new Parts("FDO", "FDO-s"));
	Fdoor_Cw = renderer.registerParts(new Parts("FDCw"));
	Fdoor_Ow = renderer.registerParts(new Parts("FDOw"));

	Bdoor_C = renderer.registerParts(new Parts("BDC"));
	Bdoor_C_of = renderer.registerParts(new Parts("BDC-of", "BDC-ofs"));
	Bdoor_C_on = renderer.registerParts(new Parts("BDC-on", "BDC-ons"));
	Bdoor_O = renderer.registerParts(new Parts("BDO", "BDO-s"));
	Bdoor_Cw = renderer.registerParts(new Parts("BDCw"));
	Bdoor_Ow = renderer.registerParts(new Parts("BDOw"));

	//台車
	bogieF = renderer.registerParts(new Parts("Fbogie")); //前台車枠
	wheelF1 = renderer.registerParts(new Parts("FwheelF")); //前車輪前
	wheelF2 = renderer.registerParts(new Parts("FwheelB")); //前車輪後
	bogieB = renderer.registerParts(new Parts("Bbogie")); //前台車枠
	wheelB1 = renderer.registerParts(new Parts("BwheelF")); //前車輪前
	wheelB2 = renderer.registerParts(new Parts("BwheelB")); //前車輪後
	//init内で定義した変数は使い回せるので固定値などはここで設定しておくと良

	//車両設定
	bogieSet = {};
	bogieSet.bd0 = 7.2; //bogiePos前
	bogieSet.bd1 = -7.2; //bogiePos後
	bogieSet.wy0 = -0.57; //車軸Y
	bogieSet.wz1 = 8.25; //車軸1 Z
	bogieSet.wz2 = 6.15; //車軸2 Z
	bogieSet.wz3 = -6.15; //車軸3 Z
	bogieSet.wz4 = -8.25; //車軸4 Z

	//幕(ひーさん制作オブジェクト定義)
	s_t = [];
	s_d = [];
	for (var i = 0; i < 70; i++) {
		var objName_t = "s_t" + i;
		var objName_d = "s_d" + i;
		s_t[i] = renderer.registerParts(new Parts(objName_t));
		s_d[i] = renderer.registerParts(new Parts(objName_d));
	}
}

//# render ############
function render(entity, pass, par3) {
	//データ保存処理の初期化
	//Entityをキーにしてるので競合を起こさない(setData/getDataのビットシフトより確実性が高い)
	//必ず一番最初に実行されるように書く
	var data = LocalData.get(entity) || {};
	LocalData.put(entity, data);
	syncCustomButton(entity);
	var Middle = isMiddleCar(entity);
	var varsion = MCVersionChecker();

	//数値設定
	var doorMove = 0.95; //ドア開閉距離(m)

	GL11.glPushMatrix();


	//通常描画
	if (pass == 0) {
		body.render(renderer);
		render_door(entity, doorMove);
		render_light(entity, Middle, pass);
		render_sign(entity, pass, par3, Middle);
		render_cab(entity, pass, par3, Middle);
		render_bogie(entity);
		render_parts(entity);
		syncCustomButton(entity);
	}

	//半透明描画
	if (pass == 1) {
		if (varsion == "1.12.2") { //RTM1でない(RTM2であるとき)
			GLHelper.setBrightness(entity ? entity.func_70070_b() : 0); //明るさ再設定
		}
		alpha.render(renderer);
		render_door_a(entity, doorMove);
		render_sign(entity, pass, par3, Middle);
		render_cab(entity, pass, par3, Middle);
		syncCustomButton(entity);

	}

	//発光部描画
	if (pass > 1) {
		body.render(renderer);
		render_light(entity, Middle, pass);
		render_door(entity, doorMove);
		render_sign(entity, pass, par3, Middle);
		render_cab(entity, pass, par3, Middle);
		render_parts(entity);
		syncCustomButton(entity);
	}
	GL11.glPopMatrix();

}

//# バージョンチェック #
function MCVersionChecker() {
	var varsion = RTMCore.VERSION;
	if (varsion.indexOf("1.7.10") >= 0) return "1.7.10";
	else if (varsion.indexOf("2.0") >= 0) return "1.8.9";
	else if (varsion.indexOf("2.1") >= 0) return "1.9.4";
	else if (varsion.indexOf("2.2") >= 0) return "1.10.2";
	else if (varsion.indexOf("2.4") >= 0) return "1.12.2";
	else return "unknown";
}

//# 号車取得 ##########
function isMiddleCar(entity) {//trueなら中間車、falseなら先頭車/後尾車

	//-通さない処理-
	if (entity === null) return;
	//if (renderer.currentMatId !== 5) return;//材質でも走査されるため

	var data = LocalData.get(entity);//保存された"データ群"の呼び出し
	var state = false;

	var formation = entity.getFormation();//formation型
	if (formation == null) {//formationが取得できないときの処理
		state = data.prevstate;
		if (data.prevstate == null) state = false;
	} else {
		var current = formation.getEntry(entity).entryId + 1;//この車両の号車を取得。0から数えるため+1する
		var max = formation.size();//連結してる両数を返す
		if (current == 1 || current == max) {//この車両が先頭or最後尾なら
			state = false;
		} else {//そうでない(つまり中間)なら
			state = true;
		}
	}

	data.prevstate = state;
	LocalData.put(entity, data);

	return state;
}

//# 両数取得 ##########
function isCarSize(entity) {
	if (entity != null) {
		var formation = entity.getFormation();//formation型
		if (formation == null) {//formationが取得できないときの処理
			return 1;
		}
		var max = formation.size();//連結してる両数を返す
		return max;
	}
}

//# 隣接車両取得 #######
function isFrontNextCar(entity) {//trueなら車両前方に、falseなら車両後方に車両あり※先頭車でのみ実行
	if (entity != null) {
		var frontNext = entity.getConnectedTrain(0);
		if (frontNext == null) {//前方に隣接車の情報が取得できない≒前方に隣接車なし＝後方に隣接車あり
			return false;
		} else {
			return true;
		}
	}
}

//カスタムボタン同期
function syncCustomButton(entity) {
	if (entity != null) {


		var formation = entity.getFormation();
		if (formation == null) {
			return;
		}
		var m = formation.getClass().getDeclaredMethod("getControlCar");
		m.setAccessible(true);
		var controlCar = m.invoke(formation);
		if (controlCar == null || controlCar == entity) {
			return;
		}
		var currentCarDataMap = entity.getResourceState().getDataMap()
		var controlCarDataMap = controlCar.getResourceState().getDataMap();

		var controlCarModelSet = controlCar.getResourceState().getResourceSet();

		if (controlCarModelSet == null) {
			return;
		}

		for (var i = 0; i < controlCarModelSet.getConfig().customButtons.length; i++) {
			var keyList = ['Button0', 'Button547733687493'];
			keyList.forEach(function (key) {
				var controlCarValue = controlCarDataMap.getInt(key);
				var currentCarValue = currentCarDataMap.getInt(key);
				if (controlCarValue != currentCarValue) {
					currentCarDataMap.setInt(key, controlCarValue, 3)
				}
			})
		}
	}
}

//# 運転席 #############
function render_cab(entity, pass, par3, Middle) {//運転席

	if (entity != null && entity.getFormation() != null && entity.getFormation().getEntry(entity) != null) {
		var frontTrainName = "";
		var backTrainName = "";
		var frontTrain = entity.getConnectedTrain(0);
		var backTrain = entity.getConnectedTrain(1);
		var varsion = MCVersionChecker();
		var Frnt = 0;
		var dire = entity.getFormation().getEntry(entity).dir;
		var data = LocalData.get(entity);//保存された"データ群"の呼び出し
		var Middle = isMiddleCar(entity);
		var Fnext = isFrontNextCar(entity);
		var max = isCarSize(entity);
		var CB1 = entity == null ? 0 : entity.getResourceState().getDataMap().getInt("Button0");//幌の向き


		if (entity != null) {
			if (varsion == "1.7.10" || varsion == "1.8.9" || varsion == "1.9.4") {
				var st0 = entity.getTrainStateData(0); //進行方向
			} else {
				var st0 = entity.getVehicleState(TrainState.getStateType(0)); //進行方向
			}

		}

		if (frontTrain != null) {
			if (varsion == "1.7.10" || varsion == "1.8.9" || varsion == "1.9.4") {
				frontTrainName = frontTrain.getModelName();
			} else {
				frontTrainName = frontTrain.getResourceState().getResourceName();
			}
		}

		if (backTrain != null) {
			if (varsion == "1.7.10" || varsion == "1.8.9" || varsion == "1.9.4") {
				backTrainName = backTrain.getModelName();
			} else {
				backTrainName = backTrain.getResourceState().getResourceName();
			}
		}

		if (Frnt != null) {
			if (varsion == "1.7.10" || varsion == "1.8.9" || varsion == "1.9.4") {
				Frnt = entity.getTrainStateData(10);//レバーサー

			} else {
				Frnt = entity.getVehicleState(TrainState.getStateType(10));
			}
		}

	}

	GL11.glPushMatrix();

	if (max > 1) { //単行でないとき
		if (Middle == false) { //編成両端であるとき
			if (Fnext == false) { //自車後方にほかの車両があるとき
				if (dire == 0) { //正の向き
					if (backTrainName.indexOf("sDC40") != -1) { //後方が"sDC40"を含んでいるとき
						switch (CB1) {
							case 0:
								GL11.glPushMatrix();
								hoodF.render(renderer);
								jumper_F1.render(renderer);
								jumper_B3.render(renderer);
								GL11.glPopMatrix();
								break;
							case 1:
								GL11.glPushMatrix();
								jumper_F2.render(renderer);
								jumper_B3.render(renderer);
								GL11.glPopMatrix();
								break;

						}
					} else {
						switch (CB1) {
							case 0:
								GL11.glPushMatrix();
								hoodF.render(renderer);
								jumper_F1.render(renderer);
								jumper_B2.render(renderer);
								GL11.glPopMatrix();
								break;
							case 1:
								GL11.glPushMatrix();
								hoodB.render(renderer);
								jumper_F2.render(renderer);
								jumper_B1.render(renderer);
								GL11.glPopMatrix();
								break;
						}
					}
				} else {
					if (backTrainName.indexOf("sDC40") != -1) { //後方が"sDC40"を含んでいるとき
						switch (CB1) {
							case 0:
								GL11.glPushMatrix();
								jumper_F2.render(renderer);
								jumper_B3.render(renderer);
								GL11.glPopMatrix();
								break;
							case 1:
								GL11.glPushMatrix();
								hoodF.render(renderer);
								jumper_F1.render(renderer);
								jumper_B3.render(renderer);
								GL11.glPopMatrix();
								break;
						}
					} else {
						switch (CB1) {
							case 0:
								GL11.glPushMatrix();
								hoodB.render(renderer);
								jumper_F2.render(renderer);
								jumper_B1.render(renderer);
								GL11.glPopMatrix();
								break;
							case 1:
								GL11.glPushMatrix();
								hoodF.render(renderer);
								jumper_F1.render(renderer);
								jumper_B2.render(renderer);
								GL11.glPopMatrix();
								break;
						}
					}
				}
				if (Frnt == 0) { //レバーサー前
					GL11.glPushMatrix();
					Fcab_off.render(renderer);
					Fdoor_C_of.render(renderer);
					Bcab_on.render(renderer);
					Bdoor_O.render(renderer);
					GL11.glPopMatrix();
				} else {
					GL11.glPushMatrix();
					Fcab_on.render(renderer);
					Fdoor_C_on.render(renderer);
					Bcab_on.render(renderer);
					Bdoor_O.render(renderer);
					GL11.glPopMatrix();
				}
				if (backTrainName.indexOf("sDC40") != -1) { //後方が"sDC40"を含んでいるとき
					GL11.glPushMatrix();
					Fdoor_Cw.render(renderer);
					Fdoor_C.render(renderer);
					Bdoor_O.render(renderer);
					Bdoor_Ow.render(renderer);
					GL11.glPopMatrix();
				} else {
					GL11.glPushMatrix();
					Fdoor_Cw.render(renderer);
					Fdoor_C.render(renderer);
					Bdoor_C.render(renderer);
					Bdoor_Cw.render(renderer);
					GL11.glPopMatrix();
				}
			} else { //自車前方にほかの車両があるとき
				if (frontTrainName.indexOf("sDC40") != -1) {    //前方が"sDC40"を含んでいるとき
					switch (CB1) {
						case 0:
							GL11.glPushMatrix();
							hoodB.render(renderer);
							jumper_B1.render(renderer);
							GL11.glPopMatrix();
							break;
						case 1:
							GL11.glPushMatrix();
							jumper_B2.render(renderer);
							GL11.glPopMatrix();
							break;
					}
					if (Frnt == 0) { //レバーサー前
						GL11.glPushMatrix();
						Bcab_off.render(renderer);
						Bdoor_C_of.render(renderer);
						Fcab_on.render(renderer);
						GL11.glPopMatrix();
					} else {
						GL11.glPushMatrix();
						Bcab_on.render(renderer);
						Bdoor_C_on.render(renderer);
						Fcab_on.render(renderer);
						GL11.glPopMatrix();
					}
					GL11.glPushMatrix();
					jumper_F3.render(renderer);
					Fdoor_O.render(renderer);
					Fdoor_Ow.render(renderer);
					Bdoor_C.render(renderer);
					Bdoor_Cw.render(renderer);
					GL11.glPopMatrix();
				} else {
					switch (CB1) {
						case 0:
							GL11.glPushMatrix();
							hoodF.render(renderer);
							jumper_F1.render(renderer);
							jumper_B2.render(renderer);
							GL11.glPopMatrix();
							break;
						case 1:
							GL11.glPushMatrix();
							hoodB.render(renderer);
							jumper_B1.render(renderer);
							jumper_F2.render(renderer);
							GL11.glPopMatrix();
							break;
					}
					if (Frnt == 0) { //レバーサー前
						GL11.glPushMatrix();
						Bcab_off.render(renderer);
						Bdoor_C_of.render(renderer);
						Fcab_on.render(renderer);
						Fdoor_C_on.render(renderer);
						GL11.glPopMatrix();
					} else {
						GL11.glPushMatrix();
						Bcab_on.render(renderer);
						Bdoor_C_on.render(renderer);
						Fcab_on.render(renderer);
						Fdoor_C_on.render(renderer);
						GL11.glPopMatrix();
					}

					GL11.glPushMatrix();
					Fdoor_C.render(renderer);
					Fdoor_Cw.render(renderer);
					Bdoor_C.render(renderer);
					Bdoor_Cw.render(renderer);
					GL11.glPopMatrix();
				}
			}
		} else { //編成中間であるとき
			if (frontTrainName.indexOf("sDC40") != -1) {    //前方が"sDC40"を含んでいるとき
				if (backTrainName.indexOf("sDC40") != -1) { //後方が"sDC40"を含んでいるとき(前後両方)
					switch (CB1) {
						case 0:
							GL11.glPushMatrix();
							jumper_F3.render(renderer);
							jumper_B3.render(renderer);
							GL11.glPopMatrix();
							break;
						case 1:
							GL11.glPushMatrix();
							jumper_F3.render(renderer);
							jumper_B3.render(renderer);
							GL11.glPopMatrix();
							break;
					}
					GL11.glPushMatrix();
					Fcab_on.render(renderer);
					Fdoor_O.render(renderer);
					Fdoor_Ow.render(renderer);
					Bcab_on.render(renderer);
					Bdoor_O.render(renderer);
					Bdoor_Ow.render(renderer);
					GL11.glPopMatrix();
				} else {                                    //前方のみ"sDC40"を含んでいるとき
					switch (CB1) {
						case 0:
							GL11.glPushMatrix();
							jumper_F3.render(renderer);
							jumper_B2.render(renderer);
							GL11.glPopMatrix();
							break;
						case 1:
							GL11.glPushMatrix();
							jumper_F3.render(renderer);
							jumper_B1.render(renderer);
							GL11.glPopMatrix();
							break;
					}
					GL11.glPushMatrix();
					cab_on.render(renderer);
					Fdoor_O.render(renderer);
					Fdoor_Ow.render(renderer);
					Bcab_on.render(renderer);
					Bdoor_C_on.render(renderer);
					Bdoor_Cw.render(renderer);
					GL11.glPopMatrix();
				}
			} else {                                        //前方が"sDC40"を含んでいないとき
				if (backTrainName.indexOf("sDC40") != -1) { //後方が"sDC40"を含んでいるとき(後方のみ)
					switch (CB1) {
						case 0:
							GL11.glPushMatrix();
							jumper_F1.render(renderer);
							jumper_B3.render(renderer);
							GL11.glPopMatrix();
							break;
						case 1:
							GL11.glPushMatrix();
							hoodF.render(renderer);
							jumper_F2.render(renderer);
							jumper_B3.render(renderer);
							GL11.glPopMatrix();
							break;
					}
					GL11.glPushMatrix();
					Fcab_on.render(renderer);
					Fdoor_C.render(renderer);
					Fdoor_C_on.render(renderer);
					Fdoor_Cw.render(renderer);
					Bcab_on.render(renderer);
					Bdoor_O.render(renderer);
					Bdoor_Ow.render(renderer);
					GL11.glPopMatrix();
				} else {                                    //前後とも"sDC40"を含んでいないとき
					switch (CB1) {
						case 0:
							GL11.glPushMatrix();
							jumper_F1.render(renderer);
							jumper_B2.render(renderer);
							GL11.glPopMatrix();
							break;
						case 1:
							GL11.glPushMatrix();
							jumper_F2.render(renderer);
							jumper_B1.render(renderer);
							GL11.glPopMatrix();
							break;
					}
					GL11.glPushMatrix();
					Fcab_on.render(renderer);
					Fdoor_C_on.render(renderer);
					Fdoor_Cw.render(renderer);
					Bcab_on.render(renderer);
					Bdoor_C_on.render(renderer);
					Bdoor_Cw.render(renderer);
					GL11.glPopMatrix();
				}
			}
		}
	} else { //単行であるとき
		switch (CB1) {
			case 0:
				GL11.glPushMatrix();
				hoodF.render(renderer);
				jumper_F1.render(renderer);
				jumper_B2.render(renderer);
				GL11.glPopMatrix();
				break;
			case 1:
				GL11.glPushMatrix();
				hoodB.render(renderer);
				jumper_F2.render(renderer);
				jumper_B1.render(renderer);
				GL11.glPopMatrix();
				break;
		}
		if (st0 == 0) {//前方に進むとき(=進行方向最前)
			GL11.glPushMatrix();
			Fcab_off.render(renderer);
			Fdoor_C.render(renderer);
			Fdoor_C_of.render(renderer);
			Fdoor_Cw.render(renderer);
			Bcab_on.render(renderer);
			Bdoor_C.render(renderer);
			Bdoor_C_on.render(renderer);
			Bdoor_Cw.render(renderer);
			GL11.glPopMatrix();
		} else {
			GL11.glPushMatrix();
			Fcab_on.render(renderer);
			Fdoor_C.render(renderer);
			Fdoor_C_on.render(renderer);
			Fdoor_Cw.render(renderer);
			Bcab_off.render(renderer);
			Bdoor_C.render(renderer);
			Bdoor_C_of.render(renderer);
			Bdoor_Cw.render(renderer);
			GL11.glPopMatrix();
		}
	}
	GL11.glPopMatrix();
}

//##### render_ライト ####################
function render_light(entity) {

	var varsion = MCVersionChecker();

	if (entity != null) {
		var form = isMiddleCar(entity);
		var Fnext = isFrontNextCar(entity);
		var max = isCarSize(entity);
	}



	if (entity != null) {
		if (varsion == "1.7.10" || varsion == "1.8.9" || varsion == "1.9.4") {
			var st0 = entity.getTrainStateData(0); //進行方向
		} else {
			var st0 = entity.getVehicleState(TrainState.getStateType(0)); //進行方向
		}
	}

	if (max > 1) {//単行でないとき
		if (form == false) {//編成両端であるとき
			if (Fnext == false) {//自車後方にほかの車両があるとき
				if (st0 == 0) {//前方に進むとき(=進行方向最前)
					GL11.glPushMatrix();

					F_Fon.render(renderer);
					F_Bof.render(renderer);
					B_Fof.render(renderer);
					B_Bof.render(renderer);


					GL11.glPopMatrix();
				} else {//後方に進むとき(=進行方向最後)
					GL11.glPushMatrix();

					F_Fof.render(renderer);
					F_Bon.render(renderer);
					B_Fof.render(renderer);
					B_Bof.render(renderer);

					GL11.glPopMatrix();
				}
			} else {//自車前方にほかの車両があるとき
				if (st0 == 0) {//前方に進むとき(=進行方向最後)
					GL11.glPushMatrix();

					F_Fof.render(renderer);
					F_Bof.render(renderer);
					B_Fof.render(renderer);
					B_Bon.render(renderer);


					GL11.glPopMatrix();
				} else {//後方に進むとき(=進行方向最前)
					GL11.glPushMatrix();

					F_Fof.render(renderer);
					F_Bof.render(renderer);
					B_Fon.render(renderer);
					B_Bof.render(renderer);


					GL11.glPopMatrix();
				}
			}
		} else {//編成中間であるとき
			GL11.glPushMatrix();

			F_Fof.render(renderer);
			F_Bof.render(renderer);
			B_Fof.render(renderer);
			B_Bof.render(renderer);


			GL11.glPopMatrix();
		}
	} else {//単行であるとき
		if (st0 == 0) {//前方に進むとき
			GL11.glPushMatrix();

			F_Fon.render(renderer);
			F_Bof.render(renderer);
			B_Fof.render(renderer);
			B_Bon.render(renderer);

			GL11.glPopMatrix();
		} else {//後方に進むとき
			GL11.glPushMatrix();

			F_Fof.render(renderer);
			B_Fon.render(renderer);
			B_Bof.render(renderer);
			F_Bon.render(renderer);

			GL11.glPopMatrix();
		}
	}

}

//##### render_ドア ####################
function render_door(entity, doorMove) {

	var doorMoveL = 0.0,
		doorMoveR = 0.0;

	try {
		doorMoveL = renderer.sigmoid(entity.doorMoveL / 60) * doorMove;
		doorMoveR = renderer.sigmoid(entity.doorMoveR / 60) * doorMove;
	} catch (e) { }

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, doorMoveL);
	door_LF.render(renderer);
	GL11.glPopMatrix();

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, -doorMoveL);
	door_LB.render(renderer);
	GL11.glPopMatrix();

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, doorMoveR);
	door_RF.render(renderer);
	GL11.glPopMatrix();

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, -doorMoveR);
	door_RB.render(renderer);
	GL11.glPopMatrix();
}

//##### render_半透ドア ####################
function render_door_a(entity, doorMove) {

	var doorMoveL = 0.0,
		doorMoveR = 0.0;

	try {
		doorMoveL = renderer.sigmoid(entity.doorMoveL / 60) * doorMove;
		doorMoveR = renderer.sigmoid(entity.doorMoveR / 60) * doorMove;
	} catch (e) { }

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, doorMoveL);
	door_LFa.render(renderer);
	GL11.glPopMatrix();

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, -doorMoveL);
	door_LBa.render(renderer);
	GL11.glPopMatrix();

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, doorMoveR);
	door_RFa.render(renderer);
	GL11.glPopMatrix();

	GL11.glPushMatrix();
	GL11.glTranslatef(0.0, 0.0, -doorMoveR);
	door_RBa.render(renderer);
	GL11.glPopMatrix();
}

//##### render_方向幕 ####################
function render_sign(entity, pass, par3, Middle) {

	var varsion = MCVersionChecker();
	var st8 = 0;
	var st9 = 0

	if (entity != null) {
		if (varsion == "1.7.10" || varsion == "1.8.9" || varsion == "1.9.4") {
			st8 = Math.floor(entity.getTrainStateData(9));
			st9 = Math.floor(entity.getTrainStateData(8));
		} else {
			st8 = Math.floor(entity.getVehicleState(TrainState.getStateType(9)));
			st9 = Math.floor(entity.getVehicleState(TrainState.getStateType(8)));
		}

	}

	if (st8 < 0 && st8 > 29) { st8 = 0; }
	if (st9 < 0 && st9 > 70) { st9 = 0; }

	GL11.glPushMatrix();
	s_t[st8].render(renderer);
	s_d[st9].render(renderer);
	GL11.glPopMatrix();


}

//##### render_台車 hi03 Bogiekitより ####################
function render_bogie(entity) {

	//初期化
	var f0 = renderer.getWheelRotationR(entity),
		bogieFYaw = 0.0,
		bogieBYaw = 0.0,
		bogieFPitch = 0.0,
		bogieBPitch = 0.0;


	//取得
	if (entity != null && entity.getBogie(0) != null && entity.getBogie(1) != null) {
		var bodyYaw = entity.field_70177_z;

		//前台車ヨーローカル角
		var i = entity.getBogie(0).field_70177_z - bodyYaw;
		if (i > 180) {
			bogieFYaw = i - 360;
		} else if (i < -180) {
			bogieFYaw = i + 360;
		} else {
			bogieFYaw = i;
		}
		//後台車ヨーローカル角
		var i = entity.getBogie(1).field_70177_z - bodyYaw - 180;
		if (i > 180) {
			bogieBYaw = i - 360;
		} else if (i < -180) {
			bogieBYaw = i + 360;
		} else {
			bogieBYaw = i;
		}

		//ピッチローカル角
		var bodyPitch = entity.field_70125_A;
		bogieFPitch = entity.getBogie(0).field_70125_A * -1 + bodyPitch;
		bogieBPitch = entity.getBogie(1).field_70125_A + bodyPitch;

	}


	//前台車
	GL11.glPushMatrix();
	renderer.rotate(bogieFYaw, 'Y', 0.0, 0.0, bogieSet["bd0"]);
	renderer.rotate(bogieFPitch, 'X', 0.0, 0.0, bogieSet["bd0"]);
	bogieF.render(renderer);
	GL11.glPushMatrix();
	renderer.rotate(f0, 'X', 0.0, bogieSet["wy0"], bogieSet["wz1"]);
	wheelF1.render(renderer);
	GL11.glPopMatrix();
	GL11.glPushMatrix();
	renderer.rotate(f0, 'X', 0.0, bogieSet["wy0"], bogieSet["wz2"]);
	wheelF2.render(renderer);
	GL11.glPopMatrix();
	GL11.glPopMatrix();

	//後台車
	GL11.glPushMatrix();
	renderer.rotate(bogieBYaw, 'Y', 0.0, 0.0, -bogieSet["bd0"]);
	renderer.rotate(bogieBPitch, 'X', 0.0, 0.0, -bogieSet["bd0"]);
	bogieB.render(renderer);
	GL11.glPushMatrix();
	renderer.rotate(f0, 'X', 0.0, bogieSet["wy0"], bogieSet["wz3"]);
	wheelB1.render(renderer);
	GL11.glPopMatrix();
	GL11.glPushMatrix();
	renderer.rotate(f0, 'X', 0.0, bogieSet["wy0"], bogieSet["wz4"]);
	wheelB2.render(renderer);
	GL11.glPopMatrix();
	GL11.glPopMatrix();
}


//##### render_その他 ####################
function render_parts(entity) {

	var CB2 = entity == null ? 0 : entity.getResourceState().getDataMap().getInt("Button1");//タンク
	var CB3 = entity == null ? 0 : entity.getResourceState().getDataMap().getInt("Button2");//クーラー
	var CB4 = entity == null ? 0 : entity.getResourceState().getDataMap().getInt("Button3");//トイレ窓
	var CB5 = entity == null ? 0 : entity.getResourceState().getDataMap().getInt("Button4");//プラウ
	var CB6 = entity == null ? 0 : entity.getResourceState().getDataMap().getInt("Button5");//ベンチレーター

	switch (CB2) {
		case 0:
			GL11.glPushMatrix();
			tankA.render(renderer);
			GL11.glPopMatrix();
			break;
		case 1:
			GL11.glPushMatrix();
			tankB.render(renderer);
			GL11.glPopMatrix();
			break;
	}

	switch (CB3) {
		case 0:
			GL11.glPushMatrix();
			cooler.render(renderer);
			GL11.glPopMatrix();
			break;
		case 1:
			GL11.glPushMatrix();
			cooler_none.render(renderer);
			GL11.glPopMatrix();
			break;
	}

	switch (CB4) {
		case 0:
			GL11.glPushMatrix();
			T1.render(renderer);
			GL11.glPopMatrix();
			break;
		case 1:
			GL11.glPushMatrix();
			T2.render(renderer);
			GL11.glPopMatrix();
			break;
	}

	switch (CB5) {
		case 0:
			GL11.glPushMatrix();
			plaw_true.render(renderer);
			GL11.glPopMatrix();
			break;
		case 1:
			GL11.glPushMatrix();
			plaw_bar.render(renderer);
			GL11.glPopMatrix();
			break;
		case 2:
			GL11.glPushMatrix();
			plaw_false.render(renderer);
			GL11.glPopMatrix();
			break;
	}

	switch (CB6) {
		case 0:
			GL11.glPushMatrix();
			bench_true.render(renderer);
			GL11.glPopMatrix();
			break;
		case 1:
			GL11.glPushMatrix();
			bench_false.render(renderer);
			GL11.glPopMatrix();
			break;
	}

}
