agentscript
Version:
AgentScript Model in Model/View architecture
1 lines • 137 kB
JavaScript
function t(){return void 0!==globalThis.document}function e(){return void 0!==globalThis.WorkerGlobalScope}function r(){return void 0!==globalThis.Deno}async function s(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 i(t=1e3){return new Promise((e=>{setTimeout(e,t)}))}async function n(t,e=-1,r=0){let s=0;for(;s++!==e;)t(s-1),await i(r)}const o=new Set;function a(t,e=!1){o.has(t)||(e?console.warn(t):console.log(t),o.add(t))}function h(t){a(t,!0)}const l=Math.PI;function c(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 m(t,e){return t+Math.random()*(e-t)}function p(t=123456){t%=2147483647,Math.random=()=>((t=16807*t%2147483647)-1)/2147483646}function g(t,e=4){if(0===Math.abs(t))return 0;if(Array.isArray(t))return t.map((t=>g(t,e)));const r=10**e;return Math.round(t*r)/r}const f=t=>!(t&t-1);function y(t,e){return(t%e+e)%e}const x=(t,e,r)=>e+y(t-e,r-e);function w(t,e,r){return t<e?e:t>r?r:t}const b=(t,e,r)=>e<=t&&t<=r,M=(t,e,r)=>t<=e?t+(e-t)*r:t-(t-e)*r;function v(t,e,r){if(e===r)throw Error("lerpScale: lo === hi");return((t=w(t,e,r))-e)/(r-e)}const z=180/Math.PI,_=Math.PI/180;function S(t){return O(t*_)}function C(t){return E(t*z)}function E(t){return y(t,360)}function O(t){return y(t,2*l)}function P(t){return E(90-t*z)}function A(t,e){return E(t)===E(e)}const k=A;function I(t,e){let r=O(t-e);return r>l&&(r-=2*l),r}function T(t,e){let r=E(t-e);return r>180&&(r-=360),r}function R(t,e){return-T(t,e)}function j(t,e,r,s){return Math.atan2(s-e,r-t)}function X(t,e,r,s,i,n,o){if(F(n,o,t,e)>r*r)return!1;const a=j(n,o,t,e);return s/2>=Math.abs(I(i,a))}const F=(t,e,r,s)=>(t-r)**2+(e-s)**2,Y=(t,e,r,s)=>Math.sqrt(F(t,e,r,s)),D=(t,e,r,s,i,n)=>(t-s)**2+(e-i)**2+(r-n)**2,N=(t,e,r,s,i,n)=>Math.sqrt(D(t,e,r,s,i,n));function L(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 $=t=>t,B=t=>e=>e[t];function U(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 W(t,e){const r=t.indexOf(e);if(-1===r)throw Error(`removeArrayItem: ${e} not in array`);return t.splice(r,1),t}const q=t=>t.map((t=>`${t}`)).join(",");function V(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 Z(t,e,r=[]){for(let s=0;s<t;s++)e(s,r);return r}function H(t,e,r){for(let s=0;s<t;s+=e)r(s)}function G(t,e){const r=t.constructor;if(r===Array)return t.concat(zt(e,Array));const s=new r(t.length+e.length);return s.set(t),s.set(e,t.length),s}function J(t){return t[c(t.length)]}function Q(t,e){if(t.length<2)throw Error("otherOneOf: array.length < 2");let r;do{r=J(t)}while(e===r);return r}const K=t=>J(Object.keys(t)),tt=t=>t[K(t)];function et(t,e,r=!0){"string"==typeof e&&(e=B(e));const s=(t,r)=>e(t)-e(r);return t.sort(((t,e)=>r?s(t,e):-s(t,e)))}function rt(t){for(let e=t.length-1;e>0;e--){const r=c(e);[t[r],t[e]]=[t[e],t[r]]}return t}function st(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 it(t,e,r=e-t+1){return st(t,e,r).map((t=>Math.round(t)))}const nt=t=>t.reduce(((t,e)=>Math.max(t,e))),ot=t=>t.reduce(((t,e)=>Math.min(t,e)));function at(t){return!!pt(t)&&Object.values(t).every((t=>Mt(t)))}function ht(t,e,r){const s={};return r.forEach((r=>{s[r]=t[r][e]})),s}function lt(t,e=Object.keys(t)){const r=t[e[0]].length,s=new Array(r);return V(s,((r,i)=>{s[i]=ht(t,i,e)})),s}const ct=t=>({}.toString.call(t).match(/\s(\w+)/)[1].toLowerCase()),ut=(t,e)=>ct(t)===e,dt=(t,e)=>e.includes(ct(t)),mt=t=>ut(t,"string"),pt=t=>ut(t,"object"),gt=t=>Array.isArray(t),ft=t=>ut(t,"number"),yt=t=>Number.isInteger(t),xt=t=>ut(t,"function"),wt=t=>dt(t,["htmlcanvaselement","offscreencanvas"]),bt=t=>dt(t,["image","htmlimageelement","htmlcanvaselement","offscreencanvas","imagebitmap"]),Mt=t=>"arraybuffer"===ct(t.buffer),vt=t=>gt(t)||Mt(t);function zt(t,e){return t.constructor===e?t:e.from(t)}function _t(t){return"object"===ct(t)&&t.width&&t.height&&t.data}function St(t,e="download.png",r=null){e.endsWith(".png")||e.endsWith(".jpeg")||(e+=".png");const s=e.endsWith(".png")?"image/png":"image/jpeg",i="string"===ct(t)?t:t.toDataURL(s,r),n=document.createElement("a");n.download=e,n.href=i,n.click()}function Ct(t,e="download",r=!0){_t(t)&&!Array.isArray(t.data)&&(t.data=Array.from(t.data)),Mt(t)&&(t=Array.from(t)),(pt(t)||Array.isArray(t))&&(t=r?JSON.stringify(t,null,2):JSON.stringify(t));const s="blob"===ct(t)?t:new Blob([t]),i=URL.createObjectURL(s),n=document.createElement("a");n.download=e,n.href=i,n.click(),URL.revokeObjectURL(i)}async function Et(s,i=!0){if(t()&&i||r())return new Promise(((t,e)=>{const r=new Image;r.crossOrigin="Anonymous",r.onload=()=>t(r),r.onerror=()=>e(`Could not load image ${s}`),r.src=s}));if(e()||!i){const t=await fetch(s).then((t=>t.blob()));return createImageBitmap(t)}}function Ot(s,i,n=!0){if(t()&&n){const t=document.createElement("canvas");return t.width=s,t.height=i,t}return r()?globalThis.createCanvas(s,i):e()||!n?new OffscreenCanvas(s,i):void 0}function Pt(t,e,s=!0,i={}){const n=Ot(t,e,s),o=n.getContext("2d",i);if(r()){const t={canvas:n};return Object.setPrototypeOf(t,o),t}return o}function At(t,e=!0){const r=Pt(t.width,t.height,e);return r.drawImage(t,0,0),r.canvas}function kt(t,e,r){const s=At(t.canvas);t.canvas.width=e,t.canvas.height=r,t.drawImage(s,0,0)}function It(t,e,r){t.width===e&&t.height==r||(t.width=e,t.height=r)}function Tt(t){t.save(),t.resetTransform()}function Rt(t,e,r="center",s="middle"){Object.assign(t,{font:e,textAlign:r,textBaseline:s})}let jt;function Xt(t,e,r="center",s="middle"){jt||(jt=Pt(0,0)),Rt(jt,e,r,s);const i=jt.measureText(t);return i.height=i.actualBoundingBoxAscent+i.actualBoundingBoxDescent,i}function Ft(t,e,r,s,i,n=!0){n&&Tt(t),t.fillStyle=i.css||i,t.fillText(e,r,s),n&&t.restore()}function Yt(t){return t.getImageData(0,0,t.canvas.width,t.canvas.height)}function Dt(t){const e=Yt(t).data,r=[];return H(e.length,4,(t=>r.push(e.subarray(t,t+4)))),r}function Nt(t,e=void 0){const{width:r,height:s}=t.canvas;Tt(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 Lt(t){const{width:e,height:r}=t,s=Pt(e,r);return $t(s,t),s}function $t(t,e){Tt(t),t.drawImage(e,0,0,t.canvas.width,t.canvas.height),t.restore()}function Bt(t){Object.assign(window,t),console.log("toWindow:",Object.keys(t).join(", "))}function Ut(t){const e=document.createElement("style");e.innerHTML=t,document.head.appendChild(e)}function Wt(){return window.location.search.substr(1)}function qt(t=Wt()){const e={},r=new URLSearchParams(t);for(const t of r.entries()){let[r,s]=t;(s.match(/^[0-9.]+$/)||s.match(/^[0-9.]+e[0-9]+$/))&&(s=Number(s)),["true","t",""].includes(s)&&(s=!0),["false","f"].includes(s)&&(s=!1),e[r]=s}return e}var Vt=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,qt())},addCssLink:function(t){const e=document.createElement("link");e.setAttribute("rel","stylesheet"),e.setAttribute("href",t),document.head.appendChild(e)},addCssStyle:Ut,arrayExtent:t=>[ot(t),nt(t)],arrayLast:t=>t[t.length-1],arrayMax:nt,arrayMin:ot,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},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},arraysEqual:U,arraysToString:q,blobToData: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)}))},blobsEqual:async function(t,e){return await t.text()===await e.text()},checkArg:function(t,e="number",r="Function"){},checkArgs:function(t,e="number",r="Function"){},clamp:w,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:Nt,cloneCanvas:At,concatArrays:G,convertArrayType:zt,createCanvas:Ot,createCtx:Pt,cssTrace:function(t,e=["position","cursor","display","width","height"]){let r=document.querySelector(t);for(;r;){const t=window.getComputedStyle(r);console.log("element:",r),console.log("tag:",r.tagName),e.forEach((e=>console.log(e+":",t[e]))),console.log("------------------------"),r=r.parentElement}},ctxImageColors:Dt,ctxImageData:Yt,ctxImagePixels:function(t){const e=Yt(t);return new Uint32Array(e.data.buffer)},degToHeading:t=>E(90-t),degToRad:S,degreesEqual:A,degreesTowardXY:function(t,e,r,s){return C(j(t,e,r,s))},difference:function(t,e){const r=new Set(e);return t.filter((t=>!r.has(t)))},distance:Y,distance3:N,downloadBlob:Ct,downloadCanvas:St,downloadJson:function(t,e="json.js"){Ct(t,e)},downloadJsonModule:function(t,e="json.js"){Ct(`const json = ${JSON.stringify(t,null,2)}\nexport default json`,e)},drawText:Ft,dump:function(t=window.model){const{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")},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 r=await e.text();return Ut(r),r},fetchData:s,fetchImage:async function(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}))},fetchImageBitmap:async function(t){const e=await s(t,"blob");return createImageBitmap(e)},fetchJson:async function(t){return s(t,"json")},fetchText:async function(t){return s(t,"text")},fillCtxWithImage:$t,floatRamp:st,forLoop:V,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},getEventXY:function(t,e){const r=t.getBoundingClientRect();return[e.clientX-r.left,e.clientY-r.top]},getQueryString:Wt,grep:function(t,e){return t.reduce(((t,r)=>(e.test(r)&&t.push(r),t)),[])},hasCanvas:function(){return"undefined"!==globalThis.canvas},headingAngleToRad:function(t){return-S(t)},headingToDeg:t=>E(90-t),headingToRad:function(t){return E(90-t)*_},headingTowardXY:function(t,e,r,s){return P(j(t,e,r,s))},headingsEq:k,identityFcn:$,imagePromise:Et,imageToCanvas:function(t){return Lt(t).canvas},imageToCtx:Lt,inCone:X,inDeno:r,inMain:t,inNode:function(){return void 0!==globalThis.require},inWorker:e,integerRamp:it,intersection:function(t,e){const r=new Set(e);return t.filter((t=>r.has(t)))},isArray:gt,isArrayLike:vt,isBetween:b,isCanvas:wt,isColorLikeArray:t=>vt(t)&&[3,4].includes(t.length)&&t.every((t=>yt(t)&&b(t,0,255)||ft(t)&&b(t,0,1))),isDataSet:_t,isFloatArray:t=>/^float.*array$/.test(ct(t)),isFunction:xt,isImage:t=>ut(t,"image"),isImageable:bt,isIntArray:t=>/^int.*array$/.test(ct(t)),isInteger:yt,isLittleEndian:function(){const t=new Uint32Array([16909060]);return 4===new Uint8ClampedArray(t.buffer)[0]},isNumber:ft,isObject:pt,isOneOfTypes:dt,isOofA:at,isPowerOf2:f,isString:mt,isType:ut,isTypedArray:Mt,isUintArray:t=>/^uint.*array$/.test(ct(t)),lerp:M,lerpScale:v,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=E(t);return e>180&&(e-=360),e},mod2pi:O,mod360:E,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,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),oneKeyOf:K,oneOf:J,oneValOf:tt,oofaBuffers:function(t){const e=[];return V(t,(t=>V(t,(t=>e.push(t.buffer))))),e},oofaObject:ht,otherOneOf:Q,parseQueryString:qt,pause:i,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)}},precision:g,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:B,radToDeg:C,radToHeading:P,radToHeadingAngle:function(t){return-C(t)},radiansTowardXY:j,radsEqual:function(t,e){return O(t)===O(e)},randomCentered:function(t){return m(-t/2,t/2)},randomFloat:d,randomFloat2:m,randomInt:c,randomInt2:u,randomNormal:function(t=0,e=1){const[r,s]=[1-Math.random(),Math.random()];return Math.sqrt(-2*Math.log(r))*Math.cos(2*l*s)*e+t},randomSeed:p,range:function(t){return Z(t,((t,e)=>{e[t]=t}))},removeArrayItem:W,repeat:Z,resizeCtx:kt,runModel:async function(e,r=500,s=!0){if(console.log("runModel: model",e),s&&p(),mt(e)&&(e=(await import(e)).default),xt(e)&&(e=new e),await e.startup(),e.setup(),t())await n((()=>{e.step()}),r);else for(let t=0;t<r;t++)e.step();return e},sampleModel:function(t){const e=L({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:It,setCtxImage:function(t,e){It(t.canvas,e.width,e.height),$t(t,e)},setIdentity:Tt,setTextProperties:Rt,shuffle:rt,sortNums:function(t,e=!0){return t.sort(((t,r)=>e?t-r:r-t))},sortObjs:et,sqDistance:F,sqDistance3:D,step:H,stringMetrics:Xt,subtractDegrees:T,subtractHeadings:R,subtractRadians:I,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)},timeoutLoop:n,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:z,toJSON:L,toOofA:function(t,e){const r=t.length,s=Object.keys(e),i={};return s.forEach((t=>{i[t]=new e[t](r)})),V(t,((t,e)=>{s.forEach((r=>i[r][e]=t[r]))})),i},toRad:_,toWindow:Bt,typeOf:ct,union:function(t,e){return Array.from(new Set(t.concat(e)))},waitUntilDone:function(t,e=10){return new Promise((r=>{!function s(){if(t())return r();setTimeout(s,e)}()}))},warn:h,wrap:x});async function Zt(t){const e=ct(t);switch(e){case"string":t=await Et(t);case"htmlimageelement":return Lt(t);case"htmlcanvaselement":case"offscreencanvas":return t.getContext("2d");case"canvasrenderingcontext2d":return t;default:throw Error("toContext: bad img type: "+e)}}const Ht=[{shift:5,msgMask:7,dataMask:248},{shift:3,msgMask:3,dataMask:252},{shift:0,msgMask:7,dataMask:248}];function Gt(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 Jt=Object.freeze({__proto__:null,decode:async function(t,e=!1){const r=await Zt(t),{width:s,height:i}=r.canvas,n=r.getImageData(0,0,s,i).data,o=Gt(n);console.log("decode: embedded msg size",o);const a=new Uint8Array(o);return a.forEach(((t,e)=>{let r=4*e;const s=(Ht[0].msgMask&n[r++])<<Ht[0].shift,i=(Ht[1].msgMask&n[r++])<<Ht[1].shift,o=(Ht[2].msgMask&n[r++])<<Ht[2].shift;a[e]=s+i+o})),console.log("decode msgArray",a),e?a:(new TextDecoder).decode(a)},encode:async function(t,e){const r=await Zt(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=ct(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>>Ht[0].shift,t>>Ht[1].shift&Ht[1].msgMask,t&Ht[2].msgMask]}(t);h=4*e,a[h]=(a[h++]&Ht[0].dataMask)+r,a[h]=(a[h++]&Ht[1].dataMask)+s,a[h]=(a[h++]&Ht[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",Gt(a)),r},stegMsgSize:Gt}),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,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 re(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(!ue(t[0])||!ue(t[1]))throw new Error("coordinates must contain numbers");return ee({type:"Point",coordinates:t},e,r)}function se(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 ee({type:"Polygon",coordinates:t},e,r)}function ie(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 ee({type:"LineString",coordinates:t},e,r)}function ne(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 oe(t,e,r){return void 0===r&&(r={}),ee({type:"MultiLineString",coordinates:t},e,r)}function ae(t,e,r){return void 0===r&&(r={}),ee({type:"MultiPoint",coordinates:t},e,r)}function he(t,e,r){return void 0===r&&(r={}),ee({type:"MultiPolygon",coordinates:t},e,r)}function le(t,e){void 0===e&&(e="kilometers");var r=Kt[e];if(!r)throw new Error(e+" units is invalid");return t*r}function ce(t,e){void 0===e&&(e="kilometers");var r=Kt[e];if(!r)throw new Error(e+" units is invalid");return t/r}function ue(t){return!isNaN(t)&&null!==t&&!Array.isArray(t)}function de(t){return!!t&&t.constructor===Object}function me(t,e,r){if(null!==t)for(var s,i,n,o,a,h,l,c,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=(c=!!(l=p?t.features[y].geometry:g?t.geometry:t)&&"GeometryCollection"===l.type)?l.geometries.length:1;for(var x=0;x<a;x++){var w=0,b=0;if(null!==(o=c?l.geometries[x]:l)){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===me(o.geometries[s],e,r))return!1;break;default:throw new Error("Unknown Geometry Type")}}}}}function pe(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 ge(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 fe(t,e){var r,s,i,n,o,a,h,l,c,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,l=m?t.features[r].properties:p?t.properties:{},c=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,l,c,u))return!1;break;case"GeometryCollection":for(s=0;s<n.geometries.length;s++)if(!1===e(n.geometries[s],d,l,c,u))return!1;break;default:throw new Error("Unknown Geometry Type")}else if(!1===e(null,d,l,c,u))return!1;d++}}function ye(t,e){fe(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(ee(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 l=t.coordinates[h];if(!1===e(ee({type:o,coordinates:l},s),r,h))return!1}}))}function xe(t,e){ye(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,l=0;return!1!==me(t,(function(n,c,u,d,m){if(void 0===o||r>a||d>h||m>l)return o=n,a=r,h=d,l=m,void(i=0);var p=ie([o,n],t.properties);if(!1===e(p,r,s,m,i))return!1;i++,o=n}))&&void 0}}}))}function we(t,e){if(!t)throw new Error("geojson is required");ye(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(ie(n[o],t.properties),r,s,o))return!1}}}))}function be(t){var e=[1/0,1/0,-1/0,-1/0];return me(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 Me(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 ve(t){return"Feature"===t.type?t.geometry:t}function ze(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],l=e[n][1];if(t[1]*(o-h)+a*(h-t[0])+l*(t[0]-o)==0&&(o-t[0])*(h-t[0])<=0&&(a-t[1])*(l-t[1])<=0)return!r;a>t[1]!=l>t[1]&&t[0]<(h-o)*(t[1]-a)/(l-a)+o&&(s=!s)}return s}be.default=be;var _e=Object.freeze({__proto__:null,bbox:be,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 se([[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=Me(t),i=ve(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,l=0;l<a.length&&!h;l++)if(ze(s,a[l][0],r.ignoreBoundary)){for(var c=!1,u=1;u<a[l].length&&!c;)ze(s,a[l][u],!r.ignoreBoundary)&&(c=!0),u++;c||(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&&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,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=te[e];if(!s)throw new Error("invalid original units");var i=te[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 le(ce(t,e),r)},coordAll:function(t){var e=[];return me(t,(function(t){e.push(t)})),e},coordEach:me,coordReduce:function(t,e,r,s){var i=r;return me(t,(function(t,s,n,o,a){i=0===s&&void 0===r?t:e(i,t,s,n,o,a)}),s),i},feature:ee,featureCollection:ne,featureEach:ge,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 ge(t,(function(t,i){s=0===i&&void 0===r?t:e(s,t,i)})),s},findPoint:function(t,e){if(!de(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 re(h,a,e);case"MultiPoint":return i<0&&(i=h.length+i),re(h[i],a,e);case"LineString":return o<0&&(o=h.length+o),re(h[o],a,e);case"Polygon":return n<0&&(n=h.length+n),o<0&&(o=h[n].length+o),re(h[n][o],a,e);case"MultiLineString":return i<0&&(i=h.length+i),o<0&&(o=h[i].length+o),re(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),re(h[i][n][o],a,e)}throw new Error("geojson is invalid")},findSegment:function(t,e){if(!de(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),ie([h[o],h[o+1]],a,e);case"Polygon":return n<0&&(n=h.length+n),o<0&&(o=h[n].length+o-1),ie([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),ie([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),ie([h[i][n][o],h[i][n][o+1]],a,e)}throw new Error("geojson is invalid")},flattenEach:ye,flattenReduce:function(t,e,r){var s=r;return ye(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:fe,geomReduce:function(t,e,r){var s=r;return fe(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 re(e).geometry;case"LineString":return ie(e).geometry;case"Polygon":return se(e).geometry;case"MultiPoint":return ae(e).geometry;case"MultiLineString":return oe(e).geometry;case"MultiPolygon":return he(e).geometry;default:throw new Error(t+" is invalid")}},geometryCollection:function(t,e,r){return void 0===r&&(r={}),ee({type:"GeometryCollection",geometries:t},e,r)},getCoord:Me,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:ve,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}(ce(t,e))},lengthToRadians:ce,lineEach:we,lineReduce:function(t,e,r){var s=r;return we(t,(function(t,i,n,o){s=0===i&&void 0===r?t:e(s,t,i,n,o)})),s},lineString:ie,lineStrings:function(t,e,r){return void 0===r&&(r={}),ne(t.map((function(t){return ie(t,e)})),r)},multiLineString:oe,multiPoint:ae,multiPolygon:he,point:re,points:function(t,e,r){return void 0===r&&(r={}),ne(t.map((function(t){return re(t,e)})),r)},polygon:se,polygons:function(t,e,r){return void 0===r&&(r={}),ne(t.map((function(t){return se(t,e)})),r)},propEach:pe,propReduce:function(t,e,r){var s=r;return pe(t,(function(t,i){s=0===i&&void 0===r?t:e(s,t,i)})),s},radiansToLength:le,segmentEach:xe,segmentReduce:function(t,e,r){var s=r,i=!1;return xe(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 Se extends Array{static get[Symbol.species](){return Se}static fromArray(t){return Object.setPrototypeOf(t,Se.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=Se){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 V(t,((t,r)=>{e[r]=this.props(r,t)})),e}uniq(){return Se.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){h(`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 Se;return mt(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 Se(e);return n.fill(0),this.ask((o=>{const a=t?o[t]:o;if(a<r||a>s)h(`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 rt(this)}sortBy(t,e=!0){return et(this,t,e),this}remove(t,e){const r=this.agentIndex(t,e);return-1!==r?this.splice(r,1):console.log(`remove: ${t.id} not in AgentArray`),this}insert(t,e){const r=this.sortedIndex(t,e);this[r]!==t?this.splice(r,0,t):console.log(`insert: item ${t.id} already in AgentArray`)}sortedIndex(t,e=$){mt(e)&&(e=B(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 J(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,r=!1){if(this.isEmpty())throw Error("min/max OneOf: empty array");"string"==typeof e&&(e=B(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: ${t} ${this.length}`);if(t===this.length)return this;const e=new Se;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 Ce extends Se{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 Ce(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 Ce(this.model),i=e*e,n=F;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 Ce(this.model);return this.ask((o=>{X(o.x,o.y,e,r,s,t.x,t.y)&&(i||t!==o)&&n.push(o)})),n}}class Ee extends Se{model;name;baseSet;AgentClass;static get[Symbol.species](){return Se}constructor(t,e,r,s=null){r.length,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.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 Ee(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 r=0;r<this.length&&this[r].id<=e;r++)t(this[r],r,this)}}class Oe{width;height;data;static emptyDataSet(t,e,r=Array){return new Oe(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 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,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,l=1-a;return this.data[i]*h*l+(this.data[i+1]||0)*o*l+(this.data[i+n]||0)*h*a+(this.data[i+1+n]||0)*o*a}clone(){return new Oe(this.width,this.height,this.data.slice(0))}emptyDataSet(t,e,r=this.dataType()){return Oe.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=Oe.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 Oe(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=zt(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=G(s,t.data);return new Oe(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=w(o,0,this.width-1),a=w(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 l=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[l++]=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,l]=[r.getXY(o,h),s.getXY(o,h)];n.push(Math.atan(Y(0,0,a,l))/t);let c=Math.atan2(-l,-a);e&&c<0&&(c+=2*Math.PI),i.push(c)}return n=new Oe(a,o,n),i=new Oe(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=>M(t,e,n*(r-s))));return r&&(o=o.map((t=>Math.round(t)))),new Oe(this.width,this.height,o)}equals(t){return this.width===t.width&&this.height===t.height&&U(this.data,t.data)}}class Pe{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,Pe.defaults)}newInstance(t){const e=Object.create(t);return Object.assign(e,Pe.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 Se.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 Ae extends Ee{constructor(t,e,r,s=null){super(t,e,r,s)}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 ke{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 ke(ke.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;V({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[m(this.minXcor,this.maxXcor),m(this.minYcor,this.maxYcor)]}random3DPoint(){return[m(this.minXcor,this.maxXcor),m(this.minYcor,this.maxYcor),m(this.minZcor,this.maxZcor)]}randomPatchPoint(){return[u(this.minX,this.maxX),u(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 Ie(t,e,r,s,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: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){It(t,this.numX*e,this.numY*e)}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 Ie{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,l=(t-r)/(n-a),c=(s-e)/(o-h),u=(t+r-l*(n+a))/2,d=(s+e-c*(o+h))/2;Object.assign(this,{mx:l,my:c,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:Te,atan:Re,atan2:je,cos:Xe,floor:Fe,log:Ye,pow:De,sin:Ne,sinh:Le,sqrt:$e,tan:Be,abs:Ue}=Math,We=t=>t*Te/180,qe=t=>180*t/Te;function Ve(t,e){return(t+180)/360*De(2,e)}function Ze(t,e){return Fe(Ve(t,e))}function He(t,e){const r=We(t);return(1-Ye(Be(r)+1/Xe(r))/Te)*De(2,e-1)}function Ge(t,e){return Fe(He(t,e))}function Je(t,e,r){return[Ve(t,r),He(e,r)]}function Qe(t,e,r){return[Ze(t,r),Ge(e,r)]}function Ke(t,e){return t/De(2,e)*360-180}function tr(t,e){const r=Re(Le(Te-2*Te*t/De(2,e)));return qe(r)}function er(t,e,r){return[Ke(t,r),tr(e,r)]}function rr(t,e,r,s=null){const[i,n]=er(t,e,r),[o,a]=er(t+1,e+1,r);return s?g([i,a,o,n],s):[i,a,o,n]}function sr(t){const[e,r,s,i]=t;return[(e+s)/2,(r+i)/2]}function ir(t){const[e,r,s,i]=t;return[[e,i],[s,i],[s,r],[e,r]]}function nr(t,e={}){const r=ir(t);return r.push(r[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[r]},properties:e}}function or(t,e=1,r=e){let[s,i]=t;return[s-e,i-r,s+e,i+r]}const ar=[-105.978,35.66],hr=or(ar,.2,.1),lr=[-109.050044,31.332301,-103.001964,37.000104],cr=sr(lr),ur=[-124.733174,24.544701,-66.949895,49.384358],dr=sr(ur);function mr(t){const[e,r,s,i]=t;return[Ue(e-s),Ue(i-r)]}function pr(t){const[e,r,s,i]=t,n=[e,i],o=[e,r];return[yr(n,[s,i]),yr(n,o)]}function gr(t){const[e,r]=pr(t);return e/r}function fr(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 yr(t,e){const[r,s]=t.map((t=>We(t))),[i,n]=e.map((t=>We(t))),o=i-r,a=Ne((n-s)/2)**2+Xe(s)*Xe(n)*Ne(o/2)**2;return 1e3*(6378.137*(2*je($e(a),$e(1-a))))}var xr=Object.freeze({__proto__:null,Lbounds2bbox:function(t){let{lng:e,lat:r}=t.getNorthWest(),{lng:s,lat:i}=t.getSouthEast();return[e,i,s,r]},attribution:function(t="osm"){const e="Map data © ";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,r,s,i]=t,n=fr(r,e,i,s);return await fetch(n).then((t=>t.json()))},bboxAspect:function(t){const[e,r]=mr(t);return e/r},bboxBounds:function(t){const[e,r,s,i]=t;return[[e,i],[s,r]]},bboxCenter:sr,bboxCoords:ir,bboxFeature:nr,bboxFromCenter:or,bboxMetricAspect:gr,bboxMetricSize:pr,bboxSize:mr,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:fr,latlon:function t(e){return"number"!=typeof e[0]?e.map((e=>t(e))):[e[1],e[0]]},latz2y:Ge,latz2yFloat:He,lonLat2meters:yr,lonLatz2bbox:function(t,e,r){const[s,i]=Qe(t,e,r);return rr(s,i,r)},lonlatz2xy:Qe,lonlatz2xyFloat:Je,lonz2x:Ze,lonz2xFloat:Ve,newMexicoBBox:lr,newMexicoCenter:cr,santaFeBBox:hr,santaFeCenter:ar,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[r,s,i,n]=t,[o,a]=Qe(r,n,e);let[h,l]=Je(i,s,e);return h=Fe(h),l=Number.isInteger(l)?l-1:Fe(l),[o,l,h,a]},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