UNPKG

agentscript

Version:

AgentScript Model in Model/View architecture

1 lines 796 kB
function t(){return void 0!==globalThis.document}function e(){return void 0!==globalThis.WorkerGlobalScope}function n(){return void 0!==globalThis.Deno}async function i(t,e="blob"){const n=["arrayBuffer","blob","json","text"];if(!n.includes(e))throw Error("fetchData: data must be one of "+n.toString());return fetch(t).then((t=>t[e]()))}function r(t=1e3){return new Promise((e=>{setTimeout(e,t)}))}async function s(t,e=-1,n=0){let i=0;for(;i++!==e;)t(i-1),await r(n)}const o=new Set;function a(t,e=!1){o.has(t)||(e?console.warn(t):console.log(t),o.add(t))}function c(t){a(t,!0)}const l=Math.PI;function h(t){return Math.floor(Math.random()*t)}function u(t,e){return t+Math.floor(Math.random()*(e-t))}function d(t){return Math.random()*t}function p(t,e){return t+Math.random()*(e-t)}function m(t=123456){t%=2147483647,Math.random=()=>((t=16807*t%2147483647)-1)/2147483646}function f(t,e=4){if(0===Math.abs(t))return 0;if(Array.isArray(t))return t.map((t=>f(t,e)));const n=10**e;return Math.round(t*n)/n}const g=t=>!(t&t-1);function y(t,e){return(t%e+e)%e}const v=(t,e,n)=>e+y(t-e,n-e);function x(t,e,n){return t<e?e:t>n?n:t}const b=(t,e,n)=>e<=t&&t<=n,w=(t,e,n)=>t<=e?t+(e-t)*n:t-(t-e)*n;function _(t,e,n){if(e===n)throw Error("lerpScale: lo === hi");return((t=x(t,e,n))-e)/(n-e)}const M=180/Math.PI,S=Math.PI/180;function E(t){return L(t*S)}function T(t){return A(t*M)}function A(t){return y(t,360)}function L(t){return y(t,2*l)}function C(t){return A(90-t*M)}function P(t,e){return A(t)===A(e)}const R=P;function O(t,e){let n=L(t-e);return n>l&&(n-=2*l),n}function I(t,e){let n=A(t-e);return n>180&&(n-=360),n}function z(t,e){return-I(t,e)}function D(t,e,n,i){return Math.atan2(i-e,n-t)}function N(t,e,n,i,r,s,o){if(B(s,o,t,e)>n*n)return!1;const a=D(s,o,t,e);return i/2>=Math.abs(O(r,a))}const B=(t,e,n,i)=>(t-n)**2+(e-i)**2,F=(t,e,n,i)=>Math.sqrt(B(t,e,n,i)),U=(t,e,n,i,r,s)=>(t-i)**2+(e-r)**2+(n-s)**2,k=(t,e,n,i,r,s)=>Math.sqrt(U(t,e,n,i,r,s));function G(t,e=0,n=!0){let i=n;const r=["rectCache"];return JSON.stringify(t,((t,e)=>{if(r.includes(t))return;return Array.isArray(e)&&e.length>0&&Number.isInteger(e[0].id)&&!i?e.map((t=>t.id)):(i=!1,e)}),e)}const H=t=>t,j=t=>e=>e[t];function V(t,e){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function W(t,e){const n=t.indexOf(e);if(-1===n)throw Error(`removeArrayItem: ${e} not in array`);return t.splice(n,1),t}const X=t=>t.map((t=>`${t}`)).join(",");function Y(t,e){if(t.slice)for(let n=0,i=t.length;n<i;n++)e(t[n],n,t);else Object.keys(t).forEach((n=>e(t[n],n,t)))}function q(t,e,n=[]){for(let i=0;i<t;i++)e(i,n);return n}function Z(t,e,n){for(let i=0;i<t;i+=e)n(i)}function J(t,e){const n=t.constructor;if(n===Array)return t.concat(Mt(e,Array));const i=new n(t.length+e.length);return i.set(t),i.set(e,t.length),i}function $(t){return t[h(t.length)]}function Q(t,e){if(t.length<2)throw Error("otherOneOf: array.length < 2");let n;do{n=$(t)}while(e===n);return n}const K=t=>$(Object.keys(t)),tt=t=>t[K(t)];function et(t,e,n=!0){"string"==typeof e&&(e=j(e));const i=(t,n)=>e(t)-e(n);return t.sort(((t,e)=>n?i(t,e):-i(t,e)))}function nt(t){for(let e=t.length-1;e>0;e--){const n=h(e);[t[n],t[e]]=[t[e],t[n]]}return t}function it(t,e,n){if(n<=1)throw Error("floatRamp: numItems must be > 1");const i=[];for(let r=0;r<n;r++)i.push(t+r/(n-1)*(e-t));return i}function rt(t,e,n=e-t+1){return it(t,e,n).map((t=>Math.round(t)))}const st=t=>t.reduce(((t,e)=>Math.max(t,e))),ot=t=>t.reduce(((t,e)=>Math.min(t,e)));function at(t){return!!mt(t)&&Object.values(t).every((t=>wt(t)))}function ct(t,e,n){const i={};return n.forEach((n=>{i[n]=t[n][e]})),i}function lt(t,e=Object.keys(t)){const n=t[e[0]].length,i=new Array(n);return Y(i,((n,r)=>{i[r]=ct(t,r,e)})),i}const ht=t=>({}.toString.call(t).match(/\s(\w+)/)[1].toLowerCase()),ut=(t,e)=>ht(t)===e,dt=(t,e)=>e.includes(ht(t)),pt=t=>ut(t,"string"),mt=t=>ut(t,"object"),ft=t=>Array.isArray(t),gt=t=>ut(t,"number"),yt=t=>Number.isInteger(t),vt=t=>ut(t,"function"),xt=t=>dt(t,["htmlcanvaselement","offscreencanvas"]),bt=t=>dt(t,["image","htmlimageelement","htmlcanvaselement","offscreencanvas","imagebitmap"]),wt=t=>"arraybuffer"===ht(t.buffer),_t=t=>ft(t)||wt(t);function Mt(t,e){return t.constructor===e?t:e.from(t)}function St(t){return"object"===ht(t)&&t.width&&t.height&&t.data}function Et(t,e="download.png",n=null){e.endsWith(".png")||e.endsWith(".jpeg")||(e+=".png");const i=e.endsWith(".png")?"image/png":"image/jpeg",r="string"===ht(t)?t:t.toDataURL(i,n),s=document.createElement("a");s.download=e,s.href=r,s.click()}function Tt(t,e="download",n=!0){St(t)&&!Array.isArray(t.data)&&(t.data=Array.from(t.data)),wt(t)&&(t=Array.from(t)),(mt(t)||Array.isArray(t))&&(t=n?JSON.stringify(t,null,2):JSON.stringify(t));const i="blob"===ht(t)?t:new Blob([t]),r=URL.createObjectURL(i),s=document.createElement("a");s.download=e,s.href=r,s.click(),URL.revokeObjectURL(r)}async function At(i,r=!0){if(t()&&r||n())return new Promise(((t,e)=>{const n=new Image;n.crossOrigin="Anonymous",n.onload=()=>t(n),n.onerror=()=>e(`Could not load image ${i}`),n.src=i}));if(e()||!r){const t=await fetch(i).then((t=>t.blob()));return createImageBitmap(t)}}function Lt(i,r,s=!0){if(t()&&s){const t=document.createElement("canvas");return t.width=i,t.height=r,t}return n()?globalThis.createCanvas(i,r):e()||!s?new OffscreenCanvas(i,r):void 0}function Ct(t,e,i=!0,r={}){const s=Lt(t,e,i),o=s.getContext("2d",r);if(n()){const t={canvas:s};return Object.setPrototypeOf(t,o),t}return o}function Pt(t,e=!0){const n=Ct(t.width,t.height,e);return n.drawImage(t,0,0),n.canvas}function Rt(t,e,n){const i=Pt(t.canvas);t.canvas.width=e,t.canvas.height=n,t.drawImage(i,0,0)}function Ot(t,e,n){t.width===e&&t.height==n||(t.width=e,t.height=n)}function It(t){t.save(),t.resetTransform()}function zt(t,e,n="center",i="middle"){Object.assign(t,{font:e,textAlign:n,textBaseline:i})}let Dt;function Nt(t,e,n="center",i="middle"){Dt||(Dt=Ct(0,0)),zt(Dt,e,n,i);const r=Dt.measureText(t);return r.height=r.actualBoundingBoxAscent+r.actualBoundingBoxDescent,r}function Bt(t,e,n,i,r,s=!0){s&&It(t),t.fillStyle=r.css||r,t.fillText(e,n,i),s&&t.restore()}function Ft(t){return t.getImageData(0,0,t.canvas.width,t.canvas.height)}function Ut(t){const e=Ft(t).data,n=[];return Z(e.length,4,(t=>n.push(e.subarray(t,t+4)))),n}function kt(t,e=void 0){const{width:n,height:i}=t.canvas;It(t),e&&"transparent"!==e?(e=e.css||e,t.fillStyle=e,t.fillRect(0,0,n,i)):t.clearRect(0,0,n,i),t.restore()}function Gt(t){const{width:e,height:n}=t,i=Ct(e,n);return Ht(i,t),i}function Ht(t,e){It(t),t.drawImage(e,0,0,t.canvas.width,t.canvas.height),t.restore()}function jt(t){Object.assign(window,t),console.log("toWindow:",Object.keys(t).join(", "))}function Vt(t){const e=document.createElement("style");e.innerHTML=t,document.head.appendChild(e)}function Wt(){return window.location.search.substr(1)}function Xt(t=Wt()){const e={},n=new URLSearchParams(t);for(const t of n.entries()){let[n,i]=t;(i.match(/^[0-9.]+$/)||i.match(/^[0-9.]+e[0-9]+$/))&&(i=Number(i)),["true","t",""].includes(i)&&(i=!0),["false","f"].includes(i)&&(i=!1),e[n]=i}return e}var Yt=Object.freeze({__proto__:null,AsyncFunction:function(t,e){return new(0,Object.getPrototypeOf((async function(){})).constructor)(...t,e)},PI:l,RESTapi:function(t){return Object.assign(t,Xt())},addCssLink:function(t){const e=document.createElement("link");e.setAttribute("rel","stylesheet"),e.setAttribute("href",t),document.head.appendChild(e)},addCssStyle:Vt,arrayExtent:t=>[ot(t),st(t)],arrayLast:t=>t[t.length-1],arrayMax:st,arrayMin:ot,arrayToMatrix:function(t,e,n){if(t.length!==e*n)throw Error("arrayToMatrix: length !== width * height");const i=[];for(let r=0;r<n;r++){const n=t.slice(r*e,(r+1)*e);i.push(n)}return i},arraysDiff:(t,e,n=t=>t)=>{if(t.length!==e.length)return console.log("lengths differ",t.length,e.length);const i=[];for(let r=0;r<t.length;r++)t[r]!==e[r]&&i.push([n(r),t[r],e[r]]);return i},arraysEqual:V,arraysToString:X,blobToData:function(t,e="dataURL"){e=e[0].toUpperCase()+e.slice(1);const n=["Text","ArrayBuffer","DataURL"];if(!n.includes(e))throw Error("blobToData: data must be one of "+n.toString());const i=new FileReader;return new Promise(((n,r)=>{i.addEventListener("load",(()=>n(i.result))),i.addEventListener("error",(t=>r(t))),i["readAs"+e](t)}))},blobsEqual:async function(t,e){return await t.text()===await e.text()},checkArg:function(t,e="number",n="Function"){},checkArgs:function(t,e="number",n="Function"){},clamp:x,classHasStartup:function(t){console.log("classHasStartup?",t);let e=t.toString().split("\n");return e=e.filter((t=>!/^ *\/\//.test(t))),e=e.filter((t=>/startup\(\)/.test(t))),e.length>0},clearCtx:kt,cloneCanvas:Pt,concatArrays:J,convertArrayType:Mt,createCanvas:Lt,createCtx:Ct,cssTrace:function(t,e=["position","cursor","display","width","height"]){let n=document.querySelector(t);for(;n;){const t=window.getComputedStyle(n);console.log("element:",n),console.log("tag:",n.tagName),e.forEach((e=>console.log(e+":",t[e]))),console.log("------------------------"),n=n.parentElement}},ctxImageColors:Ut,ctxImageData:Ft,ctxImagePixels:function(t){const e=Ft(t);return new Uint32Array(e.data.buffer)},degToHeading:t=>A(90-t),degToRad:E,degreesEqual:P,degreesTowardXY:function(t,e,n,i){return T(D(t,e,n,i))},difference:function(t,e){const n=new Set(e);return t.filter((t=>!n.has(t)))},distance:F,distance3:k,downloadBlob:Tt,downloadCanvas:Et,downloadJson:function(t,e="json.js"){Tt(t,e)},downloadJsonModule:function(t,e="json.js"){Tt(`const json = ${JSON.stringify(t,null,2)}\nexport default json`,e)},drawText:Bt,dump:function(t=window.model){const{patches:e,turtles:n,links:i}=t;Object.assign(window,{ps:e,ts:n,ls:i}),window.p=e.length>0?e.oneOf():{},window.t=n.length>0?n.oneOf():{},window.l=i.length>0?i.oneOf():{},console.log("debug: ps, ts, ls, p, t, l dumped to window")},fetchCssStyle:async function(t){t.startsWith("../")&&(console.log("fetchCssStyle relative url",t),t=import.meta.resolve(t),console.log(" absolute url",t));const e=await fetch(t);if(!e.ok)throw Error(`fetchCssStyle: Not found: ${t}`);const n=await e.text();return Vt(n),n},fetchData:i,fetchImage:async function(t){return new Promise(((e,n)=>{const i=new Image;i.crossOrigin="Anonymous",i.onload=()=>e(i),i.onerror=()=>n(`Could not load image ${t}`),i.src=t}))},fetchImageBitmap:async function(t){const e=await i(t,"blob");return createImageBitmap(e)},fetchJson:async function(t){return i(t,"json")},fetchText:async function(t){return i(t,"text")},fillCtxWithImage:Ht,floatRamp:it,forLoop:Y,fps:function(){const t="undefined"==typeof performance?Date:performance,e=t.now();let n=0;function i(){n++;const r=t.now()-e,s=parseFloat((n/(r/1e3)).toFixed(2));Object.assign(i,{fps:s,ms:r,start:e,steps:n})}return i.steps=0,i},getEventXY:function(t,e){const n=t.getBoundingClientRect();return[e.clientX-n.left,e.clientY-n.top]},getQueryString:Wt,grep:function(t,e){return t.reduce(((t,n)=>(e.test(n)&&t.push(n),t)),[])},hasCanvas:function(){return"undefined"!==globalThis.canvas},headingAngleToRad:function(t){return-E(t)},headingToDeg:t=>A(90-t),headingToRad:function(t){return A(90-t)*S},headingTowardXY:function(t,e,n,i){return C(D(t,e,n,i))},headingsEq:R,identityFcn:H,imagePromise:At,imageToCanvas:function(t){return Gt(t).canvas},imageToCtx:Gt,inCone:N,inDeno:n,inMain:t,inNode:function(){return void 0!==globalThis.require},inWorker:e,integerRamp:rt,intersection:function(t,e){const n=new Set(e);return t.filter((t=>n.has(t)))},isArray:ft,isArrayLike:_t,isBetween:b,isCanvas:xt,isColorLikeArray:t=>_t(t)&&[3,4].includes(t.length)&&t.every((t=>yt(t)&&b(t,0,255)||gt(t)&&b(t,0,1))),isDataSet:St,isFloatArray:t=>/^float.*array$/.test(ht(t)),isFunction:vt,isImage:t=>ut(t,"image"),isImageable:bt,isIntArray:t=>/^int.*array$/.test(ht(t)),isInteger:yt,isLittleEndian:function(){const t=new Uint32Array([16909060]);return 4===new Uint8ClampedArray(t.buffer)[0]},isNumber:gt,isObject:mt,isOneOfTypes:dt,isOofA:at,isPowerOf2:g,isString:pt,isType:ut,isTypedArray:wt,isUintArray:t=>/^uint.*array$/.test(ht(t)),lerp:w,lerpScale:_,logAll:function(t){Object.keys(t).forEach((e=>console.log(" ",e,t[e])))},logOnce:a,matrixToArray:t=>t.flat(),mod:y,mod180180:function(t){let e=A(t);return e>180&&(e-=360),e},mod2pi:L,mod360:A,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)}},nextPowerOf2:t=>Math.pow(2,Math.ceil(Math.log2(t))),noopFcn:()=>{},objectLength:function(t){return Object.keys(t).length},objectToString:function(t,e=2,n=!0){let i=JSON.stringify(t,null,e);return n&&(i=i.replace(/"([^"]+)":/gm,"$1:")),i},objectsEqual:(t,e)=>JSON.stringify(t)===JSON.stringify(e),oneKeyOf:K,oneOf:$,oneValOf:tt,oofaBuffers:function(t){const e=[];return Y(t,(t=>Y(t,(t=>e.push(t.buffer))))),e},oofaObject:ct,otherOneOf:Q,parseQueryString:Xt,pause:r,pps:function(t,e=""){e&&console.log(e);let n=1,i="";for(;t;){if("function"==typeof t)i=t.constructor.toString();else{const e=Object.keys(t);i=e.length>0?`[${e.join(", ")}]`:`[${t.constructor.name}]`}console.log(`[${n++}]: ${i}`),t=Object.getPrototypeOf(t)}},precision:f,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},propFcn:j,radToDeg:T,radToHeading:C,radToHeadingAngle:function(t){return-T(t)},radiansTowardXY:D,radsEqual:function(t,e){return L(t)===L(e)},randomCentered:function(t){return p(-t/2,t/2)},randomFloat:d,randomFloat2:p,randomInt:h,randomInt2:u,randomNormal:function(t=0,e=1){const[n,i]=[1-Math.random(),Math.random()];return Math.sqrt(-2*Math.log(n))*Math.cos(2*l*i)*e+t},randomSeed:m,range:function(t){return q(t,((t,e)=>{e[t]=t}))},removeArrayItem:W,repeat:q,resizeCtx:Rt,runModel:async function(e,n=500,i=!0){if(console.log("runModel: model",e),i&&m(),pt(e)&&(e=(await import(e)).default),vt(e)&&(e=new e),await e.startup(),e.setup(),t())await s((()=>{e.step()}),n);else for(let t=0;t<n;t++)e.step();return e},sampleModel:function(t){const e=G({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)},setCanvasSize:Ot,setCtxImage:function(t,e){Ot(t.canvas,e.width,e.height),Ht(t,e)},setIdentity:It,setTextProperties:zt,shuffle:nt,sortNums:function(t,e=!0){return t.sort(((t,n)=>e?t-n:n-t))},sortObjs:et,sqDistance:B,sqDistance3:U,step:Z,stringMetrics:Nt,subtractDegrees:I,subtractHeadings:z,subtractRadians:O,timeit:function(t,e=1e5,n="test"){n=n+"-"+e,console.time(n);for(let n=0;n<e;n++)t(n);console.timeEnd(n)},timeoutLoop:s,toAofO:lt,toDataURL:function(t,e=void 0){return t.toDataURL?t.toDataURL(e,e):(e||(e="text/plain;charset=US-ASCII"),`data:${e};base64,${btoa(t)}}`)},toDeg:M,toJSON:G,toOofA:function(t,e){const n=t.length,i=Object.keys(e),r={};return i.forEach((t=>{r[t]=new e[t](n)})),Y(t,((t,e)=>{i.forEach((n=>r[n][e]=t[n]))})),r},toRad:S,toWindow:jt,typeOf:ht,union:function(t,e){return Array.from(new Set(t.concat(e)))},waitUntilDone:function(t,e=10){return new Promise((n=>{!function i(){if(t())return n();setTimeout(i,e)}()}))},warn:c,wrap:v});async function qt(t){const e=ht(t);switch(e){case"string":t=await At(t);case"htmlimageelement":return Gt(t);case"htmlcanvaselement":case"offscreencanvas":return t.getContext("2d");case"canvasrenderingcontext2d":return t;default:throw Error("toContext: bad img type: "+e)}}const Zt=[{shift:5,msgMask:7,dataMask:248},{shift:3,msgMask:3,dataMask:252},{shift:0,msgMask:7,dataMask:248}];function Jt(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 $t=Object.freeze({__proto__:null,decode:async function(t,e=!1){const n=await qt(t),{width:i,height:r}=n.canvas,s=n.getImageData(0,0,i,r).data,o=Jt(s);console.log("decode: embedded msg size",o);const a=new Uint8Array(o);return a.forEach(((t,e)=>{let n=4*e;const i=(Zt[0].msgMask&s[n++])<<Zt[0].shift,r=(Zt[1].msgMask&s[n++])<<Zt[1].shift,o=(Zt[2].msgMask&s[n++])<<Zt[2].shift;a[e]=i+r+o})),console.log("decode msgArray",a),e?a:(new TextDecoder).decode(a)},encode:async function(t,e){const n=await qt(t),{width:i,height:r}=n.canvas;!function(t,e,n){const i=e*n;if(i<t.length)throw Error(`encode: image size < msg.length: ${i} ${t.length}`)}(e,i,r);const s=function(t){const e=ht(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",s);const o=n.getImageData(0,0,i,r),a=o.data;let c;return console.log("imgageData.data",a),s.forEach(((t,e)=>{const[n,i,r]=function(t){return[t>>Zt[0].shift,t>>Zt[1].shift&Zt[1].msgMask,t&Zt[2].msgMask]}(t);c=4*e,a[c]=(a[c++]&Zt[0].dataMask)+n,a[c]=(a[c++]&Zt[1].dataMask)+i,a[c]=(a[c++]&Zt[2].dataMask)+r,a[c]=255})),a[c+4]=254,console.log("encoded imgageData.data",a),n.putImageData(o,0,0),console.log("msg length",e.length),console.log("encode: embedded msg size",Jt(a)),n},stegMsgSize:Jt}),Qt=6371008.8,Kt={centimeters:637100880,centimetres:637100880,degrees:57.22891354143274,feet:20902260.511392,inches:39.37*Qt,kilometers:6371.0088,kilometres:6371.0088,meters:Qt,metres:Qt,miles:3958.761333810546,millimeters:6371008800,millimetres:6371008800,nauticalmiles:Qt/1852,radians:1,yards:6967335.223679999},te={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 ee(t,e,n){void 0===n&&(n={});var i={type:"Feature"};return(0===n.id||n.id)&&(i.id=n.id),n.bbox&&(i.bbox=n.bbox),i.properties=e||{},i.geometry=t,i}function ne(t,e,n){if(void 0===n&&(n={}),!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(!ue(t[0])||!ue(t[1]))throw new Error("coordinates must contain numbers");return ee({type:"Point",coordinates:t},e,n)}function ie(t,e,n){void 0===n&&(n={});for(var i=0,r=t;i<r.length;i++){var s=r[i];if(s.length<4)throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");for(var o=0;o<s[s.length-1].length;o++)if(s[s.length-1][o]!==s[0][o])throw new Error("First and last Position are not equivalent.")}return ee({type:"Polygon",coordinates:t},e,n)}function re(t,e,n){if(void 0===n&&(n={}),t.length<2)throw new Error("coordinates must be an array of two or more positions");return ee({type:"LineString",coordinates:t},e,n)}function se(t,e){void 0===e&&(e={});var n={type:"FeatureCollection"};return e.id&&(n.id=e.id),e.bbox&&(n.bbox=e.bbox),n.features=t,n}function oe(t,e,n){return void 0===n&&(n={}),ee({type:"MultiLineString",coordinates:t},e,n)}function ae(t,e,n){return void 0===n&&(n={}),ee({type:"MultiPoint",coordinates:t},e,n)}function ce(t,e,n){return void 0===n&&(n={}),ee({type:"MultiPolygon",coordinates:t},e,n)}function le(t,e){void 0===e&&(e="kilometers");var n=Kt[e];if(!n)throw new Error(e+" units is invalid");return t*n}function he(t,e){void 0===e&&(e="kilometers");var n=Kt[e];if(!n)throw new Error(e+" units is invalid");return t/n}function ue(t){return!isNaN(t)&&null!==t&&!Array.isArray(t)}function de(t){return!!t&&t.constructor===Object}function pe(t,e,n){if(null!==t)for(var i,r,s,o,a,c,l,h,u=0,d=0,p=t.type,m="FeatureCollection"===p,f="Feature"===p,g=m?t.features.length:1,y=0;y<g;y++){a=(h=!!(l=m?t.features[y].geometry:f?t.geometry:t)&&"GeometryCollection"===l.type)?l.geometries.length:1;for(var v=0;v<a;v++){var x=0,b=0;if(null!==(o=h?l.geometries[v]:l)){c=o.coordinates;var w=o.type;switch(u=!n||"Polygon"!==w&&"MultiPolygon"!==w?0:1,w){case null:break;case"Point":if(!1===e(c,d,y,x,b))return!1;d++,x++;break;case"LineString":case"MultiPoint":for(i=0;i<c.length;i++){if(!1===e(c[i],d,y,x,b))return!1;d++,"MultiPoint"===w&&x++}"LineString"===w&&x++;break;case"Polygon":case"MultiLineString":for(i=0;i<c.length;i++){for(r=0;r<c[i].length-u;r++){if(!1===e(c[i][r],d,y,x,b))return!1;d++}"MultiLineString"===w&&x++,"Polygon"===w&&b++}"Polygon"===w&&x++;break;case"MultiPolygon":for(i=0;i<c.length;i++){for(b=0,r=0;r<c[i].length;r++){for(s=0;s<c[i][r].length-u;s++){if(!1===e(c[i][r][s],d,y,x,b))return!1;d++}b++}x++}break;case"GeometryCollection":for(i=0;i<o.geometries.length;i++)if(!1===pe(o.geometries[i],e,n))return!1;break;default:throw new Error("Unknown Geometry Type")}}}}}function me(t,e){var n;switch(t.type){case"FeatureCollection":for(n=0;n<t.features.length&&!1!==e(t.features[n].properties,n);n++);break;case"Feature":e(t.properties,0)}}function fe(t,e){if("Feature"===t.type)e(t,0);else if("FeatureCollection"===t.type)for(var n=0;n<t.features.length&&!1!==e(t.features[n],n);n++);}function ge(t,e){var n,i,r,s,o,a,c,l,h,u,d=0,p="FeatureCollection"===t.type,m="Feature"===t.type,f=p?t.features.length:1;for(n=0;n<f;n++){for(a=p?t.features[n].geometry:m?t.geometry:t,l=p?t.features[n].properties:m?t.properties:{},h=p?t.features[n].bbox:m?t.bbox:void 0,u=p?t.features[n].id:m?t.id:void 0,o=(c=!!a&&"GeometryCollection"===a.type)?a.geometries.length:1,r=0;r<o;r++)if(null!==(s=c?a.geometries[r]:a))switch(s.type){case"Point":case"LineString":case"MultiPoint":case"Polygon":case"MultiLineString":case"MultiPolygon":if(!1===e(s,d,l,h,u))return!1;break;case"GeometryCollection":for(i=0;i<s.geometries.length;i++)if(!1===e(s.geometries[i],d,l,h,u))return!1;break;default:throw new Error("Unknown Geometry Type")}else if(!1===e(null,d,l,h,u))return!1;d++}}function ye(t,e){ge(t,(function(t,n,i,r,s){var o,a=null===t?null:t.type;switch(a){case null:case"Point":case"LineString":case"Polygon":return!1!==e(ee(t,i,{bbox:r,id:s}),n,0)&&void 0}switch(a){case"MultiPoint":o="Point";break;case"MultiLineString":o="LineString";break;case"MultiPolygon":o="Polygon"}for(var c=0;c<t.coordinates.length;c++){var l=t.coordinates[c];if(!1===e(ee({type:o,coordinates:l},i),n,c))return!1}}))}function ve(t,e){ye(t,(function(t,n,i){var r=0;if(t.geometry){var s=t.geometry.type;if("Point"!==s&&"MultiPoint"!==s){var o,a=0,c=0,l=0;return!1!==pe(t,(function(s,h,u,d,p){if(void 0===o||n>a||d>c||p>l)return o=s,a=n,c=d,l=p,void(r=0);var m=re([o,s],t.properties);if(!1===e(m,n,i,p,r))return!1;r++,o=s}))&&void 0}}}))}function xe(t,e){if(!t)throw new Error("geojson is required");ye(t,(function(t,n,i){if(null!==t.geometry){var r=t.geometry.type,s=t.geometry.coordinates;switch(r){case"LineString":if(!1===e(t,n,i,0,0))return!1;break;case"Polygon":for(var o=0;o<s.length;o++)if(!1===e(re(s[o],t.properties),n,i,o))return!1}}}))}function be(t){var e=[1/0,1/0,-1/0,-1/0];return pe(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 we(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 _e(t){return"Feature"===t.type?t.geometry:t}function Me(t,e,n){var i=!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 r=0,s=e.length-1;r<e.length;s=r++){var o=e[r][0],a=e[r][1],c=e[s][0],l=e[s][1];if(t[1]*(o-c)+a*(c-t[0])+l*(t[0]-o)==0&&(o-t[0])*(c-t[0])<=0&&(a-t[1])*(l-t[1])<=0)return!n;a>t[1]!=l>t[1]&&t[0]<(c-o)*(t[1]-a)/(l-a)+o&&(i=!i)}return i}be.default=be;var Se=Object.freeze({__proto__:null,bbox:be,bboxPolygon:function(t,e){void 0===e&&(e={});var n=Number(t[0]),i=Number(t[1]),r=Number(t[2]),s=Number(t[3]);if(6===t.length)throw new Error("@turf/bbox-polygon does not support BBox with 6 positions");var o=[n,i];return ie([[o,[r,i],[r,s],[n,s],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,n){if(void 0===n&&(n={}),!t)throw new Error("point is required");if(!e)throw new Error("polygon is required");var i=we(t),r=_e(e),s=r.type,o=e.bbox,a=r.coordinates;if(o&&!1===function(t,e){return e[0]<=t[0]&&e[1]<=t[1]&&e[2]>=t[0]&&e[3]>=t[1]}(i,o))return!1;"Polygon"===s&&(a=[a]);for(var c=!1,l=0;l<a.length&&!c;l++)if(Me(i,a[l][0],n.ignoreBoundary)){for(var h=!1,u=1;u<a[l].length&&!h;)Me(i,a[l][u],!n.ignoreBoundary)&&(h=!0),u++;h||(c=!0)}return c},collectionOf:function(t,e,n){if(!t)throw new Error("No featureCollection passed");if(!n)throw new Error(".collectionOf() requires a name");if(!t||"FeatureCollection"!==t.type)throw new Error("Invalid input to "+n+", FeatureCollection required");for(var i=0,r=t.features;i<r.length;i++){var s=r[i];if(!s||"Feature"!==s.type||!s.geometry)throw new Error("Invalid input to "+n+", Feature with geometry required");if(!s.geometry||s.geometry.type!==e)throw new Error("Invalid input to "+n+": must be a "+e+", given "+s.geometry.type)}},containsNumber:function t(e){if(e.length>1&&ue(e[0])&&ue(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,n){if(void 0===e&&(e="meters"),void 0===n&&(n="kilometers"),!(t>=0))throw new Error("area must be a positive number");var i=te[e];if(!i)throw new Error("invalid original units");var r=te[n];if(!r)throw new Error("invalid final units");return t/i*r},convertLength:function(t,e,n){if(void 0===e&&(e="kilometers"),void 0===n&&(n="kilometers"),!(t>=0))throw new Error("length must be a positive number");return le(he(t,e),n)},coordAll:function(t){var e=[];return pe(t,(function(t){e.push(t)})),e},coordEach:pe,coordReduce:function(t,e,n,i){var r=n;return pe(t,(function(t,i,s,o,a){r=0===i&&void 0===n?t:e(r,t,i,s,o,a)}),i),r},feature:ee,featureCollection:se,featureEach:fe,featureOf:function(t,e,n){if(!t)throw new Error("No feature passed");if(!n)throw new Error(".featureOf() requires a name");if(!t||"Feature"!==t.type||!t.geometry)throw new Error("Invalid input to "+n+", Feature with geometry required");if(!t.geometry||t.geometry.type!==e)throw new Error("Invalid input to "+n+": must be a "+e+", given "+t.geometry.type)},featureReduce:function(t,e,n){var i=n;return fe(t,(function(t,r){i=0===r&&void 0===n?t:e(i,t,r)})),i},findPoint:function(t,e){if(!de(e=e||{}))throw new Error("options is invalid");var n,i=e.featureIndex||0,r=e.multiFeatureIndex||0,s=e.geometryIndex||0,o=e.coordIndex||0,a=e.properties;switch(t.type){case"FeatureCollection":i<0&&(i=t.features.length+i),a=a||t.features[i].properties,n=t.features[i].geometry;break;case"Feature":a=a||t.properties,n=t.geometry;break;case"Point":case"MultiPoint":return null;case"LineString":case"Polygon":case"MultiLineString":case"MultiPolygon":n=t;break;default:throw new Error("geojson is invalid")}if(null===n)return null;var c=n.coordinates;switch(n.type){case"Point":return ne(c,a,e);case"MultiPoint":return r<0&&(r=c.length+r),ne(c[r],a,e);case"LineString":return o<0&&(o=c.length+o),ne(c[o],a,e);case"Polygon":return s<0&&(s=c.length+s),o<0&&(o=c[s].length+o),ne(c[s][o],a,e);case"MultiLineString":return r<0&&(r=c.length+r),o<0&&(o=c[r].length+o),ne(c[r][o],a,e);case"MultiPolygon":return r<0&&(r=c.length+r),s<0&&(s=c[r].length+s),o<0&&(o=c[r][s].length-o),ne(c[r][s][o],a,e)}throw new Error("geojson is invalid")},findSegment:function(t,e){if(!de(e=e||{}))throw new Error("options is invalid");var n,i=e.featureIndex||0,r=e.multiFeatureIndex||0,s=e.geometryIndex||0,o=e.segmentIndex||0,a=e.properties;switch(t.type){case"FeatureCollection":i<0&&(i=t.features.length+i),a=a||t.features[i].properties,n=t.features[i].geometry;break;case"Feature":a=a||t.properties,n=t.geometry;break;case"Point":case"MultiPoint":return null;case"LineString":case"Polygon":case"MultiLineString":case"MultiPolygon":n=t;break;default:throw new Error("geojson is invalid")}if(null===n)return null;var c=n.coordinates;switch(n.type){case"Point":case"MultiPoint":return null;case"LineString":return o<0&&(o=c.length+o-1),re([c[o],c[o+1]],a,e);case"Polygon":return s<0&&(s=c.length+s),o<0&&(o=c[s].length+o-1),re([c[s][o],c[s][o+1]],a,e);case"MultiLineString":return r<0&&(r=c.length+r),o<0&&(o=c[r].length+o-1),re([c[r][o],c[r][o+1]],a,e);case"MultiPolygon":return r<0&&(r=c.length+r),s<0&&(s=c[r].length+s),o<0&&(o=c[r][s].length-o-1),re([c[r][s][o],c[r][s][o+1]],a,e)}throw new Error("geojson is invalid")},flattenEach:ye,flattenReduce:function(t,e,n){var i=n;return ye(t,(function(t,r,s){i=0===r&&0===s&&void 0===n?t:e(i,t,r,s)})),i},geojsonType:function(t,e,n){if(!e||!n)throw new Error("type and name required");if(!t||t.type!==e)throw new Error("Invalid input to "+n+": must be a "+e+", given "+t.type)},geomEach:ge,geomReduce:function(t,e,n){var i=n;return ge(t,(function(t,r,s,o,a){i=0===r&&void 0===n?t:e(i,t,r,s,o,a)})),i},geometry:function(t,e,n){switch(t){case"Point":return ne(e).geometry;case"LineString":return re(e).geometry;case"Polygon":return ie(e).geometry;case"MultiPoint":return ae(e).geometry;case"MultiLineString":return oe(e).geometry;case"MultiPolygon":return ce(e).geometry;default:throw new Error(t+" is invalid")}},geometryCollection:function(t,e,n){return void 0===n&&(n={}),ee({type:"GeometryCollection",geometries:t},e,n)},getCoord:we,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:_e,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){var e=t%(2*Math.PI);return 180*e/Math.PI}(he(t,e))},lengthToRadians:he,lineEach:xe,lineReduce:function(t,e,n){var i=n;return xe(t,(function(t,r,s,o){i=0===r&&void 0===n?t:e(i,t,r,s,o)})),i},lineString:re,lineStrings:function(t,e,n){return void 0===n&&(n={}),se(t.map((function(t){return re(t,e)})),n)},multiLineString:oe,multiPoint:ae,multiPolygon:ce,point:ne,points:function(t,e,n){return void 0===n&&(n={}),se(t.map((function(t){return ne(t,e)})),n)},polygon:ie,polygons:function(t,e,n){return void 0===n&&(n={}),se(t.map((function(t){return ie(t,e)})),n)},propEach:me,propReduce:function(t,e,n){var i=n;return me(t,(function(t,r){i=0===r&&void 0===n?t:e(i,t,r)})),i},radiansToLength:le,segmentEach:ve,segmentReduce:function(t,e,n){var i=n,r=!1;return ve(t,(function(t,s,o,a,c){i=!1===r&&void 0===n?t:e(i,t,s,o,a,c),r=!0})),i}});class Ee extends Array{static get[Symbol.species](){return Ee}static fromArray(t){return Object.setPrototypeOf(t,Ee.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[y(t,this.length)]}all(t){return this.every(t)}props(t,e=Ee){const n=new e(this.length);for(let e=0;e<this.length;e++)n[e]=this[e][t];return n}typedSample(t){const e={};return Y(t,((t,n)=>{e[n]=this.props(n,t)})),e}uniq(){return Ee.from(new Set(this))}forLoop(t){for(let e=0,n=this.length;e<n;e++)t(this[e],e,this);return this}ask(t){const e=this.length;for(let n=0;n<Math.min(e,this.length);n++)t(this[n],n,this);if(e!=this.length){c(`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 Ee;return pt(t)?this.forLoop((n=>e.push(n[t]))):this.forLoop((n=>e.push(t(n)))),e}count(t){return this.reduce(((e,n)=>e+(t(n)?1:0)),0)}sum(t){return this.reduce(((e,n)=>e+(t?n[t]:n)),0)}avg(t){return this.sum(t)/this.length}min(t){return this.reduce(((e,n)=>Math.min(e,t?n[t]:n)),1/0)}max(t){return this.reduce(((e,n)=>Math.max(e,t?n[t]:n)),-1/0)}extent(t){return[this.min(t),this.max(t)]}histogram(t,e=10,n=this.min(t),i=this.max(t)){const r=(i-n)/e,s=new Ee(e);return s.fill(0),this.ask((o=>{const a=t?o[t]:o;if(a<n||a>i)c(`histogram bounds error: ${a}: ${n}-${i}`);else{let t=Math.floor((a-n)/r);t===e&&t--,s[t]++}})),s.parameters={key:t,bins:e,min:n,max:i,binSize:r,arraySize:this.length},s}clone(){return this.slice(0)}shuffle(){return nt(this)}sortBy(t,e=!0){return et(this,t,e),this}remove(t,e){const n=this.agentIndex(t,e);return-1!==n?this.splice(n,1):console.log(`remove: ${t.id} not in AgentArray`),this}insert(t,e){const n=this.sortedIndex(t,e);this[n]!==t?this.splice(n,0,t):console.log(`insert: item ${t.id} already in AgentArray`)}sortedIndex(t,e=H){pt(e)&&(e=j(e));const n=e(t);let i=0,r=this.length;for(;i<r;){const t=i+r>>>1;e(this[t])<n?i=t+1:r=t}return i}agentIndex(t,e){if(!e)return this.indexOf(t);const n=this.sortedIndex(t,e);return this[n]===t?n:-1}contains(t,e){return this.agentIndex(t,e)>=0}oneOf(){return $(this)}otherOneOf(t){return Q(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,n=!1){if(this.isEmpty())throw Error("min/max OneOf: empty array");"string"==typeof e&&(e=j(e));let i=null,r=t?1/0:-1/0;for(let n=0;n<this.length;n++){const s=this[n],o=e(s);(t&&o<r||!t&&o>r)&&([i,r]=[s,o])}return n?[i,r]:i}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: ${t} ${this.length}`);if(t===this.length)return this;const e=new Ee;for(;e.length<t;){const t=this.oneOf();t in e||e.push(t)}return e}minOrMaxNOf(t,e,n){if(e>this.length)throw Error("min/max nOf: n larger than AgentArray");const i=this.clone().sortBy(n);return t?i.slice(0,e):i.slice(i.length-e)}minNOf(t,e){return this.minOrMaxNOf(!0,t,e)}maxNOf(t,e){return this.minOrMaxNOf(!1,t,e)}}class Te extends Ee{constructor(t,...e){if(!t)throw Error("AgentList requires model");super(...e),this.model=t}inRect(t,e,n=e,i=!1){const r=new Te(this.model),s=t.x-e,o=t.x+e,a=t.y-n,c=t.y+n;return this.ask((e=>{s<=e.x&&e.x<=o&&a<=e.y&&e.y<=c&&(i||t!==e)&&r.push(e)})),r}inRadius(t,e,n=!1){const i=new Te(this.model),r=e*e,s=B;return this.ask((e=>{s(t.x,t.y,e.x,e.y)<=r&&(n||t!==e)&&i.push(e)})),i}inCone(t,e,n,i,r=!1){i=this.model.toRads(i),n=this.model.toAngleRads(n);const s=new Te(this.model);return this.ask((o=>{N(o.x,o.y,e,n,i,t.x,t.y)&&(r||t!==o)&&s.push(o)})),s}}class Ae extends Ee{model;name;baseSet;AgentClass;static get[Symbol.species](){return Ee}constructor(t,e,n,i=null){n.length,super(),i=i||this,Object.assign(this,{model:t,name:n,baseSet:i,AgentClass:e}),this.isBaseSet()?(this.breeds={},this.ID=0):(Object.setPrototypeOf(this,Object.getPrototypeOf(i)),this.baseSet.breeds[n]=this),this.protoMixin(e)}protoMixin(t){this.agentProto=new t;const e=this.agentProto;Object.assign(e,{agentSet:this,model:this.model}),e[this.baseSet.name]=this.baseSet,e.setBreed?console.log("protoMixin: agentClass.proto already set",t):(Object.assign(e,{setBreed(t){t.setBreed(this)},getBreed(){return this.agentSet},isBreed(t){return this.agentSet===t}}),Object.defineProperty(e,"breed",{get:function(){return this.agentSet}}))}addAgent(t=void 0){return t=t||this.agentProto.newInstance(this.agentProto),this.isBreedSet()?this.baseSet.addAgent(t):t.id=this.ID++,this.push(t),t}removeAgent(t){return-1!=t.id&&(this.isBreedSet()&&this.baseSet.remove(t,"id"),this.remove(t,"id")),this}setBreed(t){if(t.breed.name!==this.name)return t.agentSet.isBreedSet()&&t.agentSet.remove(t,"id"),this.isBreedSet()&&this.insert(t,"id"),Object.setPrototypeOf(t,this.agentProto)}setDefault(t,e){return this.agentProto[t]=e,this}getDefault(t){return this.agentProto[t]}newBreed(t){return new Ae(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}`)}clear(){for(;!this.isEmpty();)this.last().die()}ask(t){if(0===this.length)return;const e=this.last().id;for(let n=0;n<this.length&&this[n].id<=e;n++)t(this[n],n,this)}}class Le{width;height;data;static emptyDataSet(t,e,n=Array){return new Le(t,e,new n(t*e))}constructor(t,e,n){if(n.length!==t*e)throw Error(`new DataSet length: ${n.length} !== ${t} * ${e}`);Object.assign(this,{width:t,height:e,data:n})}checkXY(t,e){if(!this.inBounds(t,e))throw Error(`DataSet: x,y out of range: ${t}, ${e}`)}inBounds(t,e){return b(t,0,this.width-1)&&b(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,n){this.data[this.toIndex(t,e)]=n}sample(t,e,n=!0){return this.checkXY(t,e),n?this.nearest(t,e):this.bilinear(t,e)}nearest(t,e){return this.getXY(Math.round(t),Math.round(e))}bilinear(t,e){const n=Math.floor(t),i=Math.floor(e),r=this.toIndex(n,i),s=this.width,o=t-n,a=e-i,c=1-o,l=1-a;return this.data[r]*c*l+(this.data[r+1]||0)*o*l+(this.data[r+s]||0)*c*a+(this.data[r+1+s]||0)*o*a}clone(){return new Le(this.width,this.height,this.data.slice(0))}emptyDataSet(t,e,n=this.dataType()){return Le.emptyDataSet(t,e,n)}emptyArray(t){return new(this.type())(t)}resample(t,e,n=!0,i=Array){if(t===this.width&&e===this.height)return this.clone();const r=Le.emptyDataSet(t,e,i);for(let i=0;i<e;i++)for(let s=0;s<t;s++)r.setXY(s,i,this.sample(s*(this.width-1)/(t-1),i*(this.height-1)/(e-1),n));return r}scale(t,e){const n=this.min(),i=(e-t)/(this.max()-n),r=t-i*n;return this.map((t=>i*t+r))}subset(t,e,n,i){if(t+n>this.width||e+i>this.height)throw console.log("subset: x+width",t+n,"this.width",this.width),console.log("subset: y+height",e+i,"this.height",this.height),Error("DataSet.subSet: params out of range");const r=this.emptyDataSet(n,i);for(let s=0;s<n;s++)for(let n=0;n<i;n++)r.setXY(s,n,this.getXY(s+t,n+e));return r}crop(t,e,n,i){if(void 0===e)var{top:t,bottom:e,left:n,right:i}=t;const r=this.width-n-i,s=this.height-t-e;return this.subset(n,t,r,s)}map(t){return new Le(this.width,this.height,this.data.map(t))}col(t){const[e,n,i]=[this.width,this.height,this.data];if(t>=e)throw Error(`col: x out of range width: ${e} x: ${t}`);const r=this.emptyArray(n);for(let s=0;s<n;s++)r[s]=i[t+s*e];return r}row(t){const[e,n]=[this.width,this.height];if(t>=n)throw Error(`row: y out of range height: ${n} x: ${t}`);return this.data.slice(t*e,(t+1)*e)}convertType(t){this.data=Mt(this.data,t)}concatEast(t){const[e,n]=[this.width,this.height],[i,r]=[t.width,t.height];if(n!==r)throw Error(`concatEast: heights not equal ${n}, ${r}`);const s=this.emptyDataSet(e+i,n);for(let t=0;t<e;t++)for(let e=0;e<n;e++)s.setXY(t,e,this.getXY(t,e));for(let n=0;n<i;n++)for(let i=0;i<r;i++)s.setXY(n+e,i,t.getXY(n,i));return s}concatSouth(t){const[e,n,i]=[this.width,this.height,this.data];if(e!==t.width)throw Error(`concatSouth: widths not equal ${e}, ${t.width}`);const r=J(i,t.data);return new Le(e,n+t.height,r)}transformCoords(t,e,n,i,r,s){return[(t-n)*(this.width-1)/r,(i-e)*(this.height-1)/s]}coordSample(t,e,n,i,r,s,o=!0){const[a,c]=this.transformCoords(t,e,n,i,r,s);return this.sample(a,c,o)}neighborhood(t,e,n=[]){n.length=0;const i=0===t||t===this.width-1||0===e||e===this.height-1;for(let r=-1;r<=1;r++)for(let s=-1;s<=1;s++){let o=t+s,a=e+r;i&&(o=x(o,0,this.width-1),a=x(a,0,this.height-1)),n.push(this.data[this.toIndex(o,a)])}return n}convolve(t,e=1,n=!1){const[i,r,s,o]=n?[1,1,this.height-1,this.width-1]:[0,0,this.height,this.width],a=this.emptyDataSet(o,s),c=a.data;let l=0;for(let n=r;n<s;n++)for(let r=i;r<o;r++){const i=this.neighborhood(r,n);let s=0;for(let e=0;e<t.length;e++)s+=t[e]*i[e];c[l++]=s*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 n=this.dzdx(),i=this.dzdy();let[r,s]=[[],[]];const[o,a]=[n.height,n.width];for(let c=0;c<o;c++)for(let o=0;o<a;o++){const[a,l]=[n.getXY(o,c),i.getXY(o,c)];s.push(Math.atan(F(0,0,a,l))/t);let h=Math.atan2(-l,-a);e&&h<0&&(h+=2*Math.PI),r.push(h)}return s=new Le(a,o,s),r=new Le(a,o,r),{slope:s,aspect:r,dzdx:n,dzdy:i}}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,n=!1){const[i,r]=this.extent(),s=1/(r-i);let o=this.data.map((n=>w(t,e,s*(n-i))));return n&&(o=o.map((t=>Math.round(t)))),new Le(this.width,this.height,o)}equals(t){return this.width===t.width&&this.height===t.height&&V(this.data,t.data)}}class Ce{static defaults={width:1,hidden:!1,agentSet:null,model:null,name:null};static variables={id:null,theta:0,x:0,y:0};constructor(){Object.assign(this,Ce.defaults)}newInstance(t){const e=Object.create(t);return Object.assign(e,Ce.variables),e}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),W(this.end0.links,this),W(this.end1.links,this),this.id=-1)}isDead(){return-1===this.id}bothEnds(){return Ee.fromArray([this.end0,this.end1])}length(){return this.end0.distance(this.end1)}get heading(){const{x0:t,x1:e,y0:n,y1:i}=this,r=Math.atan2(i-n,e-t);return this.model.fromRads(r)}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((n=>n.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 Pe extends Ae{constructor(t,e,n,i=null){super(t,e,n,i)}createOne(t,e,n=t=>{}){const i=this.addAgent();return i.init(t,e),n(i),i}create(t,e,n=t=>{}){return Array.isArray(e)||(e=[e]),e.map((e=>this.createOne(t,e,n)))}}class Re{maxX=16;minX=-16;maxY=16;minY=-16;maxZ=16;minZ=-16;static defaultOptions(t=16,e=t,n=Math.max(t,e)){return{minX:-t,maxX:t,minY:-e,maxY:e,minZ:-n,maxZ:n}}static defaultWorld(t=16,e=t,n=t){return new Re(Re.defaultOptions(t,e,n))}constructor(t={}){Object.assign(this,t),this.setWorld()}setWorld(){let{minX:t,maxX:e,minY:n,maxY:i,minZ:r,maxZ:s}=this;Y({minX:t,maxX:e,minY:n,maxY:i,minZ:r,maxZ:s},((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=i-n+1,this.numZ=this.depth=s-r+1,this.minXcor=t-.5,this.maxXcor=e+.5,this.minYcor=n-.5,this.maxYcor=i+.5,this.minZcor=r-.5,this.maxZcor=s+.5,this.centerX=(t+e)/2,this.centerY=(n+i)/2,this.centerZ=(r+s)/2,this.numPatches=this.numX*this.numY}getOptions(){const{minX:t,minY:e,minZ:n,maxX:i,maxY:r,maxZ:s}=this;return{minX:t,minY:e,minZ:n,maxX:i,maxY:r,maxZ:s}}randomPoint(){return[p(this.minXcor,this.maxXcor),p(this.minYcor,this.maxYcor)]}random3DPoint(){return[p(this.minXcor,this.maxXcor),p(this.minYcor,this.maxYcor),p(this.minZcor,this.maxZcor)]}randomPatchPoint(){return[u(this.minX,this.maxX),u(this.minY,this.maxY)]}isOnWorld(t,e,n=this.centerZ){return this.minXcor<=t&&t<=this.maxXcor&&this.minYcor<=e&&e<=this.maxYcor&&this.minZcor<=n&&n<=this.maxZcor}bboxTransform(t,e,n,i){return new Oe(t,e,n,i,this)}getWorldSize(){return[this.numX,this.numY]}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:n}=this,{clientWidth:i,clientHeight:r}=t,s=i/e,o=r/n;if(s!==o)throw Error(`World patchSize: x/y sizes differ ${s}, ${o}`);return s}setCanvasSize(t,e){Ot(t,this.numX*e,this.numY*e)}pixelXYtoPatchXY(t,e,n){return[this.minXcor+t/n,this.maxYcor-e/n]}patchXYtoPixelXY(t,e,n){return[(t-this.minXcor)*n,(this.maxYcor-e)*n]}xyToPatchIndex(t,e){if(!this.isOnWorld(t,e))return;const{minX:n,maxX:i,maxY:r,numX:s,maxXcor:o,maxYcor:a}=this;return(t=t===o?i:Math.round(t))-n+s*(r-(e=e===a?r:Math.round(e)))}}class Oe{constructor(t,e,n,i,r){this.bbox=[t,e,n,i],t<n&&console.log("flipX"),i<e&&console.log("flipY"),t<n&&([t,n]=[n,t]),i<e&&([i,e]=[e,i]);const{maxXcor:s,maxYcor:o,minXcor:a,minYcor:c}=r,l=(t-n)/(s-a),h=(i-e)/(o-c),u=(t+n-l*(s+a))/2,d=(i+e-h*(o+c))/2;Object.assign(this,{mx:l,my:h,bx:u,by:d})}toWorld(t){const{mx:e,my:n,bx:i,by:r}=this,[s,o]=t;return[(s-i)/e,(o-r)/n]}toBBox(t){const{mx:e,my:n,bx:i,by:r}=this,[s,o]=t;return[e*s+i,n*o+r]}}const{PI:Ie,atan:ze,atan2:De,cos:Ne,floor:Be,log:Fe,pow:Ue,sin:ke,sinh:Ge,sqrt:He,tan:je,abs:Ve}=Math,We=t=>t*Ie/180,Xe=t=>180*t/Ie;function Ye(t,e){return(t+180)/360*Ue(2,e)}function qe(t,e){return Be(Ye(t,e))}function Ze(t,e){const n=We(t);return(1-Fe(je(n)+1/Ne(n))/Ie)*Ue(2,e-1)}function Je(t,e){return Be(Ze(t,e))}function $e(t,e,n){return[Ye(t,n),Ze(e,n)]}function Qe(t,e,n){return[qe(t,n),Je(e,n)]}function Ke(t,e){return t/Ue(2,e)*360-180}function tn(t,e){const n=ze(Ge(Ie-2*Ie*t/Ue(2,e)));return Xe(n)}function en(t,e,n){return[Ke(t,n),tn(e,n)]}function nn(t,e,n,i=null){const[r,s]=en(t,e,n),[o,a]=en(t+1,e+1,n);return i?f([r,a,o,s],i):[r,a,o,s]}function rn(t){const[e,n,i,r]=t;return[(e+i)/2,(n+r)/2]}function sn(t){const[e,n,i,r]=t;return[[e,r],[i,r],[i,n],[e,n]]}function on(t,e={}){const n=sn(t);return n.push(n[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[n]},properties:e}}function an(t,e=1,n=e){let[i,r]=t;return[i-e,r-n,i+e,r+n]}const cn=[-105.978,35.66],ln=an(cn,.2,.1),hn=[-109.050044,31.332301,-103.001964,37.000104],un=rn(hn),dn=[-124.733174,24.544701,-66.949895,49.384358],pn=rn(dn);function mn(t){const[e,n,i,r]=t;return[Ve(e-i),Ve(r-n)]}function fn(t){const[e,n,i,r]=t,s=[e,r],o=[e,n];return[vn(s,[i,r]),vn(s,o)]}function gn(t){const[e,n]=fn(t);return e/n}function yn(t,e,n,i){return"https://overpass-api.de/api/interpreter?data="+encodeURIComponent(`[out:json][timeout:180][bbox:${t},${e},${n},${i}];\nway[highway];\n(._;>;);\nout;`)}function vn(t,e){const[n,i]=t.map((t=>We(t))),[r,s]=e.map((t=>We(t))),o=r-n,a=ke((s-i)/2)**2+Ne(i)*Ne(s)*ke(o/2)**2;return 1e3*(6378.137*(2*De(He(a),He(1-a))))}var xn=Object.freeze({__proto__:null,Lbounds2bbox:function(t){let{lng:e,lat:n}=t.getNorthWest(),{lng:i,lat:r}=t.getSouthEast();return[e,r,i,n]},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)},bbox2osm:async function(t){const[e,n,i,r]=t,s=yn(n,e,r,i);return await fetch(s).then((t=>t.json()))},bboxAspect:function(t){const[e,n]=mn(t);return e/n},bboxBounds:function(t){const[e,n,i,r]=t;return[[e,r],[i,n]]},bboxCenter:rn,bboxCoords:sn,bboxFeature:on,bboxFromCenter:an,bboxMetricAspect:gn,bboxMetricSize:fn,bboxSize:mn,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.eyJ1IjoiYmFja3NwYWNlcyIsImEiOiJjanVrbzI4dncwOXl3M3ptcGJtN3oxMmhoIn0.x9iSCrtm0iADEqixVgPwqQ"}throw Error("gis.elevationTemplate: name unknown:",t)},getOsmURL:yn,latlon:function t(e){return"number"!=typeof e[0]?e.map((e=>t(e))):[e[1],e[0]]},latz2y:Je,latz2yFloat:Ze,lonLat2meters:vn,lonLatz2bbox:function(t,e,n){const[i,r]=Qe(t,e,n);return nn(i,r,n)},lonlatz2xy:Qe,lonlatz2xyFloat:$e,lonz2x:qe,lonz2xFloat:Ye,newMexicoBBox:hn,newMexicoCenter:un,santaFeBBox:ln,santaFeCenter:cn,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)},tilesBBox:function(t,e){const[n,i,r,s]=t,[o,a]=Qe(n,s,e);let[c,l]=$e(r,i,e);return c=Be(c),l=Number.isInteger(l)?l-1:Be(l),[o,l,c,a]},url:function(t,e,n,i="osm"){switch(i){case"osm":return`https://tile.openstreetmap.org/${t}/${e}/${n}.png`;case"topo":return`https://tile.opentopomap.org/${t}/${e}/${n}.png`;case"topo1":return`https://api.maptiler.com/maps/topo/${t}/${e}/${n}.png?key=iQurAP6lArV1UP4gfSVs`;case"smooth":return`https://tiles.stadiamaps.com/tiles/alidade_smooth/${t}/${e}/${n}{r}.png`;case"usgs":return`https://basemap.nationalmap.gov/arcgis/rest/services/USGSTopo/MapServer/tile/${t}/${n}/${e}`}throw Error("gis.url: name unknown