diff --git a/src/arcologyBuilding/base.js b/src/arcologyBuilding/base.js index 160edfda4beb7710c9e754468e22ac2fb51cde17..df7d221c99160c7e1104c12c218bc0bf91347710 100644 --- a/src/arcologyBuilding/base.js +++ b/src/arcologyBuilding/base.js @@ -39,91 +39,12 @@ App.Arcology.updateOwnership = function() { * @returns {App.Arcology.Building} */ App.Arcology.defaultBuilding = function(location = "default") { - const sections = []; - - sections.push(new App.Arcology.Section("penthouse", [[new App.Arcology.Cell.Penthouse()]])); - sections.push(shops()); - sections.push(apartments()); - sections.push(markets()); - sections.push(manufacturing()); - - - function shops() { - const rows = []; - if (location === "marine") { - rows.push([new App.Arcology.Cell.Shop(1), new App.Arcology.Cell.Shop(1), new App.Arcology.Cell.Shop(1), new App.Arcology.Cell.Shop(1)]); - } else { - rows.push([new App.Arcology.Cell.Shop(1), new App.Arcology.Cell.Shop(1), new App.Arcology.Cell.Shop(1)]); - } - return new App.Arcology.Section("shops", rows); - } - - function apartments() { - const rows = []; - if (location === "rural") { - rows.push([new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1)]); - rows.push([new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1)]); - } else { - if (location === "marine") { - rows.push([new App.Arcology.Cell.Apartment(1, 1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1, 1)]); - } else if (location === "oceanic") { - rows.push([new App.Arcology.Cell.Apartment(1, 1), new App.Arcology.Cell.Apartment(1, 1), new App.Arcology.Cell.Apartment(1, 1), new App.Arcology.Cell.Apartment(1, 1)]); - } else { - rows.push([new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1)]); - } - rows.push([new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1)]); - - if (location === "urban") { - rows.push([new App.Arcology.Cell.Apartment(1, 3), new App.Arcology.Cell.Apartment(1, 3), new App.Arcology.Cell.Apartment(1, 3), new App.Arcology.Cell.Apartment(1, 3)]); - } else { - rows.push([new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1), new App.Arcology.Cell.Apartment(1)]); - } - } - return new App.Arcology.Section("apartments", rows); - } - - function markets() { - if (location === "ravine") { - return new App.Arcology.Section("ravine-markets", [[new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1)]], true); - } - const rows = []; - if (location === "urban") { - rows.push([convertableCell(new App.Arcology.Cell.Apartment(1, 3), ["Apartment", "Market"]), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), convertableCell(new App.Arcology.Cell.Apartment(1, 3), ["Apartment", "Market"])]); - rows.push([new App.Arcology.Cell.Market(1), convertableCell(new App.Arcology.Cell.Market(1), ["Market", "Manufacturing"]), convertableCell(new App.Arcology.Cell.Market(1), ["Market", "Manufacturing"]), new App.Arcology.Cell.Market(1)]); - } else if (location === "rural") { - rows.push([new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1)]); - } else if (location === "marine") { - rows.push([new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), convertableCell(new App.Arcology.Cell.Manufacturing(1), ["Market", "Manufacturing"]), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1)]); - } else { - rows.push([new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1), new App.Arcology.Cell.Market(1)]); - } - return new App.Arcology.Section("markets", rows, true); - } - - function manufacturing() { - const rows = []; - if (location === "urban") { - rows.push([new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1)]); - } else if (location === "rural") { - rows.push([new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1)]); - } else if (location === "marine") { - rows.push([convertableCell(new App.Arcology.Cell.Market(1), ["Market", "Manufacturing"]), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), convertableCell(new App.Arcology.Cell.Market(1), ["Market", "Manufacturing"])]); - } else { - rows.push([new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1), new App.Arcology.Cell.Manufacturing(1)]); - } - return new App.Arcology.Section("manufacturing", rows); - } - /** - * @param {App.Arcology.Cell.BaseCell} cell - * @param {Array<string>} conversions must include the initial cell + * @type {arcologyEnvironment} */ - function convertableCell(cell, conversions) { - cell.allowedConversions = conversions; - return cell; - } - - return new App.Arcology.Building(sections); + const env = {location: location, established: false, fs: ""}; + const candidates = App.Arcology.presets.filter(preset => preset.isAllowed(env)); + return candidates.pluck().construct(env); }; /** diff --git a/src/arcologyBuilding/presets.js b/src/arcologyBuilding/presets.js new file mode 100644 index 0000000000000000000000000000000000000000..517507a859e08d2261f7ff6bb453827b62309370 --- /dev/null +++ b/src/arcologyBuilding/presets.js @@ -0,0 +1,194 @@ +/** + * @typedef {object} arcologyEnvironment + * @property {string} location + * @property {boolean} established + * @property {string} fs + */ +/** + * @typedef {object} buildingPreset + * @property {function(arcologyEnvironment):boolean} isAllowed + * @property {function(arcologyEnvironment):App.Arcology.Building} construct + * @property {function():void} apply + */ + +/** + * @type {Array<buildingPreset>} + */ +App.Arcology.presets = (function() { + /** + * @typedef {object} sectionTemplate + * @property {string} id + * @property {Array<string>} rows + * @property {boolean} [ground] + */ + /** + * @typedef {Array<sectionTemplate>} buildingTemplate + */ + + /* NOTE: test new templates, broken templates WILL explode */ + /* t is markets, () is possible values for a sector, first is default */ + const templates = { + default: [{id: "penthouse", rows: ["p"]}, + {id: "shops", rows: ["sss"]}, + {id: "apartments", rows: ["aaaa", "aaaa", "aaaa"]}, + {id: "markets", rows: ["ttttt"], ground: true}, + {id: "manufacturing", rows: ["mmmmm"]}], + urban: [{id: "penthouse", rows: ["p"]}, + {id: "shops", rows: ["sss"]}, + {id: "apartments", rows: ["aaaa", "aaaa", "dddd"]}, + {id: "markets", rows: ["(dt)tt(dt)", "t(tm)(tm)t"], ground: true}, + {id: "manufacturing", rows: ["mmmm"]}], + rural: [{id: "penthouse", rows: ["p"]}, + {id: "shops", rows: ["sss"]}, + {id: "apartments", rows: ["aaaaa", "aaaaa"]}, + {id: "markets", rows: ["tt(mt)(mt)tt"], ground: true}, + {id: "manufacturing", rows: ["mmmmm"]}], + ravine: [{id: "penthouse", rows: ["p"]}, + {id: "shops", rows: ["sss"]}, + {id: "ravine-markets", rows: ["ttttt"], ground: true}, + {id: "apartments", rows: ["aaaa", "aaaa", "aaaa"]}, + {id: "manufacturing", rows: ["mmmmm"]}], + marine: [{id: "penthouse", rows: ["p"]}, + {id: "shops", rows: ["ssss"]}, + {id: "apartments", rows: ["laal", "aaaa", "aaaa"]}, + {id: "markets", rows: ["tt(mt)tt"], ground: true}, + {id: "manufacturing", rows: ["(tm)mmm(tm)"]}], + oceanic: [{id: "penthouse", rows: ["p"]}, + {id: "shops", rows: ["sss"]}, + {id: "apartments", rows: ["llll", "aaaa", "aaaa"]}, + {id: "markets", rows: ["ttttt"], ground: true}, + {id: "manufacturing", rows: ["mmmmm"]}], + }; + + /** + * @param {buildingTemplate} template + * @returns {App.Arcology.Building} + */ + function templateToBuilding(template) { + const sections = []; + for (let sectionTemplate of template) { + sections.push(getSection(sectionTemplate)); + } + return new App.Arcology.Building(sections); + + /** + * @param {sectionTemplate} section + * @returns {App.Arcology.Section} + */ + function getSection(section) { + const rows = []; + for (const row of section.rows) { + rows.push(getRow(row)); + } + return new App.Arcology.Section(section.id, rows, section.ground === true /* to ensure no undefined gets trough */); + } + + /** + * @param {string} rowTemplate + * @returns {[App.Arcology.Cell.BaseCell]} + */ + function getRow(rowTemplate) { + const cells = []; + const iter = rowTemplate[Symbol.iterator](); + + let next = iter.next(); + while (!next.done) { + if (next.value === "(") { + const cell = charToCell(iter.next().value).cell; + next = iter.next(); + while (next.value !== ")") { + cell.allowedConversions.push(charToCell(next.value).code); + next = iter.next(); + } + cells.push(cell); + } else { + cells.push(charToCell(next.value).cell); + } + next = iter.next(); + } + + return cells; + } + + /** + * @param {string} char + * @returns {{cell: App.Arcology.Cell.BaseCell, code:string}} + */ + function charToCell(char) { + switch (char) { + case "p": + return {cell: new App.Arcology.Cell.Penthouse(), code: "Penthouse"}; + case "s": + return {cell: new App.Arcology.Cell.Shop(1), code: "Shop"}; + case "l": + return {cell: new App.Arcology.Cell.Apartment(1, 1), code: "Apartment"}; + case "a": + return {cell: new App.Arcology.Cell.Apartment(1), code: "Apartment"}; + case "d": + return {cell: new App.Arcology.Cell.Apartment(1, 3), code: "Apartment"}; + case "t": + return {cell: new App.Arcology.Cell.Market(1), code: "Market"}; + case "m": + return {cell: new App.Arcology.Cell.Manufacturing(1), code: "Manufacturing"}; + default: + return {cell: new App.Arcology.Cell.BaseCell(1), code: "BaseCell"}; + } + } + } + + return [ + /* basic types for controlled start */ + { + isAllowed: env => !env.established && env.location === "default", + construct: () => templateToBuilding(templates.default), + apply() {} + }, { + isAllowed: env => !env.established && env.location === "urban", + construct: () => templateToBuilding(templates.urban), + apply() {} + }, { + isAllowed: env => !env.established && env.location === "rural", + construct: () => templateToBuilding(templates.rural), + apply() {} + }, { + isAllowed: env => !env.established && env.location === "ravine", + construct: () => templateToBuilding(templates.ravine), + apply() {} + }, { + isAllowed: env => !env.established && env.location === "marine", + construct: () => templateToBuilding(templates.marine), + apply() {} + }, { + isAllowed: env => !env.established && env.location === "oceanic", + construct: () => templateToBuilding(templates.oceanic), + apply() {} + }, + /* crazy presets for established arcologies TODO */ + { + isAllowed: env => env.established && env.location === "default", + construct() { return templateToBuilding(templates.default); }, + apply() {} + }, { + isAllowed: env => env.established && env.location === "urban", + construct() { return templateToBuilding(templates.urban); }, + apply() {} + }, { + isAllowed: env => env.established && env.location === "rural", + construct() { return templateToBuilding(templates.rural); }, + apply() {} + }, { + isAllowed: env => env.established && env.location === "ravine", + construct() { return templateToBuilding(templates.ravine); }, + apply() {} + }, { + isAllowed: env => env.established && env.location === "marine", + construct() { return templateToBuilding(templates.marine); }, + apply() {} + }, { + isAllowed: env => env.established && env.location === "oceanic", + construct() { return templateToBuilding(templates.oceanic); }, + apply() {} + }, + ]; +}()); +