var savedXML;
var PreSelectingCarNum;
function ggbOnInit() {
ggbApplet.registerClickListener("clickFunc");
}
function mCs(){
var preCellNum = ggbApplet.getValue('preCellNum');
var cellNum = ggbApplet.getValue('CellNum');
for (var i = 1; i <= preCellNum; i++) {
ggbApplet.deleteObject('cell'+i);
}
deleteExistingObj('START');
deleteExistingObj('GOAL');
var copycars = getExistingObjIndex('copycar');
for(var k = 0; k < copycars.length; k++){
ggbApplet.unregisterObjectUpdateListener('copycar'+copycars[k]);
}
deleteExistingObj('copycar');
ggbApplet.evalCommand('Execute[Sequence[\"cell\"+t+\"=Element[CellList,\"+t+\"]\",t,1,CellNum]]');
setExistingObjLayer('cell', 1);
}
function getExistingObjIndex(name){
var allObjNames = ggbApplet.getAllObjectNames();
var targets =allObjNames.filter(function(item){
if(item.indexOf(name)!=-1) return true;
});
targets.map( function(value, index, array){
array[index] = eval(value.substr(name.length));
});
return targets;
}
function deleteExistingObj(name){
var targets = getExistingObjIndex(name);
for(var k = 0; k < targets.length; k++){
ggbApplet.deleteObject(name+targets[k]);
}
}
function setExistingObjVisible(name, bool){
var targets = getExistingObjIndex(name);
for(var k = 0; k < targets.length; k++){
ggbApplet.setVisible(name+targets[k], bool);
}
}
function setExistingObjFixed(name, fixbool, selectbool){
var targets = getExistingObjIndex(name);
for(var k = 0; k < targets.length; k++){
ggbApplet.setFixed(name+targets[k], fixbool, selectbool);
}
}
function setExistingObjLayer(name, layer){
var targets = getExistingObjIndex(name);
for(var k = 0; k < targets.length; k++){
ggbApplet.setLayer(name+targets[k], layer);
}
}
function getObjList(name, listName){
var allObjNames = ggbApplet.getAllObjectNames();
var targets =allObjNames.filter(function(item){
if(item.indexOf(name)!=-1) return true;
});
var contents = targets.toString();
ggbApplet.evalCommand(listName+' = {'+contents+'}');
}
function setJavaScript(objName, handler, script){
var isexist = ggbApplet.exists(objName);
if(isexist){
var objType = ggbApplet.getObjectType(objName);
var onwhat = (handler=='click') ? 'val' : 'onUpdate';
var xmlText = '<element type=\"'+objType+'\" label=\"'+objName+'\"><javascript '+onwhat+'=\"'+script+'\"/></element>'
ggbApplet.evalXML(xmlText);
}
}
function setImgCorner(objName, expC1,expC2,expC4){
var isexist = ggbApplet.exists(objName);
if(isexist){
var xmlText = '<element type=\"image\" label=\"'+objName+'\"><startPoint number=\"0\" exp=\"'+expC1+'\"/><startPoint number=\"1\" exp=\"'+expC2+'\"/><startPoint number=\"2\" exp=\"'+expC4+'\"/></element>';
ggbApplet.evalXML(xmlText);
}
}
function setColorByColornum(objName){
var colornum = ggbApplet.getValue('colornum');
var code = ggbApplet.getColor('button'+colornum);
var red = parseInt(code.substring(1,3), 16);
var green = parseInt(code.substring(3,5), 16);
var blue = parseInt(code.substring(5,7), 16);
ggbApplet.setColor(objName,red,green,blue);
}
function isRedExists(){
var segs = getExistingObjIndex('seg');
if(segs.length==0){ggbApplet.evalCommand('SetValue[isRedExists,false]');return -1;}
for(var k = 0; k < segs.length; k++){
var color = ggbApplet.getColor('seg'+segs[k]);
if(color == '#B51A1F'){
ggbApplet.evalCommand('SetValue[isRedExists,true]');
return k;
}
else{
ggbApplet.evalCommand('SetValue[isRedExists,false]');
}
}
return -1;
}
function clickFunc(objName){
var segs = getExistingObjIndex('seg');
var carnum = (Math.max.apply(null, segs)==-Infinity)?1:Math.max.apply(null, segs)+1;
var clickedObjData = ggbApplet.getObjectType(objName);
ggbApplet.setTextValue('clickedobjtype',clickedObjData);
ggbApplet.setTextValue('clickedobjname',objName);
var edit = ggbApplet.getValue('edit');
var isCell = objName.indexOf('cell') != -1;
var makingCar = ggbApplet.getValue('makingCar');
if(edit && isCell){
ggbApplet.evalCommand('SetValue[makingCar,¬makingCar]');
if(!makingCar){
ggbApplet.evalCommand('START'+carnum+'=CopyFreeObject[Centroid['+objName+']]');
ggbApplet.setVisible('START'+carnum, false);
ggbApplet.evalCommand('SetValue[alert,false]');
}
else{
ggbApplet.evalCommand('GOAL'+carnum+'=CopyFreeObject[Centroid['+objName+']]');
ggbApplet.setVisible('GOAL'+carnum, false);
ggbApplet.evalCommand('seg'+carnum+'=Segment[START'+carnum+',GOAL'+carnum+']');
setColorByColornum('seg'+carnum);
ggbApplet.setLayer('seg'+carnum, 1);
ggbApplet.setFixed('seg'+carnum, false,false);
ggbApplet.evalCommand('copycar'+carnum+'=CopyFreeObject[master'+ggbApplet.getValue('colornum')+']');
ggbApplet.setLayer('copycar'+carnum, 5);
ggbApplet.setFixed('copycar'+carnum, true,true);
setImgCorner('copycar'+carnum, 'CopyFreeObject[Translate[Dilate[GOAL'+carnum+', (0.4 + Distance[GOAL'+carnum+', START'+carnum+']) / Distance[GOAL'+carnum+', START'+carnum+'], START'+carnum+'], Vector[0.4UnitVector[PerpendicularVector[Vector[START'+carnum+', GOAL'+carnum+']]]]]]', 'CopyFreeObject[Translate[Dilate[START'+carnum+', (0.4 + Distance[GOAL'+carnum+', START'+carnum+']) / Distance[GOAL'+carnum+', START'+carnum+'], GOAL'+carnum+'], Vector[-0.4UnitVector[PerpendicularVector[Vector[GOAL'+carnum+', START'+carnum+']]]]]]', 'CopyFreeObject[Translate[Dilate[GOAL'+carnum+', (0.4 + Distance[GOAL'+carnum+', START'+carnum+']) / Distance[GOAL'+carnum+', START'+carnum+'], START'+carnum+'], Vector[-0.4UnitVector[PerpendicularVector[Vector[START'+carnum+', GOAL'+carnum+']]]]]]');
if(ggbApplet.getValue('x(GOAL'+carnum+')==x(START'+carnum+') && y(GOAL'+carnum+')==y(START'+carnum+') || x(GOAL'+carnum+')!=x(START'+carnum+') && y(GOAL'+carnum+')!=y(START'+carnum+')')){
ggbApplet.deleteObject('START'+carnum);
ggbApplet.deleteObject('GOAL'+carnum);
ggbApplet.setFixed('copycar'+carnum, false,true);
ggbApplet.deleteObject('copycar'+carnum);
carnum--;
}
else{
for(var k = 0; k < segs.length; k++){
ggbApplet.evalCommand('If[IsDefined[Intersect[seg'+carnum+',seg'+segs[k]+']]||START'+carnum+' == ClosestPoint[seg'+segs[k]+', START'+carnum+']||GOAL'+carnum+' == ClosestPoint[seg'+segs[k]+', GOAL'+carnum+']||START'+segs[k]+' == ClosestPoint[seg'+carnum+', START'+segs[k]+']||GOAL'+segs[k]+' == ClosestPoint[seg'+carnum+', GOAL'+segs[k]+'],Execute[{\"Delete[START'+segs[k]+']\",\"Delete[GOAL'+segs[k]+']\",\"SetFixed[copycar'+segs[k]+',false,true]\",\"Delete[copycar'+segs[k]+']\"}]]');
}
if(segs+1!=getExistingObjIndex('seg').length){carnum--;}
}
isRedExists();
if(isRedExists()!=-1 && ggbApplet.getValue('colornum')==1){ggbApplet.evalCommand('SetValue[colornum,2]');}
if(ggbApplet.getValue('edit')){savedXML = ggbApplet.getXML();}
}
}
var isSPoint = clickedObjData == 'point' && objName.indexOf('START') != -1;
var isGPoint = clickedObjData == 'point' && objName.indexOf('GOAL') != -1;
var isseg = objName.indexOf('seg') != -1;
var iscopycar = objName.indexOf('copycar') != -1;
var makingCar =ggbApplet.getValue('makingCar');
if(edit && isSPoint && !makingCar){
ggbApplet.deleteObject('START'+objName.substr(5));
ggbApplet.deleteObject('GOAL'+objName.substr(5));
ggbApplet.evalCommand('SetValue[makingCar,false]');
}
if(edit && isGPoint && !makingCar){
ggbApplet.deleteObject('START'+objName.substr(4));
ggbApplet.deleteObject('GOAL'+objName.substr(4));
ggbApplet.evalCommand('SetValue[makingCar,false]');
}
if(edit && isseg && !makingCar){
ggbApplet.deleteObject('START'+objName.substr(3));
ggbApplet.deleteObject('GOAL'+objName.substr(3));
ggbApplet.deleteObject('seg'+objName.substr(3));
}
if(edit && iscopycar && !makingCar){
ggbApplet.deleteObject('START'+objName.substr(7));
ggbApplet.deleteObject('GOAL'+objName.substr(7));
ggbApplet.deleteObject('seg'+objName.substr(7));
ggbApplet.deleteObject('copycar'+objName.substr(7));
}
isRedExists();
}
function removeSTART(){
var STARTs = getExistingObjIndex('START');
var segs = getExistingObjIndex('seg');
for(var k=0; k < STARTs.length; k++){
if(segs.indexOf(STARTs[k]) == -1){
ggbApplet.deleteObject('START'+STARTs[k]);
}
}
}
function makeRanges(){
deleteExistingObj('listO');
deleteExistingObj('listA');
deleteExistingObj('listB');
deleteExistingObj('SEGBeforeDilate');
deleteExistingObj('SEGAfterDilate');
deleteExistingObj('range');
if(!ggbApplet.getValue('edit') && isRedExists()!=-1){
var segs = getExistingObjIndex('seg');
for(var k=0; k < segs.length; k++){
var listO = (k==isRedExists()) ? 'CopyFreeObject[Zip[ClosestPoint[s,START'+segs[k]+'],s,Join[cars,wallsforred]]]' : 'CopyFreeObject[Zip[ClosestPoint[s,START'+segs[k]+'],s,Join[cars,walls]]]';
ggbApplet.evalCommand('listO'+segs[k]+'='+listO);
var listA = 'CopyFreeObject[RemoveUndefined[Zip[If[t ≟ ClosestPoint[Ray[START'+segs[k]+',GOAL'+segs[k]+'], t ]&&t!=START'+segs[k]+', t,?],t,listO'+segs[k]+']]]';
ggbApplet.evalCommand('listA'+segs[k]+'='+listA);
var listB = 'CopyFreeObject[RemoveUndefined[Zip[If[t ≟ ClosestPoint[Ray[GOAL'+segs[k]+',START'+segs[k]+'], t ]&&t!=START'+segs[k]+', t,?],t,listO'+segs[k]+']]]';
ggbApplet.evalCommand('listB'+segs[k]+'='+listB);
var segBeforeDilate = 'CopyFreeObject[Segment[ClosestPoint[listA'+segs[k]+', GOAL'+segs[k]+'],ClosestPoint[listB'+segs[k]+', START'+segs[k]+']]]';
ggbApplet.evalCommand('SEGBeforeDilate'+segs[k]+'='+segBeforeDilate);
var segAfterDilate = 'CopyFreeObject[Dilate[SEGBeforeDilate'+segs[k]+',(Length[SEGBeforeDilate'+segs[k]+'] - 2 - Distance[START'+segs[k]+',GOAL'+segs[k]+']) / Length[SEGBeforeDilate'+segs[k]+'],Midpoint[SEGBeforeDilate'+segs[k]+']]]';
ggbApplet.evalCommand('SEGAfterDilate'+segs[k]+'='+segAfterDilate);
var rangePoints = 'CopyFreeObject[Sequence[Point[SEGAfterDilate'+segs[k]+',k/(If[Length[SEGAfterDilate'+segs[k]+']==0,1,Length[SEGAfterDilate'+segs[k]+']])],k,0,Length[SEGAfterDilate'+segs[k]+']]]';
ggbApplet.evalCommand('range'+segs[k]+'='+rangePoints);
}
getObjList('range', 'Ranges');
setExistingObjVisible('listO', false);
setExistingObjVisible('listA', false);
setExistingObjVisible('listB', false);
setExistingObjVisible('SEGBeforeDilate', false);
setExistingObjVisible('SEGAfterDilate', false);
setExistingObjVisible('range', false);
ggbApplet.setVisible('Ranges', false);
}
}
function makeCENTERs(){
deleteExistingObj('CENTER');
deleteExistingObj('LISTENER');
ggbApplet.deleteObject('CenterList');
ggbApplet.deleteObject('ListenerList');
ggbApplet.deleteObject('CompareCenterListener');
ggbApplet.deleteObject('precount');
ggbApplet.deleteObject('count');
ggbApplet.deleteObject('totalcount');
ggbApplet.deleteObject('countText');
var copycars = getExistingObjIndex('copycar');
if(getExistingObjIndex('ORIGIN')!=0){
for(var k=0; k < copycars.length; k++){
ggbApplet.evalCommand('SetValue[copycar'+copycars[k]+',Translate[copycar'+copycars[k]+',Vector[Midpoint[Corner[copycar'+copycars[k]+',1],Corner[copycar'+copycars[k]+',3]],ORIGIN'+copycars[k]+']]]');
ggbApplet.setFixed('copycar'+copycars[k], true,true);
setJavaScript('copycar'+copycars[k], 'update', ' ');
ggbApplet.evalCommand('SetValue[START'+copycars[k]+',Dilate[Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]], (-0.4 + Distance[Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]], Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]]]) / Distance[Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]], Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]]], Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]]]]');
ggbApplet.evalCommand('SetValue[GOAL'+copycars[k]+',Dilate[Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]], (-0.4 + Distance[Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]], Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]]]) / Distance[Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]], Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]]], Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]]]]');
}
}
deleteExistingObj('ORIGIN');
if(!ggbApplet.getValue('edit') && isRedExists()!=-1){
var segs = getExistingObjIndex('seg');
for(var k=0; k < segs.length; k++){
ggbApplet.evalCommand('CENTER'+segs[k]+'=Point[range'+segs[k]+']');
ggbApplet.evalCommand('SetValue[CENTER'+segs[k]+',Midpoint[seg'+segs[k]+']]');
ggbApplet.evalCommand('LISTENER'+segs[k]+'=CopyFreeObject[CENTER'+segs[k]+']');
ggbApplet.evalCommand('ORIGIN'+segs[k]+'=CopyFreeObject[CENTER'+segs[k]+']');
}
setExistingObjVisible('CENTER',false);
setExistingObjVisible('LISTENER',false);
setExistingObjVisible('ORIGIN',false);
getObjList('CENTER', 'CenterList');
getObjList('LISTENER', 'ListenerList');
ggbApplet.evalCommand('CompareCenterListener=Zip[s ≠ t, s, CenterList, t, ListenerList]');
ggbApplet.evalCommand('precount=Sum[Zip[If[s, 1, 0], s, CompareCenterListener]]');
ggbApplet.evalCommand('count=0');
ggbApplet.evalCommand('totalcount=count+precount');
ggbApplet.evalCommand('countText=Text[totalcount+\"手\",(0.5,scale+1)]');
}
}
function updateRange(k){
var copycars = getExistingObjIndex('copycar');
var excNum = copycars.indexOf(k);
copycars.splice(excNum, 1);
var isRangeKExist = ggbApplet.exists('range'+k);
if(isRangeKExist && PreSelectingCarNum != k){
var listO = (excNum==isRedExists()) ? 'CopyFreeObject[Zip[ClosestPoint[s,START'+k+'],s,Join[cars,wallsforred]]]' : 'CopyFreeObject[Zip[ClosestPoint[s,START'+k+'],s,Join[cars,walls]]]';
ggbApplet.evalCommand('SetValue[listO'+k+','+listO+']');
var listA = 'CopyFreeObject[RemoveUndefined[Zip[If[t ≟ ClosestPoint[Ray[START'+k+',GOAL'+k+'], t ]&&t!=START'+k+', t,?],t,listO'+k+']]]';
ggbApplet.evalCommand('SetValue[listA'+k+','+listA+']');
var listB = 'CopyFreeObject[RemoveUndefined[Zip[If[t ≟ ClosestPoint[Ray[GOAL'+k+',START'+k+'], t ]&&t!=START'+k+', t,?],t,listO'+k+']]]';
ggbApplet.evalCommand('SetValue[listB'+k+','+listB+']');
var segBeforeDilate = 'CopyFreeObject[Segment[ClosestPoint[listA'+k+', GOAL'+k+'],ClosestPoint[listB'+k+', START'+k+']]]';
ggbApplet.evalCommand('SEGBeforeDilate'+k+'='+segBeforeDilate);
var segAfterDilate = 'CopyFreeObject[Dilate[SEGBeforeDilate'+k+',(Length[SEGBeforeDilate'+k+'] - 2 - Distance[START'+k+',GOAL'+k+']) / Length[SEGBeforeDilate'+k+'],Midpoint[SEGBeforeDilate'+k+']]]';
ggbApplet.evalCommand('SEGAfterDilate'+k+'='+segAfterDilate);
var rangePoints = 'CopyFreeObject[Sequence[Point[SEGAfterDilate'+k+',k/(If[Length[SEGAfterDilate'+k+']==0,1,Length[SEGAfterDilate'+k+']])],k,0,Length[SEGAfterDilate'+k+']]]';
ggbApplet.evalCommand('SetValue[range'+k+','+rangePoints+']');
PreSelectingCarNum = k;
}
for(var k=0; k < copycars.length; k++){
if(isRangeKExist){
var scriptText1 = 'SetValue[CENTER'+copycars[k]+',Midpoint[Corner[copycar'+copycars[k]+',1],Corner[copycar'+copycars[k]+',3]]]';
var scriptText2 = 'If[LISTENER'+copycars[k]+'!=CENTER'+copycars[k]+',SetValue[count,count+1]]'
var scriptText3 = 'SetValue[LISTENER'+copycars[k]+',CENTER'+copycars[k]+']';
ggbApplet.setTextValue('scriptText1',scriptText1);
ggbApplet.setTextValue('scriptText2',scriptText2);
ggbApplet.setTextValue('scriptText3',scriptText3);
ggbApplet.evalCommand('Execute[{scriptText1,scriptText2,scriptText3}]');
}
}
}
function getCopycarReady(){
var red = isRedExists();
if(!ggbApplet.getValue('edit') && red==-1){
ggbApplet.evalCommand('SetValue[alert,true]');
ggbApplet.evalCommand('SetValue[edit,true]');
ggbApplet.evalCommand('RunUpdateScript[edit]');
return;
}
var copycars = getExistingObjIndex('copycar');
if(!ggbApplet.getValue('edit')){
ggbApplet.setTextValue('redCarName', 'copycar'+copycars[red]);
ggbApplet.evalCommand('isEnd = If[x(CENTER'+copycars[red]+') > x(goalMidPoint),true,false]');
ggbApplet.evalCommand('SetConditionToShowObject[prizeText, ¬edit&&isEnd]');
}
else{
ggbApplet.evalCommand('SetConditionToShowObject[prizeText, ¬edit]');
}
if(!ggbApplet.getValue('edit')){
for(var k=0; k < copycars.length; k++){
ggbApplet.setFixed('copycar'+copycars[k], false, true);
var script1 = 'SetValue[CENTER'+copycars[k]+',Midpoint[Corner[copycar'+copycars[k]+',1],Corner[copycar'+copycars[k]+',3]]]';
var script2 = 'SetValue[copycar'+copycars[k]+',Translate[copycar'+copycars[k]+',Vector[Midpoint[Corner[copycar'+copycars[k]+',1],Corner[copycar'+copycars[k]+',3]],CENTER'+copycars[k]+']]]';
var script3 = 'SetValue[START'+copycars[k]+',Dilate[Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]], (-0.4 + Distance[Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]], Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]]]) / Distance[Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]], Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]]], Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]]]]';
var script4 = 'SetValue[GOAL'+copycars[k]+',Dilate[Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]], (-0.4 + Distance[Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]], Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]]]) / Distance[Midpoint[Corner[copycar'+copycars[k]+', 1], Corner[copycar'+copycars[k]+', 4]], Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]]], Midpoint[Corner[copycar'+copycars[k]+', 2], Corner[copycar'+copycars[k]+', 3]]]]';
var script5 = 'updateRange('+copycars[k]+')';
var scriptToSet = 'ggbApplet.evalCommand(\''+script1+'\'); \n ggbApplet.evalCommand(\''+script2+'\'); \n ggbApplet.evalCommand(\''+script3+'\'); \n ggbApplet.evalCommand(\''+script4+'\'); \n '+script5+';';
setJavaScript('copycar'+copycars[k], 'update', scriptToSet);
}
}
}