UNPKG

agentscript

Version:

AgentScript Model in Model/View architecture

1 lines 134 kB
function t(t){return new Promise(((e,r)=>{const s=new Image;s.crossOrigin="Anonymous",s.onload=()=>e(s),s.onerror=()=>r(`Could not load image ${t}`),s.src=t}))}function e(t){const{width:e,height:r}=t,s=new OffscreenCanvas(e,r).getContext("2d");return s.drawImage(t,0,0),s}async function r(t,e="blob"){const r=["arrayBuffer","blob","json","text"];if(!r.includes(e))throw Error("fetchData: data must be one of "+r.toString());return fetch(t).then((t=>t[e]()))}function s(t,e="download.png",r=null){!e.endsWith(".png")&&e.endsWith(".jpeg");const s=e.endsWith(".png")?"image/png":"image/jpeg",i="string"===Pt(t)?t:t.toDataURL(s,r),n=document.createElement("a");n.download=e,n.href=i,n.click()}function i(t=1e3){return new Promise((e=>{setTimeout(e,t)}))}function n(){return E()}function o(t,e,r=n()){if(r)return new OffscreenCanvas(t,e);const s=document.createElement("canvas");return s.width=t,s.height=e,s}function a(t,e,r=n(),s={}){return o(t,e,r).getContext("2d",s)}function h(t,e=n()){const r=a(t.width,t.height,e);return r.drawImage(t,0,0),r.canvas}function c(t,e,r){const s=h(t.canvas);t.canvas.width=e,t.canvas.height=r,t.drawImage(s,0,0)}function l(t,e,r){t.width===e&&t.height==r||(t.width=e,t.height=r)}function u(t){t.save(),t.setTransform(1,0,0,1,0,0)}function d(t,e,r="center",s="middle"){Object.assign(t,{font:e,textAlign:r,textBaseline:s})}function m(t,e,r,s,i,n=!0){n&&u(t),t.fillStyle=i.css||i,t.fillText(e,r,s),n&&t.restore()}function p(t){return t.getImageData(0,0,t.canvas.width,t.canvas.height)}function g(t){const e=p(t).data,r=[];return pt(e.length,4,(t=>r.push(e.subarray(t,t+4)))),r}function f(t,e){const{width:r,height:s}=t.canvas;u(t),e&&"transparent"!==e?(e=e.css||e,t.fillStyle=e,t.fillRect(0,0,r,s)):t.clearRect(0,0,r,s),t.restore()}function y(t){const e=a(t.width,t.height);return x(e,t),e}function x(t,e){u(t),t.drawImage(e,0,0,t.canvas.width,t.canvas.height),t.restore()}let w=!1;function b(t,e="number",r="Function"){if(!w&&typeof t!==e)throw new Error(`${r} expected a ${e}, got ${t}`)}const M=new Set;function v(t,e=!1){M.has(t)||(e?console.warn(t):console.log(t),M.add(t))}function _(t){v(t,!0)}function z(t){var e=document.createElement("style");e.innerHTML=t,document.head.appendChild(e)}function C(){return window.location.search.substr(1)}function S(t=C()){const e={},r=new URLSearchParams(t);for(var s of r.entries()){let[t,r]=s;(r.match(/^[0-9.]+$/)||r.match(/^[0-9.]+e[0-9]+$/))&&(r=Number(r)),["true","t",""].includes(r)&&(r=!0),["false","f"].includes(r)&&(r=!1),e[t]=r}return e}function E(){return!A()&&void 0===self.window}function A(){return"undefined"!=typeof global}const P=Math.PI;function O(t){return Math.floor(Math.random()*t)}function k(t,e){return t+Math.floor(Math.random()*(e-t))}function I(t){return Math.random()*t}function T(t,e){return t+Math.random()*(e-t)}function R(t,e=4){if(-0===t)return 0;if(Array.isArray(t))return t.map((t=>R(t,e)));const r=10**e;return Math.round(t*r)/r}const j=t=>0==(t&t-1);function X(t,e){return(t%e+e)%e}const Y=(t,e,r)=>e+X(t-e,r-e);function F(t,e,r){return t<e?e:t>r?r:t}const D=(t,e,r)=>e<=t&&t<=r,N=(t,e,r)=>t<=e?t+(e-t)*r:t-(t-e)*r;function L(t,e,r){if(e===r)throw Error("lerpScale: lo === hi");return((t=F(t,e,r))-e)/(r-e)}const $=180/Math.PI,B=Math.PI/180;function U(t){return Z(t*B)}function W(t){return q(t*$)}function V(t){return q(90-t*$)}function q(t){return X(t,360)}function Z(t){return X(t,2*P)}function G(t,e){return q(t)===q(e)}const H=G;function J(t,e){let r=Z(t-e);return r>P&&(r-=2*P),r}function Q(t,e){let r=q(t-e);return r>180&&(r-=360),r}function K(t,e){return-Q(t,e)}function tt(t,e,r,s){return Math.atan2(s-e,r-t)}const et=(t,e,r,s)=>(t-r)**2+(e-s)**2,rt=(t,e,r,s)=>Math.sqrt(et(t,e,r,s)),st=(t,e,r,s,i,n)=>(t-s)**2+(e-i)**2+(r-n)**2,it=(t,e,r,s,i,n)=>Math.sqrt(st(t,e,r,s,i,n));function nt(t,e,r,s,i,n,o){if(et(n,o,t,e)>r*r)return!1;const a=tt(n,o,t,e);return s/2>=Math.abs(J(i,a))}function ot(t,e=0,r=!0){let s=r;const i=["rectCache"];return JSON.stringify(t,((t,e)=>{if(i.includes(t))return;return Array.isArray(e)&&e.length>0&&Number.isInteger(e[0].id)&&!s?e.map((t=>t.id)):(s=!1,e)}),e)}const at=t=>t,ht=t=>e=>e[t];function ct(t,e){if(t.length!==e.length)return!1;for(let r=0;r<t.length;r++)if(t[r]!==e[r])return!1;return!0}function lt(t,e){const r=t.indexOf(e);if(-1===r)throw Error(`removeArrayItem: ${e} not in array`);return t.splice(r,1),t}const ut=t=>t.map((t=>`${t}`)).join(",");function dt(t,e){if(t.slice)for(let r=0,s=t.length;r<s;r++)e(t[r],r,t);else Object.keys(t).forEach((r=>e(t[r],r,t)))}function mt(t,e,r=[]){for(let s=0;s<t;s++)e(s,r);return r}function pt(t,e,r){for(let s=0;s<t;s+=e)r(s)}function gt(t,e){const r=t.constructor;if(r===Array)return t.concat(Lt(e,Array));const s=new r(t.length+e.length);return s.set(t),s.set(e,t.length),s}function ft(t){return t[O(t.length)]}function yt(t,e){if(t.length<2)throw Error("otherOneOf: array.length < 2");do{var r=ft(t)}while(e===r);return r}const xt=t=>ft(Object.keys(t)),wt=t=>t[xt(t)];function bt(t,e,r=!0){"string"==typeof e&&(e=ht(e));const s=(t,r)=>e(t)-e(r);return t.sort(((t,e)=>r?s(t,e):-s(t,e)))}function Mt(t){for(let e=t.length-1;e>0;e--){const r=O(e);[t[r],t[e]]=[t[e],t[r]]}return t}function vt(t,e,r){if(r<=1)throw Error("floatRamp: numItems must be > 1");const s=[];for(let i=0;i<r;i++)s.push(t+i/(r-1)*(e-t));return s}function _t(t,e,r=e-t+1){return vt(t,e,r).map((t=>Math.round(t)))}const zt=t=>t.reduce(((t,e)=>Math.max(t,e))),Ct=t=>t.reduce(((t,e)=>Math.min(t,e)));function St(t){return!!Tt(t)&&Object.values(t).every((t=>Dt(t)))}function Et(t,e,r){const s={};return r.forEach((r=>{s[r]=t[r][e]})),s}function At(t,e=Object.keys(t)){const r=t[e[0]].length,s=new Array(r);return dt(s,((r,i)=>{s[i]=Et(t,i,e)})),s}const Pt=t=>({}.toString.call(t).match(/\s(\w+)/)[1].toLowerCase()),Ot=(t,e)=>Pt(t)===e,kt=(t,e)=>e.includes(Pt(t)),It=t=>Ot(t,"string"),Tt=t=>Ot(t,"object"),Rt=t=>Array.isArray(t),jt=t=>Ot(t,"number"),Xt=t=>Number.isInteger(t),Yt=t=>kt(t,["htmlcanvaselement","offscreencanvas"]),Ft=t=>kt(t,["image","htmlimageelement","htmlcanvaselement","offscreencanvas","imagebitmap"]),Dt=t=>"arraybuffer"===Pt(t.buffer),Nt=t=>Rt(t)||Dt(t);function Lt(t,e){return t.constructor===e?t:e.from(t)}function $t(t){return"object"===Pt(t)&&null!=t.width&&null!=t.height}var Bt=Object.freeze({__proto__:null,imagePromise:t,imageToImageBitmap:async function(t){return createImageBitmap(t,{premultiplyAlpha:"none",colorSpaceConversion:"none"})},imageBitmapRendererCtx:async function(t){const{width:e,height:r}=t,s=new OffscreenCanvas(e,r).getContext("bitmaprenderer");return s.transferFromImageBitmap(t),s},imageBitmap2dCtx:e,imageBitmapData:async function(t){const r=e(t);return r.getImageData(0,0,r.canvas.width,r.canvas.height)},canvasToBlob:async function(t,e="png",r){return e.startsWith("image/")||(e="image/"+e),new Promise((s=>{t.toBlob((t=>s(t)),e,r)}))},AsyncFunction:function(t,e){return new(0,Object.getPrototypeOf((async function(){})).constructor)(...t,e)},blobToData:async function(t,e="dataURL"){e=e[0].toUpperCase()+e.slice(1);const r=["Text","ArrayBuffer","DataURL"];if(!r.includes(e))throw Error("blobToData: data must be one of "+r.toString());const s=new FileReader;return new Promise(((r,i)=>{s.addEventListener("load",(()=>r(s.result))),s.addEventListener("error",(t=>i(t))),s["readAs"+e](t)}))},fetchData:r,fetchJson:async function(t){return r(t,"json")},fetchText:async function(t){return r(t,"text")},toDataURL:function(t,e){return t.toDataURL?t.toDataURL(e,e):(e||(e="text/plain;charset=US-ASCII"),`data:${e};base64,${btoa(t)}}`)},blobsEqual:async function(t,e){return await t.text()===await e.text()},downloadCanvas:s,downloadBlob:function(t,e="download",r=!0){$t(t)&&!Array.isArray(t.data)&&(t.data=Array.from(t.data)),Dt(t)&&(t=Array.from(t)),(Tt(t)||Rt(t))&&(t=r?JSON.stringify(t,null,2):JSON.stringify(t));let s="blob"===Pt(t)?t:new Blob([t]);const i=URL.createObjectURL(s),n=document.createElement("a");n.download=e,n.href=i,n.click(),URL.revokeObjectURL(i)},xhrPromise:function(t,e="text",r="GET"){return new Promise(((s,i)=>{const n=new XMLHttpRequest;n.open(r,t),n.responseType=e,n.onload=()=>s(n.response),n.onerror=()=>i(Error(`Could not load ${t}: ${n.status}`)),n.send()}))},pause:i,timeoutLoop:async function(t,e=-1,r=0){let s=0;for(;s++!==e;)t(s-1),await i(r)},waitPromise:function(t,e=10){return new Promise((r=>{!function s(){if(t())return r();setTimeout(s,e)}()}))},createCanvas:o,createCtx:a,cloneCanvas:h,resizeCtx:c,resizeCanvas:function(t,e,r=e/t.width*t.height){const s=a(e,r);return s.drawImage(t,0,0,e,r),s.canvas},setCanvasSize:l,setIdentity:u,setTextProperties:d,drawText:m,ctxImageData:p,ctxImageColors:g,ctxImagePixels:function(t){const e=p(t);return new Uint32Array(e.data.buffer)},clearCtx:f,imageToCtx:y,imageToCanvas:function(t){return y(t).canvas},fillCtxWithImage:x,setCtxImage:function(t,e){l(t.canvas,e.width,e.height),x(t,e)},skipErrorChecks:function(t){w=t},checkArg:b,checkArgs:function(t,e="number",r="Function"){w||("arguments"===Pt(t)&&(t=Array.from(t)),t.forEach(((t,s)=>{b(t,e,r)})))},logOnce:v,warn:_,timeit:function(t,e=1e5,r="test"){r=r+"-"+e,console.time(r);for(let r=0;r<e;r++)t(r);console.timeEnd(r)},fps:function(){const t="undefined"==typeof performance?Date:performance,e=t.now();let r=0;function s(){r++;const i=t.now()-e,n=parseFloat((r/(i/1e3)).toFixed(2));Object.assign(s,{fps:n,ms:i,start:e,steps:r})}return s.steps=0,s},pps:function(t,e=""){e&&console.log(e);let r=1,s="";for(;t;){if("function"==typeof t)s=t.constructor.toString();else{const e=Object.keys(t);s=e.length>0?`[${e.join(", ")}]`:`[${t.constructor.name}]`}console.log(`[${r++}]: ${s}`),t=Object.getPrototypeOf(t)}},toWindow:function(t){Object.assign(window,t),console.log("toWindow:",Object.keys(t).join(", "))},logAll:function(t){Object.keys(t).forEach((e=>console.log(" ",e,t[e])))},dump:function(t=window.model){let{patches:e,turtles:r,links:s}=t;Object.assign(window,{ps:e,ts:r,ls:s}),window.p=e.length>0?e.oneOf():{},window.t=r.length>0?r.oneOf():{},window.l=s.length>0?s.oneOf():{},console.log("debug: ps, ts, ls, p, t, l dumped to window")},addCssLink:function(t){const e=document.createElement("link");e.setAttribute("rel","stylesheet"),e.setAttribute("href",t),document.head.appendChild(e)},fetchCssStyle:async function(t){const e=await fetch(t);if(!e.ok)throw Error(`fetchCssStyle: Not found: ${t}`);const r=await e.text();return z(r),r},addCssStyle:z,getQueryString:C,parseQueryString:S,RESTapi:function(t){return Object.assign(t,S())},inWorker:E,inNode:A,inDeno:function(){return!!Deno},printToPage:function(t,e=document.body){"object"==typeof t&&(t=JSON.stringify(t,null,2)),t="<pre>"+t+"</pre>","string"==typeof e&&(e=document.getElementById(e)),e.style.fontFamily="monospace",e.innerHTML+=t},getEventXY:function(t,e){const r=t.getBoundingClientRect();return[e.clientX-r.left,e.clientY-r.top]},fcnToWorker:function(t){const e=document.location.href.replace(/\/[^\/]+$/,"/"),r=`(${t.toString(e)})("${e}")`,s=URL.createObjectURL(new Blob([r],{type:"text/javascript"})),i=new Worker(s);return i.onerror=function(t){console.log("Worker ERROR: Line ",t.lineno,": ",t.message)},i},PI:P,randomInt:O,randomInt2:k,randomFloat:I,randomFloat2:T,randomCentered:function(t){return T(-t/2,t/2)},randomNormal:function(t=0,e=1){const[r,s]=[1-Math.random(),Math.random()];return Math.sqrt(-2*Math.log(r))*Math.cos(2*P*s)*e+t},randomSeed:function(t=123456){t%=2147483647,Math.random=()=>((t=16807*t%2147483647)-1)/2147483646},precision:R,isPowerOf2:j,nextPowerOf2:t=>Math.pow(2,Math.ceil(Math.log2(t))),mod:X,wrap:Y,clamp:F,isBetween:D,lerp:N,lerpScale:L,toDeg:$,toRad:B,degToRad:U,radToDeg:W,radToHeading:V,headingToRad:function(t){return q(90-t)*B},radToHeadingAngle:function(t){return-W(t)},headingAngleToRad:function(t){return-U(t)},degToHeading:t=>q(90-t),headingToDeg:t=>q(90-t),mod360:q,mod2pi:Z,modpipi:function(t){return X(t,2*P)-P},mod180180:function(t){return q(t)-180},degreesEqual:G,radsEqual:function(t,e){return Z(t)===Z(e)},headingsEq:H,subtractRadians:J,subtractDegrees:Q,subtractHeadings:K,radiansTowardXY:tt,headingTowardXY:function(t,e,r,s){return V(tt(t,e,r,s))},degreesTowardXY:function(t,e,r,s){return W(tt(t,e,r,s))},sqDistance:et,distance:rt,sqDistance3:st,distance3:it,inCone:nt,toJSON:ot,sampleModel:function(t){const e=ot({ticks:t.ticks,model:Object.keys(t),patches:t.patches.length,patch:t.patches.oneOf(),turtles:t.turtles.length,turtle:t.turtles.oneOf(),links:t.links.length,link:t.links.oneOf()});return JSON.parse(e)},identityFcn:at,noopFcn:()=>{},propFcn:ht,arraysEqual:ct,removeArrayItem:lt,arraysToString:ut,forLoop:dt,repeat:mt,step:pt,range:function(t){return mt(t,((t,e)=>{e[t]=t}))},override:function(t,e){const r=t;return dt(t,((t,s)=>{e[s]&&(r[s]=e[s])})),r},concatArrays:gt,objectToString:function(t,e=2,r=!0){let s=JSON.stringify(t,null,e);return r&&(s=s.replace(/"([^"]+)":/gm,"$1:")),s},objectsEqual:(t,e)=>JSON.stringify(t)===JSON.stringify(e),oneOf:ft,otherOneOf:yt,oneKeyOf:xt,oneValOf:wt,sortNums:function(t,e=!0){return t.sort(((t,r)=>e?t-r:r-t))},sortObjs:bt,shuffle:Mt,union:function(t,e){return Array.from(new Set(t.concat(e)))},intersection:function(t,e){const r=new Set(e);return t.filter((t=>r.has(t)))},difference:function(t,e){const r=new Set(e);return t.filter((t=>!r.has(t)))},floatRamp:vt,integerRamp:_t,nestedProperty:function(t,e){switch("string"==typeof e&&(e=e.split(".")),e.length){case 1:return t[e[0]];case 2:return t[e[0]][e[1]];case 3:return t[e[0]][e[1]][e[2]];case 4:return t[e[0]][e[1]][e[2]][e[3]];default:return e.reduce(((t,e)=>t[e]),t)}},arrayLast:t=>t[t.length-1],arrayMax:zt,arrayMin:Ct,arrayExtent:t=>[Ct(t),zt(t)],arraysDiff:(t,e,r=(t=>t))=>{if(t.length!==e.length)return console.log("lengths differ",t.length,e.length);const s=[];for(let i=0;i<t.length;i++)t[i]!==e[i]&&s.push([r(i),t[i],e[i]]);return s},arrayToMatrix:function(t,e,r){if(t.length!==e*r)throw Error("arrayToMatrix: length !== width * height");const s=[];for(let i=0;i<r;i++){const r=t.slice(i*e,(i+1)*e);s.push(r)}return s},matrixToArray:t=>t.flat(),isOofA:St,toOofA:function(t,e){const r=t.length,s=Object.keys(e),i={};return s.forEach((t=>{i[t]=new e[t](r)})),dt(t,((t,e)=>{s.forEach((r=>i[r][e]=t[r]))})),i},oofaObject:Et,toAofO:At,oofaBuffers:function(t){const e=[];return dt(t,(t=>dt(t,(t=>e.push(t.buffer))))),e},typeOf:Pt,isType:Ot,isOneOfTypes:kt,isString:It,isObject:Tt,isArray:Rt,isNumber:jt,isInteger:Xt,isFunction:t=>Ot(t,"function"),isImage:t=>Ot(t,"image"),isCanvas:Yt,isImageable:Ft,isTypedArray:Dt,isUintArray:t=>/^uint.*array$/.test(Pt(t)),isIntArray:t=>/^int.*array$/.test(Pt(t)),isFloatArray:t=>/^float.*array$/.test(Pt(t)),isArrayLike:Nt,isColorLikeArray:t=>Nt(t)&&[3,4].includes(t.length)&&t.every((t=>Xt(t)&&D(t,0,255)||jt(t)&&D(t,0,1))),isLittleEndian:function(){const t=new Uint32Array([16909060]);return 4===new Uint8ClampedArray(t.buffer)[0]},convertArrayType:Lt,isDataSet:$t});async function Ut(e){const r=Pt(e);switch(r){case"string":e=await t(e);case"htmlimageelement":return y(e);case"htmlcanvaselement":case"offscreencanvas":return e.getContext("2d");case"canvasrenderingcontext2d":return e;default:throw Error("toContext: bad img type: "+r)}}const Wt=[{shift:5,msgMask:7,dataMask:248},{shift:3,msgMask:3,dataMask:252},{shift:0,msgMask:7,dataMask:248}];function Vt(t){for(let e=3;e<t.length;e+=4)if(254===t[e])return(e-3)/4;throw Error(`decode: no message terminator in image data, length = ${t.length}`)}var qt=Object.freeze({__proto__:null,stegMsgSize:Vt,encode:async function(t,e){const r=await Ut(t),{width:s,height:i}=r.canvas;!function(t,e,r){const s=e*r;if(s<t.length)throw Error(`encode: image size < msg.length: ${s} ${t.length}`)}(e,s,i);const n=function(t){const e=Pt(t);switch(e){case"number":t=String.fromCharCode(t);case"string":return(new TextEncoder).encode(t);case"uint8array":case"uint8clampedarray":return t;default:throw Error("toUint8Array: bad msg type: "+e)}}(e);console.log("msg buffer",n);const o=r.getImageData(0,0,s,i),a=o.data;let h;return console.log("imgageData.data",a),n.forEach(((t,e)=>{const[r,s,i]=function(t){return[t>>Wt[0].shift,t>>Wt[1].shift&Wt[1].msgMask,t&Wt[2].msgMask]}(t);h=4*e,a[h]=(a[h++]&Wt[0].dataMask)+r,a[h]=(a[h++]&Wt[1].dataMask)+s,a[h]=(a[h++]&Wt[2].dataMask)+i,a[h]=255})),a[h+4]=254,console.log("encoded imgageData.data",a),r.putImageData(o,0,0),console.log("msg length",e.length),console.log("encode: embedded msg size",Vt(a)),r},decode:async function(t,e=!1){const r=await Ut(t),{width:s,height:i}=r.canvas,n=r.getImageData(0,0,s,i).data,o=Vt(n);console.log("decode: embedded msg size",o);const a=new Uint8Array(o);return a.forEach(((t,e)=>{let r=4*e;const s=(Wt[0].msgMask&n[r++])<<Wt[0].shift,i=(Wt[1].msgMask&n[r++])<<Wt[1].shift,o=(Wt[2].msgMask&n[r++])<<Wt[2].shift;a[e]=s+i+o})),console.log("decode msgArray",a),e?a:(new TextDecoder).decode(a)}}),Zt={centimeters:637100880,centimetres:637100880,degrees:6371008.8/111325,feet:20902260.511392,inches:6371008.8*39.37,kilometers:6371.0088,kilometres:6371.0088,meters:6371008.8,metres:6371008.8,miles:3958.761333810546,millimeters:6371008800,millimetres:6371008800,nauticalmiles:6371008.8/1852,radians:1,yards:6371008.8*1.0936},Gt={acres:247105e-9,centimeters:1e4,centimetres:1e4,feet:10.763910417,hectares:1e-4,inches:1550.003100006,kilometers:1e-6,kilometres:1e-6,meters:1,metres:1,miles:386e-9,millimeters:1e6,millimetres:1e6,yards:1.195990046};function Ht(t,e,r){void 0===r&&(r={});var s={type:"Feature"};return(0===r.id||r.id)&&(s.id=r.id),r.bbox&&(s.bbox=r.bbox),s.properties=e||{},s.geometry=t,s}function Jt(t,e,r){if(void 0===r&&(r={}),!t)throw new Error("coordinates is required");if(!Array.isArray(t))throw new Error("coordinates must be an Array");if(t.length<2)throw new Error("coordinates must be at least 2 numbers long");if(!oe(t[0])||!oe(t[1]))throw new Error("coordinates must contain numbers");return Ht({type:"Point",coordinates:t},e,r)}function Qt(t,e,r){void 0===r&&(r={});for(var s=0,i=t;s<i.length;s++){var n=i[s];if(n.length<4)throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");for(var o=0;o<n[n.length-1].length;o++)if(n[n.length-1][o]!==n[0][o])throw new Error("First and last Position are not equivalent.")}return Ht({type:"Polygon",coordinates:t},e,r)}function Kt(t,e,r){if(void 0===r&&(r={}),t.length<2)throw new Error("coordinates must be an array of two or more positions");return Ht({type:"LineString",coordinates:t},e,r)}function te(t,e){void 0===e&&(e={});var r={type:"FeatureCollection"};return e.id&&(r.id=e.id),e.bbox&&(r.bbox=e.bbox),r.features=t,r}function ee(t,e,r){return void 0===r&&(r={}),Ht({type:"MultiLineString",coordinates:t},e,r)}function re(t,e,r){return void 0===r&&(r={}),Ht({type:"MultiPoint",coordinates:t},e,r)}function se(t,e,r){return void 0===r&&(r={}),Ht({type:"MultiPolygon",coordinates:t},e,r)}function ie(t,e){void 0===e&&(e="kilometers");var r=Zt[e];if(!r)throw new Error(e+" units is invalid");return t*r}function ne(t,e){void 0===e&&(e="kilometers");var r=Zt[e];if(!r)throw new Error(e+" units is invalid");return t/r}function oe(t){return!isNaN(t)&&null!==t&&!Array.isArray(t)}function ae(t){return!!t&&t.constructor===Object}function he(t,e,r){if(null!==t)for(var s,i,n,o,a,h,c,l,u=0,d=0,m=t.type,p="FeatureCollection"===m,g="Feature"===m,f=p?t.features.length:1,y=0;y<f;y++){a=(l=!!(c=p?t.features[y].geometry:g?t.geometry:t)&&"GeometryCollection"===c.type)?c.geometries.length:1;for(var x=0;x<a;x++){var w=0,b=0;if(null!==(o=l?c.geometries[x]:c)){h=o.coordinates;var M=o.type;switch(u=!r||"Polygon"!==M&&"MultiPolygon"!==M?0:1,M){case null:break;case"Point":if(!1===e(h,d,y,w,b))return!1;d++,w++;break;case"LineString":case"MultiPoint":for(s=0;s<h.length;s++){if(!1===e(h[s],d,y,w,b))return!1;d++,"MultiPoint"===M&&w++}"LineString"===M&&w++;break;case"Polygon":case"MultiLineString":for(s=0;s<h.length;s++){for(i=0;i<h[s].length-u;i++){if(!1===e(h[s][i],d,y,w,b))return!1;d++}"MultiLineString"===M&&w++,"Polygon"===M&&b++}"Polygon"===M&&w++;break;case"MultiPolygon":for(s=0;s<h.length;s++){for(b=0,i=0;i<h[s].length;i++){for(n=0;n<h[s][i].length-u;n++){if(!1===e(h[s][i][n],d,y,w,b))return!1;d++}b++}w++}break;case"GeometryCollection":for(s=0;s<o.geometries.length;s++)if(!1===he(o.geometries[s],e,r))return!1;break;default:throw new Error("Unknown Geometry Type")}}}}}function ce(t,e){var r;switch(t.type){case"FeatureCollection":for(r=0;r<t.features.length&&!1!==e(t.features[r].properties,r);r++);break;case"Feature":e(t.properties,0)}}function le(t,e){if("Feature"===t.type)e(t,0);else if("FeatureCollection"===t.type)for(var r=0;r<t.features.length&&!1!==e(t.features[r],r);r++);}function ue(t,e){var r,s,i,n,o,a,h,c,l,u,d=0,m="FeatureCollection"===t.type,p="Feature"===t.type,g=m?t.features.length:1;for(r=0;r<g;r++){for(a=m?t.features[r].geometry:p?t.geometry:t,c=m?t.features[r].properties:p?t.properties:{},l=m?t.features[r].bbox:p?t.bbox:void 0,u=m?t.features[r].id:p?t.id:void 0,o=(h=!!a&&"GeometryCollection"===a.type)?a.geometries.length:1,i=0;i<o;i++)if(null!==(n=h?a.geometries[i]:a))switch(n.type){case"Point":case"LineString":case"MultiPoint":case"Polygon":case"MultiLineString":case"MultiPolygon":if(!1===e(n,d,c,l,u))return!1;break;case"GeometryCollection":for(s=0;s<n.geometries.length;s++)if(!1===e(n.geometries[s],d,c,l,u))return!1;break;default:throw new Error("Unknown Geometry Type")}else if(!1===e(null,d,c,l,u))return!1;d++}}function de(t,e){ue(t,(function(t,r,s,i,n){var o,a=null===t?null:t.type;switch(a){case null:case"Point":case"LineString":case"Polygon":return!1!==e(Ht(t,s,{bbox:i,id:n}),r,0)&&void 0}switch(a){case"MultiPoint":o="Point";break;case"MultiLineString":o="LineString";break;case"MultiPolygon":o="Polygon"}for(var h=0;h<t.coordinates.length;h++){var c=t.coordinates[h];if(!1===e(Ht({type:o,coordinates:c},s),r,h))return!1}}))}function me(t,e){de(t,(function(t,r,s){var i=0;if(t.geometry){var n=t.geometry.type;if("Point"!==n&&"MultiPoint"!==n){var o,a=0,h=0,c=0;return!1!==he(t,(function(n,l,u,d,m){if(void 0===o||r>a||d>h||m>c)return o=n,a=r,h=d,c=m,void(i=0);var p=Kt([o,n],t.properties);if(!1===e(p,r,s,m,i))return!1;i++,o=n}))&&void 0}}}))}function pe(t,e){if(!t)throw new Error("geojson is required");de(t,(function(t,r,s){if(null!==t.geometry){var i=t.geometry.type,n=t.geometry.coordinates;switch(i){case"LineString":if(!1===e(t,r,s,0,0))return!1;break;case"Polygon":for(var o=0;o<n.length;o++)if(!1===e(Kt(n[o],t.properties),r,s,o))return!1}}}))}function ge(t){var e=[1/0,1/0,-1/0,-1/0];return he(t,(function(t){e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]<t[0]&&(e[2]=t[0]),e[3]<t[1]&&(e[3]=t[1])})),e}function fe(t){if(!t)throw new Error("coord is required");if(!Array.isArray(t)){if("Feature"===t.type&&null!==t.geometry&&"Point"===t.geometry.type)return t.geometry.coordinates;if("Point"===t.type)return t.coordinates}if(Array.isArray(t)&&t.length>=2&&!Array.isArray(t[0])&&!Array.isArray(t[1]))return t;throw new Error("coord must be GeoJSON Point or an Array of numbers")}function ye(t){return"Feature"===t.type?t.geometry:t}function xe(t,e,r){var s=!1;e[0][0]===e[e.length-1][0]&&e[0][1]===e[e.length-1][1]&&(e=e.slice(0,e.length-1));for(var i=0,n=e.length-1;i<e.length;n=i++){var o=e[i][0],a=e[i][1],h=e[n][0],c=e[n][1];if(t[1]*(o-h)+a*(h-t[0])+c*(t[0]-o)==0&&(o-t[0])*(h-t[0])<=0&&(a-t[1])*(c-t[1])<=0)return!r;a>t[1]!=c>t[1]&&t[0]<(h-o)*(t[1]-a)/(c-a)+o&&(s=!s)}return s}ge.default=ge;var we=Object.freeze({__proto__:null,bbox:ge,bboxPolygon:function(t,e){void 0===e&&(e={});var r=Number(t[0]),s=Number(t[1]),i=Number(t[2]),n=Number(t[3]);if(6===t.length)throw new Error("@turf/bbox-polygon does not support BBox with 6 positions");var o=[r,s];return Qt([[o,[i,s],[i,n],[r,n],o]],e.properties,{bbox:t,id:e.id})},bearingToAzimuth:function(t){var e=t%360;return e<0&&(e+=360),e},booleanPointInPolygon:function(t,e,r){if(void 0===r&&(r={}),!t)throw new Error("point is required");if(!e)throw new Error("polygon is required");var s=fe(t),i=ye(e),n=i.type,o=e.bbox,a=i.coordinates;if(o&&!1===function(t,e){return e[0]<=t[0]&&e[1]<=t[1]&&e[2]>=t[0]&&e[3]>=t[1]}(s,o))return!1;"Polygon"===n&&(a=[a]);for(var h=!1,c=0;c<a.length&&!h;c++)if(xe(s,a[c][0],r.ignoreBoundary)){for(var l=!1,u=1;u<a[c].length&&!l;)xe(s,a[c][u],!r.ignoreBoundary)&&(l=!0),u++;l||(h=!0)}return h},collectionOf:function(t,e,r){if(!t)throw new Error("No featureCollection passed");if(!r)throw new Error(".collectionOf() requires a name");if(!t||"FeatureCollection"!==t.type)throw new Error("Invalid input to "+r+", FeatureCollection required");for(var s=0,i=t.features;s<i.length;s++){var n=i[s];if(!n||"Feature"!==n.type||!n.geometry)throw new Error("Invalid input to "+r+", Feature with geometry required");if(!n.geometry||n.geometry.type!==e)throw new Error("Invalid input to "+r+": must be a "+e+", given "+n.geometry.type)}},containsNumber:function t(e){if(e.length>1&&oe(e[0])&&oe(e[1]))return!0;if(Array.isArray(e[0])&&e[0].length)return t(e[0]);throw new Error("coordinates must only contain numbers")},convertArea:function(t,e,r){if(void 0===e&&(e="meters"),void 0===r&&(r="kilometers"),!(t>=0))throw new Error("area must be a positive number");var s=Gt[e];if(!s)throw new Error("invalid original units");var i=Gt[r];if(!i)throw new Error("invalid final units");return t/s*i},convertLength:function(t,e,r){if(void 0===e&&(e="kilometers"),void 0===r&&(r="kilometers"),!(t>=0))throw new Error("length must be a positive number");return ie(ne(t,e),r)},coordAll:function(t){var e=[];return he(t,(function(t){e.push(t)})),e},coordEach:he,coordReduce:function(t,e,r,s){var i=r;return he(t,(function(t,s,n,o,a){i=0===s&&void 0===r?t:e(i,t,s,n,o,a)}),s),i},feature:Ht,featureCollection:te,featureEach:le,featureOf:function(t,e,r){if(!t)throw new Error("No feature passed");if(!r)throw new Error(".featureOf() requires a name");if(!t||"Feature"!==t.type||!t.geometry)throw new Error("Invalid input to "+r+", Feature with geometry required");if(!t.geometry||t.geometry.type!==e)throw new Error("Invalid input to "+r+": must be a "+e+", given "+t.geometry.type)},featureReduce:function(t,e,r){var s=r;return le(t,(function(t,i){s=0===i&&void 0===r?t:e(s,t,i)})),s},findPoint:function(t,e){if(!ae(e=e||{}))throw new Error("options is invalid");var r,s=e.featureIndex||0,i=e.multiFeatureIndex||0,n=e.geometryIndex||0,o=e.coordIndex||0,a=e.properties;switch(t.type){case"FeatureCollection":s<0&&(s=t.features.length+s),a=a||t.features[s].properties,r=t.features[s].geometry;break;case"Feature":a=a||t.properties,r=t.geometry;break;case"Point":case"MultiPoint":return null;case"LineString":case"Polygon":case"MultiLineString":case"MultiPolygon":r=t;break;default:throw new Error("geojson is invalid")}if(null===r)return null;var h=r.coordinates;switch(r.type){case"Point":return Jt(h,a,e);case"MultiPoint":return i<0&&(i=h.length+i),Jt(h[i],a,e);case"LineString":return o<0&&(o=h.length+o),Jt(h[o],a,e);case"Polygon":return n<0&&(n=h.length+n),o<0&&(o=h[n].length+o),Jt(h[n][o],a,e);case"MultiLineString":return i<0&&(i=h.length+i),o<0&&(o=h[i].length+o),Jt(h[i][o],a,e);case"MultiPolygon":return i<0&&(i=h.length+i),n<0&&(n=h[i].length+n),o<0&&(o=h[i][n].length-o),Jt(h[i][n][o],a,e)}throw new Error("geojson is invalid")},findSegment:function(t,e){if(!ae(e=e||{}))throw new Error("options is invalid");var r,s=e.featureIndex||0,i=e.multiFeatureIndex||0,n=e.geometryIndex||0,o=e.segmentIndex||0,a=e.properties;switch(t.type){case"FeatureCollection":s<0&&(s=t.features.length+s),a=a||t.features[s].properties,r=t.features[s].geometry;break;case"Feature":a=a||t.properties,r=t.geometry;break;case"Point":case"MultiPoint":return null;case"LineString":case"Polygon":case"MultiLineString":case"MultiPolygon":r=t;break;default:throw new Error("geojson is invalid")}if(null===r)return null;var h=r.coordinates;switch(r.type){case"Point":case"MultiPoint":return null;case"LineString":return o<0&&(o=h.length+o-1),Kt([h[o],h[o+1]],a,e);case"Polygon":return n<0&&(n=h.length+n),o<0&&(o=h[n].length+o-1),Kt([h[n][o],h[n][o+1]],a,e);case"MultiLineString":return i<0&&(i=h.length+i),o<0&&(o=h[i].length+o-1),Kt([h[i][o],h[i][o+1]],a,e);case"MultiPolygon":return i<0&&(i=h.length+i),n<0&&(n=h[i].length+n),o<0&&(o=h[i][n].length-o-1),Kt([h[i][n][o],h[i][n][o+1]],a,e)}throw new Error("geojson is invalid")},flattenEach:de,flattenReduce:function(t,e,r){var s=r;return de(t,(function(t,i,n){s=0===i&&0===n&&void 0===r?t:e(s,t,i,n)})),s},geojsonType:function(t,e,r){if(!e||!r)throw new Error("type and name required");if(!t||t.type!==e)throw new Error("Invalid input to "+r+": must be a "+e+", given "+t.type)},geomEach:ue,geomReduce:function(t,e,r){var s=r;return ue(t,(function(t,i,n,o,a){s=0===i&&void 0===r?t:e(s,t,i,n,o,a)})),s},geometry:function(t,e,r){switch(t){case"Point":return Jt(e).geometry;case"LineString":return Kt(e).geometry;case"Polygon":return Qt(e).geometry;case"MultiPoint":return re(e).geometry;case"MultiLineString":return ee(e).geometry;case"MultiPolygon":return se(e).geometry;default:throw new Error(t+" is invalid")}},geometryCollection:function(t,e,r){return void 0===r&&(r={}),Ht({type:"GeometryCollection",geometries:t},e,r)},getCoord:fe,getCoords:function(t){if(Array.isArray(t))return t;if("Feature"===t.type){if(null!==t.geometry)return t.geometry.coordinates}else if(t.coordinates)return t.coordinates;throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array")},getGeom:ye,getType:function(t,e){return"FeatureCollection"===t.type?"FeatureCollection":"GeometryCollection"===t.type?"GeometryCollection":"Feature"===t.type&&null!==t.geometry?t.geometry.type:t.type},lengthToDegrees:function(t,e){return function(t){return t%(2*Math.PI)*180/Math.PI}(ne(t,e))},lengthToRadians:ne,lineEach:pe,lineReduce:function(t,e,r){var s=r;return pe(t,(function(t,i,n,o){s=0===i&&void 0===r?t:e(s,t,i,n,o)})),s},lineString:Kt,lineStrings:function(t,e,r){return void 0===r&&(r={}),te(t.map((function(t){return Kt(t,e)})),r)},multiLineString:ee,multiPoint:re,multiPolygon:se,point:Jt,points:function(t,e,r){return void 0===r&&(r={}),te(t.map((function(t){return Jt(t,e)})),r)},polygon:Qt,polygons:function(t,e,r){return void 0===r&&(r={}),te(t.map((function(t){return Qt(t,e)})),r)},propEach:ce,propReduce:function(t,e,r){var s=r;return ce(t,(function(t,i){s=0===i&&void 0===r?t:e(s,t,i)})),s},radiansToLength:ie,segmentEach:me,segmentReduce:function(t,e,r){var s=r,i=!1;return me(t,(function(t,n,o,a,h){s=!1===i&&void 0===r?t:e(s,t,n,o,a,h),i=!0})),s}});class be extends Array{static get[Symbol.species](){return be}static fromArray(t){return Object.setPrototypeOf(t,be.prototype)}constructor(...t){super(...t)}toArray(){return Object.setPrototypeOf(this,Array.prototype),this}isEmpty(){return 0===this.length}first(){return this[0]}last(){return this[this.length-1]}atIndex(t){if(0===this.length)return;return this[X(t,this.length)]}all(t){return this.every(t)}props(t,e=be){const r=new e(this.length);for(let e=0;e<this.length;e++)r[e]=this[e][t];return r}typedSample(t){const e={};return dt(t,((t,r)=>{e[r]=this.props(r,t)})),e}uniq(){return be.from(new Set(this))}forLoop(t){for(let e=0,r=this.length;e<r;e++)t(this[e],e,this);return this}ask(t){const e=this.length;for(let r=0;r<Math.min(e,this.length);r++)t(this[r],r,this);if(e!=this.length){_(`AgentArray.ask array mutation: ${this.name||this.constructor.name}: ${this.length<e?"decreasing":"increasing"}`)}}with(t){return this.filter(t)}other(t){return this.filter((e=>e!==t))}getValues(t){const e=new be;return It(t)?this.forLoop((r=>e.push(r[t]))):this.forLoop((r=>e.push(t(r)))),e}count(t){return this.reduce(((e,r)=>e+(t(r)?1:0)),0)}sum(t){return this.reduce(((e,r)=>e+(t?r[t]:r)),0)}avg(t){return this.sum(t)/this.length}min(t){return this.reduce(((e,r)=>Math.min(e,t?r[t]:r)),1/0)}max(t){return this.reduce(((e,r)=>Math.max(e,t?r[t]:r)),-1/0)}extent(t){return[this.min(t),this.max(t)]}histogram(t,e=10,r=this.min(t),s=this.max(t)){const i=(s-r)/e,n=new be(e);return n.fill(0),this.ask((o=>{const a=t?o[t]:o;if(a<r||a>s)_(`histogram bounds error: ${a}: ${r}-${s}`);else{let t=Math.floor((a-r)/i);t===e&&t--,n[t]++}})),n.parameters={key:t,bins:e,min:r,max:s,binSize:i,arraySize:this.length},n}clone(){return this.slice(0)}shuffle(){return Mt(this)}sortBy(t,e=!0){return bt(this,t,e),this}remove(t,e){const r=this.agentIndex(t,e);return-1!==r?this.splice(r,1):_(`remove: ${t} not in AgentArray`),this}insert(t,e){const r=this.sortedIndex(t,e);if(this[r]===t)throw Error("insert: item already in AgentArray");this.splice(r,0,t)}sortedIndex(t,e=at){It(e)&&(e=ht(e));const r=e(t);let s=0,i=this.length;for(;s<i;){const t=s+i>>>1;e(this[t])<r?s=t+1:i=t}return s}agentIndex(t,e){if(!e)return this.indexOf(t);const r=this.sortedIndex(t,e);return this[r]===t?r:-1}contains(t,e){return this.agentIndex(t,e)>=0}oneOf(){return ft(this)}otherOneOf(t){return yt(this,t)}otherNOf(t,e){if(this.length<t)throw Error("AgentArray: otherNOf: length < N");return this.clone().remove(e).shuffle().slice(0,t)}minOrMaxOf(t,e,r=!1){if(this.isEmpty())throw Error("min/max OneOf: empty array");"string"==typeof e&&(e=ht(e));let s=null,i=t?1/0:-1/0;for(let r=0;r<this.length;r++){const n=this[r],o=e(n);(t&&o<i||!t&&o>i)&&([s,i]=[n,o])}return r?[s,i]:s}minOneOf(t){return this.minOrMaxOf(!0,t)}maxOneOf(t){return this.minOrMaxOf(!1,t)}minValOf(t){return this.minOrMaxOf(!0,t,!0)}maxValOf(t){return this.minOrMaxOf(!1,t,!0)}nOf(t){if(t>this.length)throw Error("nOf: n larger than AgentArray");if(t===this.length)return this;const e=new be;for(;e.length<t;){const t=this.oneOf();t in e||e.push(t)}return e}minOrMaxNOf(t,e,r){if(e>this.length)throw Error("min/max nOf: n larger than AgentArray");const s=this.clone().sortBy(r);return t?s.slice(0,e):s.slice(s.length-e)}minNOf(t,e){return this.minOrMaxNOf(!0,t,e)}maxNOf(t,e){return this.minOrMaxNOf(!1,t,e)}}class Me extends be{constructor(t,...e){if(!t)throw Error("AgentList requires model");super(...e),this.model=t}inRect(t,e,r=e,s=!1){const i=new Me(this.model),n=t.x-e,o=t.x+e,a=t.y-r,h=t.y+r;return this.ask((e=>{n<=e.x&&e.x<=o&&a<=e.y&&e.y<=h&&(s||t!==e)&&i.push(e)})),i}inRadius(t,e,r=!1){const s=new Me(this.model),i=e*e,n=et;return this.ask((e=>{n(t.x,t.y,e.x,e.y)<=i&&(r||t!==e)&&s.push(e)})),s}inCone(t,e,r,s,i=!1){s=this.model.toRads(s),r=this.model.toAngleRads(r);const n=new Me(this.model);return this.ask((o=>{nt(o.x,o.y,e,r,s,t.x,t.y)&&(i||t!==o)&&n.push(o)})),n}}class ve extends be{model;name;baseSet;AgentClass;static get[Symbol.species](){return be}constructor(t,e,r,s=null){super(),s=s||this,Object.assign(this,{model:t,name:r,baseSet:s,AgentClass:e}),this.isBaseSet()?(this.breeds={},this.ID=0):(Object.setPrototypeOf(this,Object.getPrototypeOf(s)),this.baseSet.breeds[r]=this),this.ownVariables=[],this.agentProto=new e(this),this.protoMixin(this.agentProto,e)}protoMixin(t,e){Object.assign(t,{agentSet:this,model:this.model}),t[this.baseSet.name]=this.baseSet,e.prototype.setBreed||(Object.assign(e.prototype,{setBreed(t){t.setBreed(this)},getBreed(){return this.agentSet},isBreed(t){return this.agentSet===t}}),Object.defineProperty(e.prototype,"breed",{get:function(){return this.agentSet}}))}newBreed(t){return new ve(this.model,this.AgentClass,t,this)}isBreedSet(){return this.baseSet!==this}isBaseSet(){return this.baseSet===this}withBreed(t){return this.filter((e=>e.agentSet===t))}create(){console.log(`AgentSet: Abstract method called: ${this}`)}addAgent(t){return t=t||Object.create(this.agentProto),this.isBreedSet()?this.baseSet.addAgent(t):(t.id=this.ID++,t.agentConstructor&&t.agentConstructor()),this.push(t),t}clear(){for(;!this.isEmpty();)this.last().die()}removeAgent(t){return-1!=t.id&&(this.isBreedSet()&&this.baseSet.remove(t,"id"),this.remove(t,"id")),this}setDefault(t,e){return this.agentProto[t]=e,this}getDefault(t){return this.agentProto[t]}setBreed(t){if(t.agentSet===this)return;t.agentSet.isBreedSet()&&t.agentSet.remove(t,"id"),this.isBreedSet()&&this.insert(t,"id");const e=t.agentSet.ownVariables;for(const r of e)this.ownVariables.includes(r)||delete t[r];for(const r of this.ownVariables)e.includes(r)||(t[r]=0);return Object.setPrototypeOf(t,this.agentProto)}ask(t){if(0===this.length)return;const e=this.last().id;for(let r=0;r<this.length&&this[r].id<=e;r++)t(this[r],r,this)}askSet(t){0!==this.length&&("patches"===this.name?super.forLoop(t):this.isBaseSet()?this.baseSetAsk(t):this.isBreedSet()&&this.cloneAsk(t))}baseSetAsk(t){if(0===this.length)return;const e=this.last().id;for(let r=0;r<this.length;r++){const s=this[r],i=s.id;if(i>e)break;if(t(s,r,this),r>=this.length)break;if(this[r].id>i)for(;r>=0&&this[r].id>i;)r--}}cloneAsk(t){const e=this.clone();for(let r=0;r<e.length;r++){const s=e[r];s.breed==this&&s.id>0&&t(s,r,e)}}}class _e{width;height;data;static emptyDataSet(t,e,r=Array){return new _e(t,e,new r(t*e))}constructor(t,e,r){if(r.length!==t*e)throw Error(`new DataSet length: ${r.length} !== ${t} * ${e}`);Object.assign(this,{width:t,height:e,data:r})}checkXY(t,e){if(!this.inBounds(t,e))throw Error(`DataSet: x,y out of range: ${t}, ${e}`)}inBounds(t,e){return D(t,0,this.width-1)&&D(e,0,this.height-1)}dataType(){return this.data.constructor}type(){return this.constructor}toIndex(t,e){return t+e*this.width}toXY(t){return[t%this.width,Math.floor(t/this.width)]}getXY(t,e){return this.data[this.toIndex(t,e)]}setXY(t,e,r){this.data[this.toIndex(t,e)]=r}sample(t,e,r=!0){return this.checkXY(t,e),r?this.nearest(t,e):this.bilinear(t,e)}nearest(t,e){return this.getXY(Math.round(t),Math.round(e))}bilinear(t,e){const r=Math.floor(t),s=Math.floor(e),i=this.toIndex(r,s),n=this.width,o=t-r,a=e-s,h=1-o,c=1-a;return this.data[i]*h*c+(this.data[i+1]||0)*o*c+(this.data[i+n]||0)*h*a+(this.data[i+1+n]||0)*o*a}clone(){return new _e(this.width,this.height,this.data.slice(0))}emptyDataSet(t,e,r=this.dataType()){return _e.emptyDataSet(t,e,r)}emptyArray(t){return new(this.type())(t)}resample(t,e,r=!0,s=Array){if(t===this.width&&e===this.height)return this.clone();const i=_e.emptyDataSet(t,e,s);for(let s=0;s<e;s++)for(let n=0;n<t;n++)i.setXY(n,s,this.sample(n*(this.width-1)/(t-1),s*(this.height-1)/(e-1),r));return i}scale(t,e){const r=this.min(),s=(e-t)/(this.max()-r),i=t-s*r;return this.map((t=>s*t+i))}subset(t,e,r,s){if(t+r>this.width||e+s>this.height)throw console.log("subset: x+width",t+r,"this.width",this.width),console.log("subset: y+height",e+s,"this.height",this.height),Error("DataSet.subSet: params out of range");const i=this.emptyDataSet(r,s);for(let n=0;n<r;n++)for(let r=0;r<s;r++)i.setXY(n,r,this.getXY(n+t,r+e));return i}crop(t,e,r,s){if(void 0===e)var{top:t,bottom:e,left:r,right:s}=t;const i=this.width-r-s,n=this.height-t-e;return this.subset(r,t,i,n)}map(t){return new _e(this.width,this.height,this.data.map(t))}col(t){const[e,r,s]=[this.width,this.height,this.data];if(t>=e)throw Error(`col: x out of range width: ${e} x: ${t}`);const i=this.emptyArray(r);for(let n=0;n<r;n++)i[n]=s[t+n*e];return i}row(t){const[e,r]=[this.width,this.height];if(t>=r)throw Error(`row: y out of range height: ${r} x: ${t}`);return this.data.slice(t*e,(t+1)*e)}convertType(t){this.data=Lt(this.data,t)}concatEast(t){const[e,r]=[this.width,this.height],[s,i]=[t.width,t.height];if(r!==i)throw Error(`concatEast: heights not equal ${r}, ${i}`);const n=this.emptyDataSet(e+s,r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)n.setXY(t,e,this.getXY(t,e));for(let r=0;r<s;r++)for(let s=0;s<i;s++)n.setXY(r+e,s,t.getXY(r,s));return n}concatSouth(t){const[e,r,s]=[this.width,this.height,this.data];if(e!==t.width)throw Error(`concatSouth: widths not equal ${e}, ${t.width}`);const i=gt(s,t.data);return new _e(e,r+t.height,i)}transformCoords(t,e,r,s,i,n){return[(t-r)*(this.width-1)/i,(s-e)*(this.height-1)/n]}coordSample(t,e,r,s,i,n,o=!0){const[a,h]=this.transformCoords(t,e,r,s,i,n);return this.sample(a,h,o)}neighborhood(t,e,r=[]){r.length=0;const s=0===t||t===this.width-1||0===e||e===this.height-1;for(let i=-1;i<=1;i++)for(let n=-1;n<=1;n++){let o=t+n,a=e+i;s&&(o=F(o,0,this.width-1),a=F(a,0,this.height-1)),r.push(this.data[this.toIndex(o,a)])}return r}convolve(t,e=1,r=!1){const[s,i,n,o]=r?[1,1,this.height-1,this.width-1]:[0,0,this.height,this.width],a=this.emptyDataSet(o,n),h=a.data;let c=0;for(let r=i;r<n;r++)for(let i=s;i<o;i++){const s=this.neighborhood(i,r);let n=0;for(let e=0;e<t.length;e++)n+=t[e]*s[e];h[c++]=n*e}return a}dzdx(t=2,e=1/8){return this.convolve([-1,0,1,-t,0,t,-1,0,1],e)}dzdy(t=2,e=1/8){return this.convolve([1,t,1,0,0,0,-1,-t,-1],e)}laplace8(){return this.convolve([-1,-1,-1,-1,8,-1,-1,-1,-1])}laplace4(){return this.convolve([0,-1,0,-1,4,-1,0,-1,0])}blur(t=.0625){return this.convolve([1,2,1,2,4,2,1,2,1],t)}edge(){return this.convolve([1,1,1,1,-7,1,1,1,1])}slopeAndAspect(t=1,e=!0){const r=this.dzdx(),s=this.dzdy();let[i,n]=[[],[]];const[o,a]=[r.height,r.width];for(let h=0;h<o;h++)for(let o=0;o<a;o++){const[a,c]=[r.getXY(o,h),s.getXY(o,h)];n.push(Math.atan(rt(0,0,a,c))/t);let l=Math.atan2(-c,-a);e&&l<0&&(l+=2*Math.PI),i.push(l)}return n=new _e(a,o,n),i=new _e(a,o,i),{slope:n,aspect:i,dzdx:r,dzdy:s}}max(){return this.data.reduce(((t,e)=>Math.max(t,e)))}min(){return this.data.reduce(((t,e)=>Math.min(t,e)))}extent(){return[this.min(),this.max()]}sum(){return this.data.reduce(((t,e)=>t+e))}normalize(t=0,e=1,r=!1){const[s,i]=this.extent(),n=1/(i-s);let o=this.data.map((r=>N(t,e,n*(r-s))));return r&&(o=o.map((t=>Math.round(t)))),new _e(this.width,this.height,o)}equals(t){return this.width===t.width&&this.height===t.height&&ct(this.data,t.data)}}class ze{hidden=!1;end0=null;end1=null;width=1;agentSet;model;name;init(t,e){this.end0=t,this.end1=e,t.links.push(this),e.links.push(this)}die(){-1!==this.id&&(this.agentSet.removeAgent(this),lt(this.end0.links,this),lt(this.end1.links,this),this.id=-1)}isDead(){return-1===this.id}bothEnds(){return be.fromArray([this.end0,this.end1])}length(){return this.end0.distance(this.end1)}get heading(){const{x0:t,x1:e,y0:r,y1:s}=this,i=Math.atan2(s-r,e-t);return this.model.fromRads(i)}otherEnd(t){if(t===this.end0)return this.end1;if(t===this.end1)return this.end0;throw Error(`Link.otherEnd: turtle not a link turtle: ${t}`)}distanceXY(t,e){return this.bothEnds().map((r=>r.distanceXY(t,e))).sum()-this.length()}get x0(){return this.end0.x}get y0(){return this.end0.y}get z0(){return this.end0.z?this.end0.z:0}get x1(){return this.end1.x}get y1(){return this.end1.y}get z1(){return this.end1.z?this.end1.z:0}}class Ce extends ve{createOne(t,e,r=(t=>{})){const s=this.addAgent();return s.init(t,e),r(s),s}create(t,e,r=(t=>{})){return Array.isArray(e)||(e=[e]),e.map((e=>this.createOne(t,e,r)))}}class Se{maxX=16;minX=-16;maxY=16;minY=-16;maxZ=16;minZ=-16;static defaultOptions(t=16,e=t,r=Math.max(t,e)){return{minX:-t,maxX:t,minY:-e,maxY:e,minZ:-r,maxZ:r}}static defaultWorld(t=16,e=t,r=t){return new Se(Se.defaultOptions(t,e,r))}constructor(t={}){Object.assign(this,t),this.setWorld()}setWorld(){let{minX:t,maxX:e,minY:r,maxY:s,minZ:i,maxZ:n}=this;dt({minX:t,maxX:e,minY:r,maxY:s,minZ:i,maxZ:n},((t,e)=>{if(!Number.isInteger(t))throw Error(`World: ${e}:${t} must be an integer`)})),this.numX=this.width=e-t+1,this.numY=this.height=s-r+1,this.numZ=this.depth=n-i+1,this.minXcor=t-.5,this.maxXcor=e+.5,this.minYcor=r-.5,this.maxYcor=s+.5,this.minZcor=i-.5,this.maxZcor=n+.5,this.centerX=(t+e)/2,this.centerY=(r+s)/2,this.centerZ=(i+n)/2,this.numPatches=this.numX*this.numY}getOptions(){const{minX:t,minY:e,minZ:r,maxX:s,maxY:i,maxZ:n}=this;return{minX:t,minY:e,minZ:r,maxX:s,maxY:i,maxZ:n}}randomPoint(){return[T(this.minXcor,this.maxXcor),T(this.minYcor,this.maxYcor)]}random3DPoint(){return[T(this.minXcor,this.maxXcor),T(this.minYcor,this.maxYcor),T(this.minZcor,this.maxZcor)]}randomPatchPoint(){return[k(this.minX,this.maxX),k(this.minY,this.maxY)]}isOnWorld(t,e,r=this.centerZ){return this.minXcor<=t&&t<=this.maxXcor&&this.minYcor<=e&&e<=this.maxYcor&&this.minZcor<=r&&r<=this.maxZcor}bboxTransform(t,e,r,s){return new Ee(t,e,r,s,this)}getWorldSize(t=1){return[this.numX*t,this.numY*t]}setEuclideanTransform(t,e){this.setCanvasSize(t.canvas,e),t.restore(),t.save(),t.scale(e,-e),t.translate(-this.minXcor,-this.maxYcor)}patchSize(t){const{numX:e,numY:r}=this,{clientWidth:s,clientHeight:i}=t,n=s/e,o=i/r;if(n!==o)throw Error(`World patchSize: x/y sizes differ ${n}, ${o}`);return n}setCanvasSize(t,e){const[r,s]=this.getWorldSize(e);l(t,r,s)}pixelXYtoPatchXY(t,e,r){return[this.minXcor+t/r,this.maxYcor-e/r]}patchXYtoPixelXY(t,e,r){return[(t-this.minXcor)*r,(this.maxYcor-e)*r]}xyToPatchIndex(t,e){if(!this.isOnWorld(t,e))return;const{minX:r,maxX:s,maxY:i,numX:n,maxXcor:o,maxYcor:a}=this;return(t=t===o?s:Math.round(t))-r+n*(i-(e=e===a?i:Math.round(e)))}}class Ee{constructor(t,e,r,s,i){this.bbox=[t,e,r,s],t<r&&console.log("flipX"),s<e&&console.log("flipY"),t<r&&([t,r]=[r,t]),s<e&&([s,e]=[e,s]);const{maxXcor:n,maxYcor:o,minXcor:a,minYcor:h}=i,c=(t-r)/(n-a),l=(s-e)/(o-h),u=(t+r-c*(n+a))/2,d=(s+e-l*(o+h))/2;Object.assign(this,{mx:c,my:l,bx:u,by:d})}toWorld(t){const{mx:e,my:r,bx:s,by:i}=this,[n,o]=t;return[(n-s)/e,(o-i)/r]}toBBox(t){const{mx:e,my:r,bx:s,by:i}=this,[n,o]=t;return[e*n+s,r*o+i]}}const{PI:Ae,atan:Pe,atan2:Oe,cos:ke,floor:Ie,log:Te,pow:Re,sin:je,sinh:Xe,sqrt:Ye,tan:Fe,abs:De}=Math,Ne=t=>t*Ae/180;function Le(t,e){return(t+180)/360*Re(2,e)}function $e(t,e){return Ie(Le(t,e))}function Be(t,e){const r=Ne(t);return(1-Te(Fe(r)+1/ke(r))/Ae)*Re(2,e-1)}function Ue(t,e){return Ie(Be(t,e))}function We(t,e,r){return[Le(t,r),Be(e,r)]}function Ve(t,e,r){return[$e(t,r),Ue(e,r)]}function qe(t,e){return t/Re(2,e)*360-180}function Ze(t,e){return(t=>180*t/Ae)(Pe(Xe(Ae-2*Ae*t/Re(2,e))))}function Ge(t,e,r){return[qe(t,r),Ze(e,r)]}function He(t,e,r,s=null){const[i,n]=Ge(t,e,r),[o,a]=Ge(t+1,e+1,r);return s?R([i,a,o,n],s):[i,a,o,n]}function Je(t){const[e,r,s,i]=t;return[(e+s)/2,(r+i)/2]}function Qe(t){const[e,r,s,i]=t;return[[e,i],[s,i],[s,r],[e,r]]}function Ke(t,e={}){const r=Qe(t);return r.push(r[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[r]},properties:e}}function tr(t,e=1,r=e){let[s,i]=t;return[s-e,i-r,s+e,i+r]}const er=[-105.978,35.66],rr=tr(er,.2,.1),sr=[-109.050044,31.332301,-103.001964,37.000104],ir=Je(sr),nr=[-124.733174,24.544701,-66.949895,49.384358],or=Je(nr);function ar(t){const[e,r,s,i]=t;return[De(e-s),De(i-r)]}function hr(t){const[e,r,s,i]=t,n=[e,i],o=[e,r];return[ur(n,[s,i]),ur(n,o)]}function cr(t){const[e,r]=hr(t);return e/r}function lr(t,e,r,s){return"https://overpass-api.de/api/interpreter?data="+encodeURIComponent(`[out:json][timeout:180][bbox:${t},${e},${r},${s}];\nway[highway];\n(._;>;);\nout;`)}function ur(t,e){const[r,s]=t.map((t=>Ne(t))),[i,n]=e.map((t=>Ne(t))),o=i-r,a=je((n-s)/2)**2+ke(s)*ke(n)*je(o/2)**2;return 1e3*(6378.137*(2*Oe(Ye(a),Ye(1-a))))}var dr=Object.freeze({__proto__:null,latlon:function t(e){return"number"!=typeof e[0]?e.map((e=>t(e))):[e[1],e[0]]},lonz2xFloat:Le,lonz2x:$e,latz2yFloat:Be,latz2y:Ue,lonlatz2xyFloat:We,lonlatz2xy:Ve,xz2lon:qe,yz2lat:Ze,xyz2lonlat:Ge,xyz2centerLonlat:function(t,e,r){return[qe(t+.5,r),Ze(e+.5,r)]},xyz2bbox:He,xyInBBox:function(t,e){const[r,s,i,n]=t,[o,a]=e;return D(o,r,i)&&D(a,s,n)},lonLatz2bbox:function(t,e,r){const[s,i]=Ve(t,e,r);return He(s,i,r)},Lbounds2bbox:function(t){let{lng:e,lat:r}=t.getNorthWest(),{lng:s,lat:i}=t.getSouthEast();return[e,i,s,r]},tilesBBox:function(t,e){const[r,s,i,n]=t,[o,a]=Ve(r,n,e);let[h,c]=We(i,s,e);return h=Ie(h),c=Number.isInteger(c)?c-1:Ie(c),[o,c,h,a]},bboxCenter:Je,bboxCoords:Qe,bboxBounds:function(t){const[e,r,s,i]=t;return[[e,i],[s,r]]},bboxFeature:Ke,bboxFromCenter:tr,santaFeCenter:er,santaFeBBox:rr,newMexicoBBox:sr,newMexicoCenter:ir,usaBBox:nr,usaCenter:or,bboxSize:ar,bboxAspect:function(t){const[e,r]=ar(t);return e/r},bboxMetricSize:hr,bboxMetricAspect:cr,getOsmURL:lr,bbox2osm:async function(t){const[e,r,s,i]=t,n=lr(r,e,i,s);return await fetch(n).then((t=>t.json()))},lonLat2meters:ur,attribution:function(t="osm"){const e="Map data &copy; ";switch(t){case"osm":return e+'<a href="https://openstreetmap.org">OpenStreetMap</a>';case"topo":return e+'<a href="https://opentopomap.org">OpenTopoMap</a>';case"topo1":return e+'<a href="https://www.maptiler.com">OpenTopoMap</a>';case"smooth":return e+'<a href="https://stadiamaps.com/">Stadia Maps</a>';case"usgs":return e+'Tiles courtesy of the <a href="https://usgs.gov/">U.S. Geological Survey</a>'}throw Error("gis.attribution: name unknown:",t)},template:function(t="osm"){switch(t){case"osm":return"https://tile.openstreetmap.org/{z}/{x}/{y}.png";case"topo":return"https://a.tile.opentopomap.org/{z}/{x}/{y}.png";case"topo1":return"https://api.maptiler.com/maps/topo/{z}/{x}/{y}.png?key=iQurAP6lArV1UP4gfSVs";case"smooth":return"https://tiles.stadiamaps.com/tiles/alidade_smooth/{z}/{x}/{y}{r}.png";case"usgs":return"https://basemap.nationalmap.gov/arcgis/rest/services/USGSTopo/MapServer/tile/{z}/{y}/{x}";case"contour":return"https://api.maptiler.com/tiles/contours/tiles.json?key=iQurAP6lArV1UP4gfSVs"}throw Error("gis.template: name unknown:",t)},url:function(t,e,r,s="osm"){switch(s){case"osm":return`https://tile.openstreetmap.org/${t}/${e}/${r}.png`;case"topo":return`https://tile.opentopomap.org/${t}/${e}/${r}.png`;case"topo1":return`https://api.maptiler.com/maps/topo/${t}/${e}/${r}.png?key=iQurAP6lArV1UP4gfSVs`;case"smooth":return`https://tiles.stadiamaps.com/tiles/alidade_smooth/${t}/${e}/${r}{r}.png`;case"usgs":return`https://basemap.nationalmap.gov/arcgis/rest/services/USGSTopo/MapServer/tile/${t}/${r}/${e}`}throw Error("gis.url: name unknown:",s)},elevationTemplate:function(t="mapzen"){switch(t){case"mapzen":return"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png";case"maptiler":return"https://api.maptiler.com/tiles/terrain-rgb/{z}/{x}/{y}.png?key=iQurAP6lArV1UP4gfSVs";case"redfishUSA":return"https://s3-us-west-2.amazonaws.com/simtable-elevation-tiles/{z}/{x}/{y}.png";case"redfishWorld":return"https://s3-us-west-2.amazonaws.com/world-elevation-tiles/DEM_tiles/{z}/{x}/{y}.png";case"mapbox":return"https://api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiYmFja3NwYWNlcyIsImEiOiJjanV