/* Trusted environment controller. No screenshot/model hooks and no gameplay help. It tightens the stock Micro Park deadline to two in-game years, records evidence, and blocks cheats, reloading and pausing after the clock starts. */ registerPlugin({name:"micropark-controller",version:"1.0.0",authors:["Task environment"],type:"local",licence:"MIT",targetApiVersion:77,main:function(){ var ORIGIN="micropark-v1-2yr-001",TARGET=1000000,DEADLINE_MONTHS=16; // The supervisor prepends the try it launched this engine for; a plugin cannot // read a file, and a stand-alone copy of this controller is simply try 1 of 1. var TRY=typeof T37_TRY!=="undefined"?T37_TRY:1,TRIES=typeof T37_TRIES!=="undefined"?T37_TRIES:1; var store=context.getParkStorage("micropark-v1"); var initialized=false,started=0,lastTime=0,elapsed=0,panel=null; var lastDay=-1,lastMonth=-1,monthOpeningNet=0,dayMinCash=Infinity,dayMinRating=999,dayTicks=0; var peakValue=0,peakMonths=0,days=[],outcome=null,crashes=0,blocked=0,finalSaved=false; var incomes=["park_entrance_tickets","park_ride_tickets","shop_sales","food_drink_sales"]; var expenses=["ride_runningcosts","shop_stock","food_drink_stock","wages","marketing","research","interest"]; function emit(x){x.schema=1;x.elapsed=Math.round(elapsed);x.parkOrigin=store.get("origin","");console.log("T37JSON "+JSON.stringify(x));} function amount(types,i){return types.reduce(function(a,t){return a+(park.getMonthlyExpenditure(t)[i]||0);},0);} function net(i){return amount(incomes,i)+amount(expenses,i);} function rides(){return map.rides.filter(function(r){return r.classification==="ride";});} function shops(){return map.rides.filter(function(r){return r.classification!=="ride";});} function guests(){return map.getAllEntities("guest").filter(function(g){return g.isInPark;});} function note(text){park.postMessage({type:"blank",text:text});emit({kind:"notice",text:text});} function money(v){return context.formatString("{CURRENCY}",v);} function snapshot(kind){ var rs=rides(),ss=shops(),gs=guests(); return {kind:kind,elapsed:Math.round(elapsed), scenarioStatus:scenario.status,objectiveYear:scenario.objective.year,objectiveTarget:scenario.objective.parkValue, completedCompanyValue:scenario.completedCompanyValue, parkValue:park.value,peakParkValue:peakValue,companyValue:park.companyValue, cash:park.cash,loan:park.bankLoan,maxLoan:park.maxBankLoan, rating:park.rating,guests:park.guests,parkOpen:park.getFlag("open"), monthsElapsed:date.monthsElapsed,monthsRemaining:Math.max(0,DEADLINE_MONTHS-date.monthsElapsed), day:date.day,month:date.month,year:date.year,ticks:date.ticksElapsed, netMonth:net(0),incomeMonth:amount(incomes,0),expensesMonth:amount(expenses,0), totalCustomers:rs.concat(ss).reduce(function(a,r){return a+r.totalCustomers;},0), openRides:rs.filter(function(r){return r.status==="open";}).length, openShops:ss.filter(function(r){return r.status==="open";}).length, crashes:crashes,blockedActions:blocked, rides:rs.map(function(r){return {id:r.id,name:r.name,type:r.type,status:r.status,breakdown:r.breakdown,excitement:r.excitement,intensity:r.intensity,totalCustomers:r.totalCustomers,totalProfit:r.totalProfit,value:r.value};}), shops:ss.map(function(r){return {id:r.id,name:r.name,type:r.type,status:r.status,totalCustomers:r.totalCustomers};})}; } function recordOutcome(){ if(outcome)return; if(scenario.status==="completed")outcome={result:"completed",monthsElapsed:date.monthsElapsed,day:date.day,month:date.month,year:date.year,parkValue:park.value,completedCompanyValue:scenario.completedCompanyValue}; else if(scenario.status==="failed")outcome={result:"failed",monthsElapsed:date.monthsElapsed,day:date.day,month:date.month,year:date.year,parkValue:park.value}; else return; store.set("outcome",outcome); emit({kind:outcome.result==="completed"?"objective_complete":"objective_failed",outcome:outcome,state:snapshot("outcome_state")}); var again=TRIES>1&&TRY1?"Micro Park objective - try "+TRY+" of "+TRIES:"Micro Park objective",x:890,y:42,width:380,height:139,widgets:[ {type:"label",name:"value",x:10,y:23,width:360,height:16,text:""}, {type:"label",name:"clock",x:10,y:42,width:360,height:16,text:""}, {type:"label",name:"money",x:10,y:61,width:360,height:16,text:""}, {type:"label",name:"park",x:10,y:80,width:360,height:16,text:""}, {type:"label",name:"status",x:10,y:101,width:360,height:23,text:""}],onClose:function(){panel=null;}});} panel.findWidget("value").text="Park value "+money(s.parkValue)+" / "+money(TARGET)+" (peak "+money(s.peakParkValue)+")"; panel.findWidget("clock").text="Year "+s.year+", month "+(s.month+1)+" of 8; "+s.monthsRemaining+" month(s) left of 16"; panel.findWidget("money").text="Cash "+money(s.cash)+"; loan "+money(s.loan)+" of "+money(s.maxLoan); panel.findWidget("park").text="Guests "+s.guests+"; rating "+s.rating+"; open rides "+s.openRides+"; shops "+s.openShops; panel.findWidget("status").text=s.scenarioStatus==="completed"?"OBJECTIVE COMPLETE - save your park" :s.scenarioStatus==="failed"?"OBJECTIVE FAILED - deadline passed" :started?"Running. Reach "+money(TARGET)+" park value before the deadline.":"Unpause to start the two-year clock."; } function initialize(){ initialized=true; // The shipped scenario allows three years; this task is a two-year run. scenario.objective.year=2; store.set("initialized",true);store.set("origin",ORIGIN); store.set("baselineRideIds",map.rides.map(function(r){return r.id;})); peakValue=park.value; note("HANDOVER: Micro Park, try "+TRY+" of "+TRIES+". Reach a park value of "+money(TARGET)+" within two in-game years (16 months). Cash "+money(park.cash)+", loan "+money(park.bankLoan)+" of "+money(park.maxBankLoan)+"."); note("OPERATIONS: The clock starts when you unpause, and pausing is then disabled. Fast-forward stays available. Park entry is free in this scenario: charge for rides."); if(TRIES>1)note("TRIES: This is try "+TRY+" of "+TRIES+". Whichever try scores best is the one that counts."); emit({kind:"micropark_initialized",target:TARGET,deadlineMonths:DEADLINE_MONTHS,tries:TRIES,baseline:snapshot("baseline")}); lastDay=date.day;lastMonth=date.monthsElapsed;monthOpeningNet=net(0); dayMinCash=park.cash;dayMinRating=park.rating;dayTicks=0; updatePanel(); context.paused=true; } context.subscribe("interval.tick",function(){ if(!initialized){ if(store.get("initialized",false)){ initialized=true;elapsed=store.get("elapsed",0);peakValue=store.get("peakValue",0); outcome=store.get("outcome",null);started=Date.now();lastTime=started; lastDay=date.day;lastMonth=date.monthsElapsed;monthOpeningNet=net(0); dayMinCash=park.cash;dayMinRating=park.rating; emit({kind:"micropark_reloaded",state:snapshot("reload_state")}); }else initialize(); return; } if(!started){started=Date.now();lastTime=started;emit({kind:"clock_started",state:snapshot("start_state")});note("Two-year clock started. Pause is disabled for this task.");} if(park.value>peakValue){peakValue=park.value;peakMonths=date.monthsElapsed;store.set("peakValue",peakValue);} dayMinCash=Math.min(dayMinCash,park.cash);dayMinRating=Math.min(dayMinRating,park.rating);dayTicks++; if(date.day!==lastDay||date.monthsElapsed!==lastMonth){ var closing=net(date.monthsElapsed===lastMonth?0:1); var record={kind:"micropark_day",full:dayTicks>=500,monthsElapsed:lastMonth, operatingNet:closing-monthOpeningNet,minCash:dayMinCash,minRating:dayMinRating, parkValue:park.value,peakParkValue:peakValue,guests:park.guests,cash:park.cash, totalCustomers:snapshot("day_customers").totalCustomers}; days.push(record);if(days.length>64)days.shift(); store.set("days",days);emit(record); lastDay=date.day;lastMonth=date.monthsElapsed;monthOpeningNet=net(0); dayMinCash=park.cash;dayMinRating=park.rating;dayTicks=0; } recordOutcome(); }); context.setInterval(function(){ if(!initialized)return; if(started){var now=Date.now();elapsed+=(now-lastTime)/1000;lastTime=now;store.set("elapsed",elapsed);} recordOutcome();updatePanel(); var state=snapshot("micropark_state");state.outcome=outcome;state.days=days.length;emit(state); },5000); context.subscribe("vehicle.crash",function(e){crashes++;emit({kind:"crash",vehicle:e.id,into:e.crashIntoType});}); context.subscribe("map.save",function(){recordOutcome();var s=snapshot("micropark_save");s.outcome=outcome;emit(s);}); context.subscribe("action.query",function(e){ var forbidden=["cheatset","parksetdate","ridefreezerating","scenariosetsetting","tilemodify","loadsave"]; if(forbidden.indexOf(e.action)>=0||(e.action==="pausetoggle"&&started)){ e.result.error=1;e.result.errorTitle="Task rules"; e.result.errorMessage="This task runs continuously; cheats, reloading and pausing are disabled."; blocked++;emit({kind:"blocked_action",action:e.action}); } }); context.subscribe("action.execute",function(e){emit({kind:"action",action:e.action,args:e.args,result:e.result});}); }});