Skip to content

Commit

Permalink
Priority refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
KirkMcDonald committed Sep 26, 2024
1 parent e05da09 commit 65f4a9d
Show file tree
Hide file tree
Showing 6 changed files with 422 additions and 266 deletions.
52 changes: 39 additions & 13 deletions data/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4604,55 +4604,81 @@
"resources": [
{
"key_name": "iron-ore",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 307
},
{
"key_name": "copper-ore",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 123
},
{
"key_name": "coal",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 141
},
{
"key_name": "limestone",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 233
},
{
"key_name": "caterium-ore",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 50
},
{
"key_name": "sulfur",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 36
},
{
"key_name": "raw-quartz",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 45
},
{
"key_name": "crude-oil",
"category": "oil"
"category": "oil",
"priority": 1,
"weight": 42
},
{
"key_name": "bauxite",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 41
},
{
"key_name": "uranium",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 7
},
{
"key_name": "water",
"category": "water"
"category": "water",
"priority": 0,
"weight": 100
},
{
"key_name": "nitrogen-gas",
"category": "water"
"category": "water",
"priority": 1,
"weight": 40
},
{
"key_name": "sam",
"category": "mineral"
"category": "mineral",
"priority": 1,
"weight": 34
}
]
}
122 changes: 8 additions & 114 deletions factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Formatter } from "./align.js"
import { renderDebug } from "./debug.js"
import { displayItems } from "./display.js"
import { formatSettings } from "./fragment.js"
import { PriorityList } from "./priority.js"
import { Rational, zero, half, one } from "./rational.js"
import { DisabledRecipe } from "./recipe.js"
import { solve } from "./solve.js"
Expand All @@ -35,42 +36,6 @@ export let DEFAULT_PURITY = resourcePurities[1]

export let DEFAULT_BELT = "belt1"

// higher in list == harder to acquire
// Broadly speaking, corresponds to tech tree.
// Much of this order is arbitrary; don't read too much into it.
// This strictly relates to acquisition as a resource directly from the world.
// If an item can be acquired from crafting via lower-priority items, then the
// solution will prefer the craft over harvesting the resource.
const DEFAULT_PRIORITY = [
["Iron Ore", "Copper Ore", "Limestone"],
["Coal", "Water"],
["Caterium Ore", "Sulfur", "Raw Quartz"],
["Crude Oil"],
["Bauxite"],
["Uranium"],
]

class PriorityLevel {
constructor() {
this.recipes = new Set()
}
getRecipeArray() {
return Array.from(this.recipes)
}
add(recipe) {
this.recipes.add(recipe)
}
remove(recipe) {
this.recipes.delete(recipe)
}
has(recipe) {
return this.recipes.has(recipe)
}
isEmpty() {
return this.recipes.size == 0
}
}

class FactorySpecification {
constructor() {
// Game data definitions
Expand All @@ -96,7 +61,8 @@ class FactorySpecification {
this.disable = new Set()
this.defaultDisable = new Set()

this.priority = []
this.priority = null
this.defaultPriority = null

this.format = new Formatter()

Expand Down Expand Up @@ -172,6 +138,8 @@ class FactorySpecification {
this.belts = belts
this.belt = belts.get(DEFAULT_BELT)
this.initMinerSettings()
this.defaultPriority = PriorityList.getDefault(recipes)
this.priority = null
}
setDefaultDisable() {
this.disable.clear()
Expand All @@ -197,87 +165,13 @@ class FactorySpecification {
this.disable.delete(recipe)
}
setDefaultPriority() {
let tiers = []
for (let tierNames of DEFAULT_PRIORITY) {
let tier = []
for (let name of tierNames) {
tier.push(this.resourceNameMap.get(name).key)
}
tiers.push(tier)
}
this.setPriorities(tiers)
this.priority = this.defaultPriority.copy()
}
setPriorities(tiers) {
this.priority = []
for (let tier of tiers) {
let tierList = new PriorityLevel()
for (let key of tier) {
let recipe = this.recipes.get(key)
if (!recipe) {
throw new Error("bad resource key: " + key)
}
tierList.add(recipe)
}
this.priority.push(tierList)
}
this.priority.applyKeys(tiers, this.recipes)
}
isDefaultPriority() {
if (this.priority.length !== DEFAULT_PRIORITY.length) {
return false
}
for (let i = 0; i < this.priority.length; i++) {
let pri = this.priority[i]
let def = DEFAULT_PRIORITY[i]
if (pri.recipes.size !== def.length) {
return false
}
for (let name of def) {
if (!pri.has(this.resourceNameMap.get(name))) {
return false
}
}
}
return true
}
// Moves recipe to the given priority level. If the recipe's old
// priority is empty as a result, removes it and returns true. Returns
// false otherwise.
setPriority(recipe, priority) {
let oldPriority = null
let i = 0
for (; i < this.priority.length; i++) {
let p = this.priority[i]
if (p.has(recipe)) {
oldPriority = p
break
}
}
oldPriority.remove(recipe)
priority.add(recipe)
if (oldPriority.isEmpty()) {
this.priority.splice(i, 1)
return true
}
return false
}
// Creates a new priority level immediately preceding the given one.
// If the given priority is null, adds the new priority to the end of
// the priority list.
//
// Returns the new PriorityLevel.
addPriorityBefore(priority) {
let newPriority = new PriorityLevel()
if (priority === null) {
this.priority.push(newPriority)
} else {
for (let i = 0; i < this.priority.length; i++) {
if (this.priority[i] === priority) {
this.priority.splice(i, 0, newPriority)
break
}
}
}
return newPriority
return this.priority.equal(this.defaultPriority)
}
initMinerSettings() {
this.minerSettings = new Map()
Expand Down
2 changes: 1 addition & 1 deletion fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function formatSettings() {

if (!spec.isDefaultPriority()) {
let priority = []
for (let tier of spec.priority) {
for (let tier of spec.priority.priority) {
let keys = []
for (let p of tier.recipes) {
keys.push(p.key)
Expand Down
Loading

0 comments on commit 65f4a9d

Please sign in to comment.