Ord.io Logo
Ord.io
Inscription

64,529,011

/*๐Ÿ‡ฟed's Tree v0 */
export class ZedTreeV0 {
  constructor(id3, paintbox, calc, builder, architect, recursor, speed, stylist, sat) {
    this.paintbox = paintbox;
    this.calc = new calc();
    this.architect = new architect(builder);
    this.stylist = stylist;
    this.data = null;
    this.sat = sat;
    this.leaves = null;
    this.recursor = new recursor();
    this.totalDepth = null;
    this.speed = speed;
    this.id3 = id3;
    this.calendar = null;
    this.mirror = null;
    this.glow = null;
  }
  /*augmentData*/
  augmentData(rawData, depth = 0) {
    if (rawData.children.length === 0 || rawData.content == "โ†”๏ธ") return {
      ...rawData,
      depth,
      descendants: 0
    };
    const axe = rawData.children.reduce((acc, child, index) => {
      return child.content === "๐Ÿชš" ? index : acc;
    }, -1);
    const filtered = rawData.children.filter((_, index) => index > axe);
    let total = 0;
    const newChildren = filtered.map(child => {
      const newChild = this.augmentData(child, depth + 1);
      total += newChild.descendants + 1;
      return newChild;
    });
    return {
      ...rawData,
      depth,
      descendants: total,
      children: newChildren
    };
  }
  /*max*/
  max(data, depth = 0) {
    if (data.children.length === 0) return depth;
    let max = depth;
    for (const child of data.children) {
      const childDepth = this.max(child, depth + 1);
      if (childDepth > max) max = childDepth;
    }
    return max;
  }
  /*descendants*/
  descendants(data) {
    let total = 0;
    for (const child of data.children) {
      total += 1 + this.descendants(child)
    }
    return total;
  }
  /*mother*/
  async mother(children, max = 0, iid = null) {
    for (const childIid of children) {
      this.data = await this.collect(childIid);
      let count = this.descendants(this.data);
      if (count > max) {
        max = count;
        iid = childIid;
      }
    }
    return iid;
  }
  /*build*/
  async build(calendar, mirror, glow = false, test = false, leaves, rawData = null) {
    this.calendar = calendar;
    this.mirror = mirror;
    this.glow = glow;
    this.leaves = leaves;
    if (test) {
      this.data = await this.randTree(10, 2000, 2)
    } else {
      let sat = `${this.sat.prefix}${this.id3}999`;
      let iid = await this.recursor.inscription(sat, 0);
      if (this.id3 == "000") {
        const parentIid = await this.recursor.inscription(this.sat.parent, 0);
        const children = await this.recursor.children(parentIid);
        iid = await this.mother(children);
      }
      this.data = await this.collect(iid);
    }
    this.totalDepth = this.max(this.data);
    return this.augmentData(this.data);
  }
  /*collect*/
  async collect(iid) {
    return await this.recursor.collect(iid, {
      rules: [this.recursor.fsize, this.recursor.ftype],
      config: {
        maxSize: 20,
        typePrefix: 'text'
      }
    }, {
      rules: [this.recursor.matcher],
      config: {
        matchArray: ['๐ŸคŽ', '๐Ÿ’›', '๐Ÿงก', 'โค๏ธ', '๐Ÿฉท', '๐Ÿ’œ', '๐Ÿ’š', '๐Ÿฉต', '๐Ÿ’™', '๐Ÿ–ค', '๐Ÿฉถ', '๐Ÿค', 'โ†”๏ธ', 'โœ‚๏ธ', '๐Ÿชš']
      }
    }, true);
  }
  /*draw*/
  async draw(layers, data, x, y, a, z, gpcol) {
    if (data.depth > this.totalDepth || data.content == "โ†”๏ธ") return;
    setTimeout(async () => {
      await this.calc.set(data.iid.slice(0, -2));
      z = Math.min(Math.max(z, Math.PI * .25), Math.PI * .75);
      const dsc = data.descendants + 1 + this.calc.get() * .1;
      const dp = data.depth + 1 + this.calc.get() * .1;
      const len = z * dsc ** .05 * dp ** -.4 * 48;
      const t = z * dsc ** .5 * dp ** -.2 * 2;
      const leafMod = dsc ** .3 * dp ** -.45 * .15;
      let endX, endY, mixcol, col;
      if (data.depth == 0) {
        endX = x;
        endY = y;
        mixcol = null;
      } else {
        endX = x + Math.cos(a) * len; //*.9;
        endY = y - Math.sin(a) * Math.sin(z) * len; //*1.1;
        col = {
          ...this.paintbox.hearts[data.content],
          a: 1
        };
        if (gpcol) {
          if (data.content == "โœ‚๏ธ") {
            col = gpcol
          }
          mixcol = this.paintbox.avg(gpcol, col, .7);
        } else {
          if (data.content == "โœ‚๏ธ") {
            col = {
              r: 0,
              g: 0,
              b: 0,
              a: 0
            }
          };
          mixcol = col;
        }
        const initBranchCol = this.paintbox.avg(this.paintbox.ink(1), mixcol, dp ** -.2);
        const branch = {
          glow: this.paintbox.obj2str(this.paintbox.avg(this.paintbox.btc(1), initBranchCol, .8)),
          main: this.paintbox.obj2str(this.paintbox.avg({
            r: 99,
            g: 99,
            b: 99,
            a: .8
          }, initBranchCol, .5)),
          barkBG: this.paintbox.obj2str(this.paintbox.avg({
            r: 99,
            g: 99,
            b: 99,
            a: .5
          }, initBranchCol, (data.depth + 1) ** -.4)),
          bark: this.paintbox.obj2str(this.paintbox.avg({
            r: 22,
            g: 22,
            b: 22,
            a: .5
          }, initBranchCol, (data.depth + 1) ** -.4)),
          shade: this.paintbox.obj2str(this.paintbox.avg({
            r: 22,
            g: 22,
            b: 22,
            a: .5
          }, initBranchCol, .5)),
        };
        if (this.glow) {
          this.stylist.art({
            t: "line",
            l: "glow",
            x1: x - t * .05 * this.mirror,
            y1: y + t * .03 * this.mirror,
            x2: endX - t * .05 * this.mirror,
            y2: endY + t * .03 * this.mirror,
            c1: branch.glow,
            sw: t * 4 * .2
          });
          this.stylist.art({
            t: "circle",
            l: "glow",
            x1: endX - t * .05 * this.mirror,
            y1: endY + t * .03 * this.mirror,
            r1: t * 4 * .1,
            c1: branch.glow
          });
        }
        this.stylist.art({
          t: "line",
          l: "branch",
          x1: x,
          y1: y,
          x2: endX,
          y2: endY,
          c1: branch.main,
          sw: t * 4 * .2
        });
        this.stylist.art({
          t: "circle",
          l: "branch",
          x1: endX,
          y1: endY,
          r1: t * 4 * .1,
          c1: branch.main
        });
        for (var i = 3; i >= 0; i--) {
          this.stylist.art({
            t: "line",
            l: "shade",
            x1: x + t * (4 - i) * .07 * this.mirror,
            y1: y - t * (4 - i) * .05 * this.mirror,
            x2: endX + t * (4 - i) * .07 * this.mirror,
            y2: endY - t * (4 - i) * .05 * this.mirror,
            c1: branch.shade,
            sw: t * i * .2
          });
          this.stylist.art({
            t: "circle",
            l: "shade",
            x1: endX + t * (4 - i) * .07 * this.mirror,
            y1: endY - t * (4 - i) * .05 * this.mirror,
            r1: t * i * .1,
            c1: branch.shade
          });
        };
        this.stylist.art({
          t: "line",
          l: "barkBG",
          x1: x - .18 * t,
          y1: y,
          x2: endX - .18 * t,
          y2: endY,
          c1: branch.barkBG,
          sw: t ** 1.5 * .025
        });
        this.stylist.art({
          t: "line",
          l: "barkBG",
          x1: x + .18 * t,
          y1: y,
          x2: endX + .18 * t,
          y2: endY,
          c1: branch.barkBG,
          sw: t ** 1.5 * .025
        });
        this.stylist.art({
          t: "line",
          l: "bark",
          x1: x - .2 * t,
          y1: y,
          x2: endX - .2 * t,
          y2: endY,
          c1: branch.bark,
          sw: t ** 1.5 * .025
        });
        this.stylist.art({
          t: "line",
          l: "bark",
          x1: x + .2 * t,
          y1: y,
          x2: endX + .2 * t,
          y2: endY,
          c1: branch.bark,
          sw: t ** 1.5 * .025
        });
      }
      const node = this.stylist.art({
        t: "text",
        l: "nodes",
        x1: endX,
        y1: endY,
        t1: data.content,
        r1: 10,
        c1: 'black'
      });
      node.style.cursor = 'pointer';
      const title = this.stylist.artist.builder.element('title', {});
      title.textContent = `๐Ÿ“‹ ${data.iid}`;
      node.appendChild(title);
      node.addEventListener('click', function() {
        navigator.clipboard.writeText(text).then(function() {
          console.log('Copied: ' + data.iid)
        });
      });
      if (data.content && data.children.length === 0 && data.content != "โœ‚๏ธ" && this.calc.get() + .5 < this.calendar.warmth()) {
        this.leaves.draw(col, mixcol, endX, endY, z, leafMod);
      } else {
        data.children.forEach((child, i) => {
          let newAngle = a + Math.PI / 2 - Math.PI / (data.children.length + 1) * (i + 1);
          z += this.calc.get() * .3;
          newAngle += this.calc.get() * .3;
          const offVert = Math.abs(newAngle - Math.PI / 2);
          newAngle += (newAngle > Math.PI / 2 ? -1 : 1) * offVert * .1;
          this.draw(layers, child, endX, endY, newAngle, z, mixcol);
        });
      }
    }, data.depth * this.speed);
  }
  /*randEmoji*/
  randEmoji() {
    const emojis = ['๐Ÿฉต', '๐Ÿ’™'];
    return emojis[Math.floor(Math.random() * emojis.length)];
  }
  /*randTree*/
  async randTree(maxDepth, totalEnds, maxChildren) {
    const createNode = async (emoji) => {
      return {
        iid: await this.calc.hasher(Math.random()) + "i0",
        content: emoji,
        children: []
      }
    }
    const tree = await createNode();

    function findNodesAtDepth(node, depth, currentDepth = 0) {
      if (currentDepth === depth) {
        if (node.children.length < maxChildren) {
          return [node]
        } else {
          return []
        }
      }
      let nodes = [];
      for (let child of node.children) {
        nodes = nodes.concat(findNodesAtDepth(child, depth, currentDepth + 1));
      }
      return nodes;
    }
    for (let depth = 0; depth < maxDepth && totalEnds > 0; depth++) {
      let depthNodes = findNodesAtDepth(tree, depth);
      while (depthNodes.length > 0 && totalEnds > 0) {
        const nodeIndex = Math.floor(Math.random() * depthNodes.length);
        const currentNode = depthNodes[nodeIndex];
        let numChildren = 0;
        if (Math.random() < .9) numChildren = 1;
        numChildren = Math.min(numChildren, totalEnds, maxChildren - currentNode.children.length);
        for (let i = 0; i < numChildren && depth < maxDepth - 1; i++) {
          let random = Math.random();
          if (random < .01) {
            currentNode.children.push(await createNode("๐Ÿชš"));
          } else if (random < .03) {
            currentNode.children.push(await createNode("โœ‚๏ธ"));
          } else if (random < .1) {
            currentNode.children.push(await createNode("โ†”๏ธ"));
          } else {
            currentNode.children.push(await createNode(this.randEmoji()));
          }
          totalEnds--;
          if (totalEnds <= 0) break;
        }
        if (numChildren === 0 || depth === maxDepth - 1) depthNodes.splice(nodeIndex, 1);
      }
    }
    return tree;
  }
}

No replies yet

  • ID

    97d0f...df6i5

  • Owned By
  • File Type

    JAVASCRIPTtext/javascript

  • File Size

    7.343 KB

  • Created

    March 15, 2024, 9:53 PM UTC

    1 year ago

  • Creation Block
  • Creation Transaction
  • Creation Fee

    22,323sats

  • Tags

    No tags yet

Sat

Parent Inscription