File size: 10,194 Bytes
5463b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* 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&&TRY<TRIES&&outcome.result!=="completed";
  note(outcome.result==="completed"
   ?"OBJECTIVE COMPLETE on try "+TRY+": park value reached "+money(TARGET)+" inside two years. Save your park now; the run is over."
   :"OBJECTIVE FAILED on try "+TRY+" of "+TRIES+": the two-year deadline passed below "+money(TARGET)+". Save your park for review."
    +(again?" Try "+(TRY+1)+" starts shortly on a fresh empty plot.":" No tries remain."));
  // Trusted proof of how this try ended. The engine's finest autosave interval
  // is five minutes, which can miss the moment the objective settled, and this
  // try's park is thrown away as soon as the next try starts. Saving here is
  // last, so the map.save it raises finds the outcome already recorded.
  if(!finalSaved){
   finalSaved=true;
   try{context.saveGame({filename:"t37-try-"+TRY+"-final"});}
   catch(e){emit({kind:"final_save_failed",message:String(e)});}
  }
 }

 function updatePanel(){
  if(typeof ui==="undefined")return;
  var s=snapshot("panel_state");
  if(!panel){panel=ui.openWindow({classification:"micropark-progress",title:TRIES>1?"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});});
}});