「センサー2個で交換駅」は汎用性を向上させ、1つのスクリプトに統合できました。
#----------------
#交換駅用76
#----------------
#OBJID=76
import vrmapi
def vrmevent_76(obj,ev,param):
di = obj.GetDict()
if ev == 'init':
di['train'] = 0
di['evid'] = 0
di['thisplatform'] = "中間駅下り" #使用ホーム名
di['thatplatform'] = "中間駅上り" #対向ホーム名
di['inpointid'] = 74 #進入ポイントのID
di['outpointid'] = 75 #退出ポイントのID
di['stopdistance'] = 800.0 #停車距離
di['stoptime'] = 10.0 #停車時間
di['startdistance'] = 600.0 #出発列車電圧設定距離
di['startvoltage'] = 0.8 #出発列車電圧
di['inpoint'] = vrmapi.LAYOUT().GetPoint(di['inpointid'])
di['outpoint'] = vrmapi.LAYOUT().GetPoint(di['outpointid'])
vrmapi.LAYOUT().SetStatusDataInt(di['thisplatform'], 0)
elif ev == 'broadcast':
dummy = 1
elif ev == 'timer':
if param['eventUID'] == 101:
if vrmapi.LAYOUT().GetStatusDataInt(di['thatplatform']) == 1:
obj.SetEventAfter(di['stoptime'],100)
elif ev == 'time':
dummy = 1
elif ev == 'after':
if param['eventUID'] == 100:
di['train'].AutoSpeedCTRL(di['startdistance'],di['startvoltage'])
di['outpoint'].SetBranch(1)
vrmapi.LAYOUT().SetStatusDataInt(di['thisplatform'], 0)
obj.ResetEvent(di['evid'])
elif ev == 'frame':
dummy = 1
elif ev == 'catch':
if obj.GetForward() == 1:
di['inpoint'].SetBranch(0)
vrmapi.LAYOUT().SetStatusDataInt(di['thisplatform'], 1)
di['train'] = obj.GetTrain()
speed = di['train'].GetSpeed()
delaytime = di['stoptime'] + 1.08 * di['stopdistance'] / speed
di['train'].AutoSpeedCTRL(di['stopdistance'],0.0)
if vrmapi.LAYOUT().GetStatusDataInt(di['thatplatform']) == 1:
obj.SetEventAfter(delaytime,100)
else:
di['evid'] = obj.SetEventTimer(1.0,101)
グローバル変数をやめて、ホーム名を共通ステータスにすることで対応できました。赤字の所を逆転させると反対側用のスクリプトになります。
このような感じで駅がたくさんあっても対応できます。また前回は「一線終端駅用」だけでしたが今回は「二線終端駅用」も記載します。
#----------------
#一線終端駅用
#----------------
#OBJID=78
import vrmapi
def vrmevent_78(obj,ev,param):
di = obj.GetDict()
if ev == 'init':
di['train'] = 0
di['stopdistance'] = 600.0 #停車距離
di['stoptime'] = 10.0 #停車時間
di['startdistance'] = 600.0 #出発列車電圧設定距離
di['startvoltage'] = 0.8 #出発列車電圧
elif ev == 'broadcast':
dummy = 1
elif ev == 'timer':
dummy = 1
elif ev == 'time':
dummy = 1
elif ev == 'after':
if param['eventUID'] == 100:
di['train'].Turn()
di['train'].AutoSpeedCTRL(di['startdistance'],di['startvoltage'])
elif ev == 'frame':
dummy = 1
elif ev == 'catch':
f = obj.GetForward()
if f == 1:
di['train'] = obj.GetTrain()
speed = di['train'].GetSpeed()
delaytime = di['stoptime'] + 1.08 * di['stopdistance'] / speed
di['train'].AutoSpeedCTRL(di['stopdistance'],0.0)
obj.SetEventAfter(delaytime,100)
#----------------
#二線終端駅用
#----------------
#OBJID=79
import vrmapi
def vrmevent_79(obj,ev,param):
di = obj.GetDict()
if ev == 'init':
di['train'] = 0
di['thistrain'] = 0
di['thattrain'] = 0
di['evid'] = 0
di['thisplatform'] = "二線終端駅直進" #直進ホーム名
di['thatplatform'] = "二線終端駅分岐" #分岐ホーム名
di['inoutpointid'] = 81 #進入退出ポイントのID
di['stopdistance'] = 800.0 #停車距離
di['stoptime'] = 10.0 #停車時間
di['startdistance'] = 600.0 #出発列車電圧設定距離
di['startvoltage'] = 0.8 #出発列車電圧
di['inoutpoint'] = vrmapi.LAYOUT().GetPoint(di['inoutpointid'])
vrmapi.LAYOUT().SetStatusDataInt(di['thisplatform'], 0)
vrmapi.LAYOUT().SetStatusDataInt(di['thatplatform'], 0)
elif ev == 'broadcast':
dummy = 1
elif ev == 'timer':
dummy = 1
elif ev == 'time':
dummy = 1
elif ev == 'after':
if param['eventUID'] == 100:
di['thattrain'].Turn()
di['thattrain'].AutoSpeedCTRL(di['startdistance'],di['startvoltage'])
di['inoutpoint'].SetBranch(1)
vrmapi.LAYOUT().SetStatusDataInt(di['thatplatform'], 0)
if param['eventUID'] == 101:
di['thistrain'].Turn()
di['thistrain'].AutoSpeedCTRL(di['startdistance'],di['startvoltage'])
di['inoutpoint'].SetBranch(0)
vrmapi.LAYOUT().SetStatusDataInt(di['thisplatform'], 0)
elif ev == 'frame':
dummy = 1
elif ev == 'catch':
if obj.GetForward() == 1:
if vrmapi.LAYOUT().GetStatusDataInt(di['thisplatform']) == 0:
di['inoutpoint'].SetBranch(0)
vrmapi.LAYOUT().SetStatusDataInt(di['thisplatform'], 1)
di['train'] = obj.GetTrain()
di['thistrain'] = di['train']
speed = di['thistrain'].GetSpeed()
delaytime = di['stoptime'] + 1.08 * di['stopdistance'] / speed
di['thistrain'].AutoSpeedCTRL(di['stopdistance'],0.0)
if vrmapi.LAYOUT().GetStatusDataInt(di['thatplatform']) == 1:
obj.SetEventAfter(delaytime,100)
elif vrmapi.LAYOUT().GetStatusDataInt(di['thisplatform']) == 1:
di['inoutpoint'].SetBranch(1)
vrmapi.LAYOUT().SetStatusDataInt(di['thatplatform'], 1)
di['train'] = obj.GetTrain()
di['thattrain'] = di['train']
speed = di['thattrain'].GetSpeed()
delaytime = di['stoptime'] + 1.08 * di['stopdistance'] / speed
di['thattrain'].AutoSpeedCTRL(di['stopdistance'],0.0)
if vrmapi.LAYOUT().GetStatusDataInt(di['thisplatform']) == 1:
obj.SetEventAfter(delaytime,101)
動画でもどうぞ(^^)/
鉄道模型シミュレーターNX単線5駅で5編成が走る