Skip to content

Commit

Permalink
Merge pull request #249 from KirkMcDonald/rewrite-merge
Browse files Browse the repository at this point in the history
Rewrite merge
  • Loading branch information
KirkMcDonald authored Oct 16, 2024
2 parents c7df837 + cbbb2bc commit 34814e9
Show file tree
Hide file tree
Showing 67 changed files with 9,043 additions and 9,872 deletions.
1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

88 changes: 88 additions & 0 deletions align.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*Copyright 2019 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
import { Rational, one } from "./rational.js"

export const DEFAULT_RATE = "m"
export const DEFAULT_RATE_PRECISION = 3
export const DEFAULT_COUNT_PRECISION = 1
export const DEFAULT_FORMAT = "decimal"

let seconds = one
let minutes = Rational.from_float(60)
let hours = Rational.from_float(3600)

let displayRates = new Map([
["s", seconds],
["m", minutes],
["h", hours],
])

export let longRateNames = new Map([
["s", "second"],
["m", "minute"],
["h", "hour"],
])

export class Formatter {
constructor() {
this.setDisplayRate(DEFAULT_RATE)
this.displayFormat = "decimal"
this.ratePrecision = DEFAULT_RATE_PRECISION
this.countPrecision = DEFAULT_COUNT_PRECISION
}
setDisplayRate(rate) {
this.rateName = rate
this.longRate = longRateNames.get(rate)
this.rateFactor = displayRates.get(rate)
}
align(s, prec) {
if (this.displayFormat === "rational") {
return s
}
let idx = s.indexOf(".")
if (idx === -1) {
idx = s.length
}
let toAdd = prec - s.length + idx
if (prec > 0) {
toAdd += 1
}
while (toAdd > 0) {
s += "\u00A0"
toAdd--
}
return s
}
rate(rate) {
rate = rate.mul(this.rateFactor)
if (this.displayFormat === "rational") {
return rate.toMixed()
} else {
return rate.toDecimal(this.ratePrecision)
}
}
alignRate(rate) {
return this.align(this.rate(rate), this.ratePrecision)
}
count(count) {
if (this.displayFormat === "rational") {
return count.toMixed()
} else {
return count.toUpDecimal(this.countPrecision)
}
}
alignCount(count) {
return this.align(this.count(count), this.countPrecision)
}
}
70 changes: 46 additions & 24 deletions belt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*Copyright 2015-2019 Kirk McDonald
/*Copyright 2019-2021 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -11,41 +11,63 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
"use strict"
import { Icon } from "./icon.js"
import { Rational } from "./rational.js"

function Belt(name, speed) {
this.name = name
this.speed = speed
class Belt {
constructor(key, name, col, row, rate) {
this.key = key
this.name = name
this.rate = rate
this.icon_col = col
this.icon_row = row
this.icon = new Icon(this)
}
renderTooltip() {
let self = this
let t = d3.create("div")
.classed("frame", true)
let header = t.append("h3")
header.append(() => self.icon.make(32, true))
header.append(() => new Text(self.name))
t.append("b")
.text(`Max throughput: `)
t.append(() => new Text(`${spec.format.rate(this.rate)}/${spec.format.longRate}`))
return t.node()
}
}

function getBelts(data) {
var beltData = data["transport-belt"]
var beltObjs = []
for (var beltName in beltData) {
var beltInfo = beltData[beltName]
export function getBelts(data) {
let beltData = data["transport-belt"]
let beltObjs = []
for (let beltName in beltData) {
let beltInfo = beltData[beltName]
// Belt speed is given in tiles/tick, which we can convert to
// items/second as follows:
// tiles ticks 32 pixels/tile
// speed ----- * 60 ------ * 2 lanes * --------------
// tick second 9 pixels/item
// 0.17 changes this formula from 9 pixels/item to 8 pixels/item.
var baseSpeed = RationalFromFloat(beltInfo.speed)
var pixelsPerSecond = baseSpeed.mul(RationalFromFloat(3840))
var speed
if (useLegacyCalculations) {
speed = pixelsPerSecond.div(RationalFromFloat(9))
} else {
speed = pixelsPerSecond.div(RationalFromFloat(8))
}
beltObjs.push(new Belt(beltName, speed))
// tick second 8 pixels/item
let baseSpeed = Rational.from_float_approximate(beltInfo.speed)
let speed = baseSpeed.mul(Rational.from_float(480))
beltObjs.push(new Belt(
beltName,
beltInfo.localized_name.en,
beltInfo.icon_col,
beltInfo.icon_row,
speed,
))
}
beltObjs.sort(function(a, b) {
if (a.speed.less(b.speed)) {
if (a.rate.less(b.rate)) {
return -1
} else if (b.speed.less(a.speed)) {
} else if (b.rate.less(a.rate)) {
return 1
}
return 0
})
return beltObjs
let belts = new Map()
for (let belt of beltObjs) {
belts.set(belt.key, belt)
}
return belts
}
Loading

0 comments on commit 34814e9

Please sign in to comment.