[HN Gopher] Functorio
___________________________________________________________________
Functorio
Author : ibobev
Score : 281 points
Date : 2021-02-16 19:50 UTC (3 hours ago)
(HTM) web link (bartoszmilewski.com)
(TXT) w3m dump (bartoszmilewski.com)
| jzer0cool wrote:
| This is a fun read. Learn Haskell, functional programming, and
| game design all in one.
| pul wrote:
| I wrote a similar article explaining Kafka with Factorio
| analogies a while back:
| https://medium.com/@ruurtjan/understanding-kafka-with-factor...
|
| Visually explaining abstract concepts works a lot better in
| building an intuition than explaining them in abstract terms in
| my experience.
| LukeEF wrote:
| I started to dream in assemblers. And belts. Endless belts.
| SuchAnonMuchWow wrote:
| To me, Factorio looks more like dataflow programming
| (https://en.wikipedia.org/wiki/Dataflow_programming) much more
| than functional programming, but I guess people see much more
| similitude with what they are used to.
| Groxx wrote:
| > _In Factorio, the nesting of functors is drastically limited.
| It's possible to produce belts, and you can put them on belts, so
| you can have a beltful of belts, Belt Belt. Similarly you can
| store chests inside chests. But you can't have belts of loaded
| belts._
|
| You can, however, have _vehicles_ on belts, and they have
| storage: https://youtu.be/JgqT6DEnBGM?t=2082
|
| AFAIK that's where the nesting ends though.
| falcolas wrote:
| An interesting writeup, but <troll>like most oversimplified
| functional programming writeups</troll> they are ignoring a lot
| of external state around Factorio "functions" and "composed
| functions" - things that absolutely need to be considered when
| creating a factory.
|
| For example: energy requirements, inserter speed (and the impact
| of that and inserter stack size on throughput), input/output
| bottlenecks, pollution, efficiency modules (which _really_ mucks
| with the "an assembler is a function" analogy), beacons, and
| probably more.
| Ericson2314 wrote:
| None of those resemble imperative programming though.
|
| Imperative programming would be like instead of having your
| factorio factory, having 1-32 employees running around at
| ludicrously fast speeds doing all the work trying not to crash
| into each other.
| Ericson2314 wrote:
| And microservices is more like normal Factorio, but spending
| a bunch of time designing all the building blocks a few
| redundant ways, and then getting really drunk and blindfolded
| before placing them on the map. And then having your equally
| drunk and blindfolded factory reliability engineers fight off
| the swarming bugs.
| falcolas wrote:
| Which is, funnily enough, roughly how it's programmed.
|
| Each entity has a state, time is advanced, and the threads
| run through their queue of entities and update their state,
| along with the state of connected entities. Since some
| entities interact directly with other entities (such as two
| fluid pipe segments), these queues are ordered (so the fluid
| movement and pressure calculations make sense).
|
| For example, a belt would advance the items on it 1/60th of
| its speed. Inserters would look to see if there's room in the
| destination, if there are items within their reach on the
| belt, and start moving to pick them up. Assemblers would
| check to see if they're currently building something, if
| they're full (the quantity that determines "full" is based on
| whether there's an outgoing inserter attached), if they have
| the necessary ingredients in their incoming buffer, and start
| incrementing the creation timer. The outgoing inserter would
| check for items in the assembler's output, grab them, and so
| forth.
| TameAntelope wrote:
| Yeah, that was the obvious missing piece when I took a look at
| the article, a lack of rate calculations.
|
| Basically the whole game is driven on rate and ratios, not just
| ratios.
| nynx wrote:
| This is kind of unrelated, but it would be so cool if assemblers
| like that existed in real life. It's a "hard problem"
| unfortunately.
| tomaszs wrote:
| I love the way the author explained it. And surely there are
| places where functional programming helps eg. linear processing
| of data, analysis, processing. However in my field, frontend,
| there is more focus on interactions between very short factory
| lines. So functional programming does not shine, and often just
| leads to increased complexity and risk of mistakes.
| revskill wrote:
| Then you can try React Hook
| scubbo wrote:
| I look forward to the inevitable two camps of comments on
| anything related to Factorio: "This is just software engineering,
| why would I do that in my free time?" and "This is just software
| engineering, but without all the distractions, frustrations,
| meetings, and dependencies - how can I do this all the time?"
|
| (FWIW, I'm in the latter camp)
| martindbp wrote:
| There's a third camp: "This is just software engineering, I
| might as well code something useful and equally fun".
| tick_tock_tick wrote:
| ^^^ That's my camp by and large unless I'm playing
| multiplayer with friends it just makes me feel like I should
| be working on my side project.
| recursive wrote:
| The problem with doing something useful is that you might end
| up with users.
| martindbp wrote:
| Good point, but you could make something that's useful for
| yourself, or to learn something new, or even something like
| an art piece. Recently when I had some time I implemented a
| Minesweeper solver. Not useful to anybody, except perhaps
| rural Cambodian farmers, but it was great fun, and probably
| more satisfying than playing a game.
| thomastjeffery wrote:
| "Just" software engineering is a rare thing.
|
| If you want to make real software, you have to do your software
| engineering in the middle of a sandwich with tooling and
| dependencies on one side and your platform on the other.
|
| Dealing with either of these can sometimes be satisfying, but
| they both tend to require a lot more effort than is desired.
| hinkley wrote:
| When SVG was young someone made a graph game where a bunch of
| points are placed on a field. Each point has two edges leading
| to other points. You 'win' the game when no edges cross.
|
| It was pretty fun for the first half hour. Then for the next
| fifteen minutes something didn't feel right. To quote Robert
| Zemeckis, it felt like kissing your brother.
|
| The other thing that was hot around that time was UML, and it
| feels like half of UML is just trying to get lines not to cross
| each other. Ding! I was doing some of my least favorite work
| 'for fun'. Closed that window, pushed away from my desk
| dramatically, and went for a cup of coffee and a bit of
| perspective.
| Arnavion wrote:
| Like Untangle from
| https://www.chiark.greenend.org.uk/~sgtatham/puzzles/ ?
| dmurray wrote:
| Yes, a clone of that, but I suspect GP meant the
| implementation at http://planarity.net/
| willis936 wrote:
| >how can I do this all the time?
|
| Make a factorio-themed reskin of labview.
|
| If it took me 100 hours to complete a coding project of the
| magnitude of a factorio playthrough I would say I'm pretty bad
| at coding.
|
| I'm of the former of your two listed camps, but I don't try to
| look at how the game is like coding and look at it how it is
| like modded minecraft. Learning rules and discovering tricks
| and being creative is a fun way to spend free time. Maybe
| coding can have a similar track, but it often feels heavy on
| the grind.
| falcolas wrote:
| I call it circuit design. "How can I fit this efficiently into
| the space I have allotted and protect it from outside
| influences (biters)?"
| tasogare wrote:
| I never felt space (with normal game settings) was an issue
| in the game. The infinite map even means it's literally
| impossible to run out of space.
| falcolas wrote:
| Space becomes an issue when trying to minimize your
| buffers. During a casual playthrough, it won't really
| matter, but if you're dealing with megabases, the distances
| and buffers can really impact how the smoothly a factory
| works, how quickly it can respond to changes. For example,
| not all of the "infinite research" technologies have the
| same science pack requirements, which changes what your
| base has to produce.
| andrewzah wrote:
| "This is just software engineering, why would I do that in my
| free time?"
|
| I was of this opinion until I realized I was basically doing
| digital logic in minecraft. You're right in that engineering
| without crud is very fun.
| entropie wrote:
| Software engineering with angry aliens.
| KineticLensman wrote:
| That would be me in approx 2006 in Second Life, in-game
| coding using the Linden Scripting Language [0]. I have vague
| recollections of trying to script some sort of scanner object
| and explaining what I was doing to a curious gothic vampire
| demon who was passing by. I think she lost interest (and flew
| away) when I got to quarternions [1]
|
| [0] http://wiki.secondlife.com/wiki/LSL_Portal
|
| [1] http://wiki.secondlife.com/wiki/Quaternion
| scubbo wrote:
| Literal bugs!
| Erwin wrote:
| Angry aliens, or angry end-users?
| test6554 wrote:
| The biters of clan cholg-un tphol have requested a meeting
| about the planned nuclear plant expansion into their sacred
| ancestral territory.
| romaniv wrote:
| In 1986 people wanted to make real programming more like a
| computer game:
|
| https://www.youtube.com/watch?v=I9LZ6TnSP40
|
| Today, when we have much more computational resources, I'm
| reading this article about representing Factorio as static text
| with compiler checks. Something somewhere is off.
| bluefirebrand wrote:
| Probably only off by one, though.
| bmgxyz wrote:
| I wonder how difficult it would be to implement a language for
| specifying Factorio factories without worrying about physical
| layouts. This post provides a lot of good insights into how that
| could be done. Once a factory is specified, a compiler could turn
| the high-level specification into a blueprint or a valid save
| file to be loaded up in the game.
|
| Of course, the compiler would have no way to lay out the factory
| with respect to available resource deposits. Maybe that would
| have to be part of the specification as well? Or maybe the
| programmer (player?) could pass an empty map file as a compiler
| argument.
|
| Also, this doesn't give any consideration to the tech tree or
| enemies within the game. I guess those could be compiler flags,
| as long as we're dreaming.
|
| Look, all I'm saying is that it would be really cool to be able
| to check factories into version control.
| klyrs wrote:
| Such a language could be an extension of VHDL or verilog (or
| more likely, just a library in such a language). The map could
| plausibly be the equivalent of an FPGA architecture, and build
| upon existing tooling. Caveat: I'm a bit of an FPGA noob and
| have zero clue how one specifies a detailed FPGA fabric to such
| tooling.
| MayeulC wrote:
| No, you are right. One would likely need to define more logic
| types to be fully accurate, but those could be abstracted
| away.
|
| Just connect blocks A and B in some hardware description
| language. Further describe B as an assembly of blocks A and
| C.
|
| Ask a compiler such as yosys to flatten your hierarchy down
| to a set of primitive blocks (say, A and C). Once you have
| the diagram, use a custom placer and router to position it.
|
| There might be additional constraints while placing blocks.
| In silicon, this relates to impedance, design rules, and
| making sure the clocks are properly synchronized (adding
| buffers, etc). In factorio, this could be making sure
| conveyors are the same length, placing extractors on
| resources, etc. But the general topology doesn't change,
| since you are the one specifying it.
|
| Note: I've done some, but very little logic synthesis, this
| is really a bird view.
| jcurbo wrote:
| Not too far off from circuit design languages (VHDL, Verilog)
| to me.
| ashtonkem wrote:
| I've been noodling away on this related question:
|
| Given that sub factories in Factorio can be graded numerically
| on units/s output, and given that Factorio has a built in
| blueprint system, could one use ML to automatically create the
| most efficient blueprint possible for a given output?
| arduinomancer wrote:
| Hardware description languages such as VHDL/Verilog feel very
| similar to Factorio in that "everything is happening at the
| same time in parallel". Definately takes a while to wrap your
| head around it coming from procedural languages.
|
| The "compilers" for these languages have very sophisticated
| "routing" algorithms which synthesize efficient physical
| layouts of circuits.
|
| Programs like Quartus even let you edit your description
| visually as an abstract block diagram by dragging around
| wires/placing blocks.
| NickBusey wrote:
| You absolutely can check factories into version control. They
| are exported as 'blueprints' and many blueprint management
| sites exist. One such example: https://factorioprints.com/
| dmerrick wrote:
| That's not quite as satisfying as text-based files managed
| via proper version control
| count wrote:
| "infrastructure" as code :)
| apabepa wrote:
| I have been day dreaming about a program taking a factorio
| blueprint and running it through a genetic algorithm to produce
| a compressed but still functional version. I can imagine it
| could produce some interesting spaghetti layout! One way to
| ensure that the end result still works the same would be to
| only let the algorithm work with a selection of predefined
| refactoring steps that guarantee that the factory does not
| break. Another maybe more interesting approach would be to
| include some actual simulation that could test the factory.
| With the simulation the algorithm could work more freely with
| possibly destructive changes and apply them until the factory
| works as expected. This I figure would produce even more
| interesting spaghetti..
| munchbunny wrote:
| I don't think it's actually that hard, as long as you don't try
| too hard to optimize the layout itself. Write a few heuristics
| into the system and you can get really close to the thing
| you're describing. Most of the throughput modeling is pretty
| straightforward from an algorithmic perspective.
|
| This probably gets the most interesting in the super-late game
| where players have unlocked all of the tech and are building
| megabases. Now that the making of everything has been
| automated, perhaps the building of everything should be
| automated as well.
| ljm wrote:
| I really want to see a factorio app with a pipeline that takes
| an HTTP request as input and eventually converts it into a
| useful JSON at the end.
| wongarsu wrote:
| Personally I think most of the game is actually about managing
| changing requirements and layouting, but there's certainly a
| subset of gamers that are after the perfect endgame base (the X
| science per second crowd). If that's the target group of the
| compiler then you can assume that enemies don't exist (they
| waste CPU power), resources are decoupled from the factory
| using trains (they run out too fast for tight integration) and
| that the entire tech tree is explored (everything before that
| is just a unimportant bootstrap base). Making those assumptions
| certainly makes the compiler more tractable, but it also gives
| you another interesting metric to optimize (CPU load of running
| the produced layout)
| munchbunny wrote:
| If you can make some assumptions like train length and
| loading/unloading station design, you can probably guess how
| many trains you need to supply a given design, which means
| you can probably automate the placement of resource
| collection outposts.
|
| You'd need some empirical data on train acceleration and top
| speed, but once you have that plus some generous assumptions
| about latency tolerances, you can probably automatically lay
| out tracks and train routes and resource outposts.
| BenoitP wrote:
| This will take a specification:
| https://kirkmcdonald.github.io/calc.html#tab=graph&data=1-1-...
|
| But 98% of the problem remains: generating a layout.
| momothereal wrote:
| It could take inspiration from some speedruns formats where the
| "seed" is pre-selected. That way you could have multiple people
| compete to write the most efficient factory in terms of
| resources and space to generate as many rockets as possible,
| and everyone with the same resource limitations and map layout!
| wruza wrote:
| That was fun, now how do I build a warehouse dispatch and
| accounting 3-tier with functorio?
| bee_rider wrote:
| I don't really know much about functional languages, but did the
| section on higher order functions throw any flags for those of
| you who know both functional programming and the game? The
| "assembler" components, when used as an ingredient in a recipe,
| aren't really acting as assemblers anymore, just as a
| component...
| dexwiz wrote:
| Factorio is a genre-defining game, and I think the number of
| clones is a testament to that.
|
| Factorio is often compared to programming, but it's closer to
| Excel where the logic and the output live in the same space. The
| game does a great job letting the user build with programming-
| like constructs without them ever having to understand any
| theory.
| tdy721 wrote:
| The inserter function automagically picks a direction for the
| ingredient/product flow.
| an_ko wrote:
| > Merging an empty belt with any other belt, makes no difference.
|
| I know the intended meaning was that it makes no difference to
| the result belt's _type_ , but in-game it does have the important
| difference of putting all material from the input belt onto just
| 1 side of the output belt, halving _throughput_.
|
| If in this image
| https://bartoszmilewski.files.wordpress.com/2020/11/unit.png the
| incoming belt had both sides filled, both would go onto only one
| side of the outgoing belt, possibly causing some stalling.
|
| Do type systems exist that include a notion of "throughput" or
| "capacity" like this?
| mgradowski wrote:
| Dependent types could presumably capture this idea. They are
| very expressive - see [1] for a perfectly type-safe printf
| implementation in Idris.
|
| [1]:
| https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c...
| rrobukef wrote:
| The inserter type is wrong when considering fluids, maybe it
| should be `inserter: Item a => a -> a'? Oh well, leaky
| abstractions are endemic.
| munk-a wrote:
| I think this is a neat attempt to make functional programming
| more acceptable but I really dislike the first section on
| assembler production
|
| > If Factorio were a strongly typed language all the way, there
| would be separate recipes for producing different assemblers
| (that is assemblers with different recipes). For instance, we
| could have:
|
| Technically yes - in a strongly typed language assemblers would
| be specifically specialized at compile time. For the Haskell side
| of things they go on to specify a higher order function to
| produce an assembler that can then be given a purpose by a second
| call. However they dropped the ball a bit on the C++ side and,
| while generics cause grief, they are quite good to learn about
| and this is a perfect place to use a templated function that
| could infer the correct type at compile time.
| jeremyis wrote:
| I know this kind of misses the point, but Factorio is one of the
| most addictive and rewarding gaming experience I've had. Beware:
| within months it ate up collectively hundreds of hours from
| myself and members of my friend group.
| AnIdiotOnTheNet wrote:
| I would agree that Factorio is addictive, but "rewarding" is
| not a word I would use to describe it personally. I finished it
| but did not enjoy the process.
| NickBusey wrote:
| I see this kind of comment often and it confuses me. If you
| didn't enjoy the process, why did you even bother finishing
| it?
|
| Further, how was launching your first rocket anything other
| than rewarding?
| AnIdiotOnTheNet wrote:
| See: Addicting.
|
| Launching the rocket was inevitable after a certain point,
| it was just a matter of tuning everything well enough to
| get there as quickly as possible, which seemed always to be
| hampered by some tedious task or another. Having robotics
| much _much_ earlier in the game might have helped a lot
| there.
| tasogare wrote:
| I watched part of the speed run on marathon + death world
| challenge and the guy got robotics under 2 hours. So it's
| nit that late game. Giving it earlier would be confusing
| for beginners.
| AnIdiotOnTheNet wrote:
| > I watched part of the speed run on marathon + death
| world challenge and the guy got robotics under 2 hours.
| So it's nit that late game.
|
| You were watching a speed runner who knew exactly what
| they were doing. I was a first time player who didn't. I
| spent north of 30 hours tediously laying out factory bits
| and wondering "why the hell can't I automate _this_? "
|
| P.S.: and hey wait, whole speed runs of Factorio seem to
| be in the range of 2-3 hours, and you're suggesting under
| 2 hours isn't late game?
| MauranKilom wrote:
| "marathon + death" is _much_ harder than normal Factorio.
| I agree that it 's a bit of a weird data point to bring
| up ("in a harder game mode it took a significant amount
| of time") without further reference (such as how long the
| full run took) though.
| jcranmer wrote:
| In a speed run, investing in robots is a waste of time
| and oil (oil being the most precious resource because you
| have so little of it in your starting area). However,
| robotics is necessary for yellow science, which is likely
| to be the last science pack (purple science unlocks
| automation techs, which might be useful for speed). So
| leaving robotics until one of the last techs to be
| researched wouldn't be unreasonable in a speed run.
|
| But robotics itself only requires blue research. In my
| own speed run of ~6h40m (admittedly, on an older version
| of the game, but the game pre-blue science hasn't changed
| much), I got blue science in about 2h, and bee-lining for
| robotics after that would only take a couple of
| researches beyond that (advanced oil, electric engine,
| robotics, and then construction robotics to do anything
| useful IIRC).
|
| I wouldn't call anything that takes only blue science to
| get to be late game; for me, that requires at least
| purple or yellow science. Robotics is on the other side
| of the oil barrier, which is definitely going to push it
| far later in the game for new people, but it's also
| accessible before you hit the point of needing to
| repetitively scale up your factory (which tends to come
| with purple/yellow science), so it is accessible before
| you really need it. However, the game doesn't
| particularly guide you towards knowing that it exists, so
| I can see how it might be frustrating for new players.
| falcolas wrote:
| A guess, compulsive completionism based around the sunk
| cost fallacy. Pretty common with videogame completionists
| (including myself).
| AnIdiotOnTheNet wrote:
| I am not a completionist. I can't really put my finger on
| why I kept playing, possibly it was because the end goal
| was inevitable and the only thing separating me from it
| was time and tedium as opposed to any real difficulty.
| Possibly I just had nothing better to do at the time.
| ssully wrote:
| As someone who loves Factorio, there is a lot about the
| process of the game that I don't love. Especially after
| having 'completed' about 4-5 factories with a friend, you
| get to a point where you know how to accomplish a task, but
| there is a lot of drudgery to get there. Also the combat is
| the game is tedious. When playing with a friend, we
| literally do a coin flip for who has to clear hives that
| are inside our pollution area. I personally push to turn
| off aliens when we play, but he thinks its lame to take out
| a function of the game.
|
| With all of that said though, getting to a result is always
| a great feeling and basically why I still play it after all
| of these years.
| depaya wrote:
| Makes sense, it's not for everyone.
|
| I will note that someone who does enjoy the game would never
| describe themselves as having "finished" it, there is always
| more to expand, more to optimize, etc. Not to mention all the
| mods to explore!
| AnIdiotOnTheNet wrote:
| Frankly that's probably one of the big reasons I didn't
| enjoy it, but did enjoy many Zachtronics games.
| minsc__and__boo wrote:
| Wait until you try Dyson Sphere Program.
|
| It definitely scratches that Factorio itch but in a different
| way.
| keithnz wrote:
| I haven't found dyson sphere as addictive, but it's
| definitely fun and has a similar feel, but dyson sphere seems
| more about building massive amounts of things rather than
| clever designs
| munchbunny wrote:
| I can definitely see the point the author is trying to make, and
| I agree that it naturally lends itself to a functional model of
| how the game works.
|
| On the other hand, and this is speaking as someone who has played
| the game for an embarrassing number of hours all the way into
| endgame "megabase" territory: my mental abstraction of the game
| isn't really functional. It's directed graphs. Sure, the two
| views can be equivalent representations, but I find thinking
| about the game as a directed graph (and reasoning about
| throughputs in a graph representing a network) to be the
| perspective that gives actionable insights most readily.
| dexwiz wrote:
| The author is one of the main sources of Category Theory for
| Programmers material. Take that as you will.
| munchbunny wrote:
| > The author is one of the main sources of Category Theory
| for Programmers material. Take that as you will.
|
| I'm having some trouble coming up with plausible inferences
| from that fact. Do you mean to imply that "when you have a
| hammer everything looks like a nail"?
| ksm1717 wrote:
| Maybe more like: "the best hammers can treat anything as a
| nail"
| AnHonestComment wrote:
| The realization at the end of your post is what the author
| is trying to get people to reach about software.
|
| The category theory for programmers crowd argue that
| categorical diagrams (annotated graphs) are the right way
| to reason about software to gain insights -- and the rest
| is describing the equivalence between functions in a type
| theory and maps in a category.
|
| Your mental model of directed graphs _is_ a functional
| model.
| dexwiz wrote:
| That, or this article is an attempt to explain the author's
| speciality using a concrete example.
| swagonomixxx wrote:
| Pretty much. Bartosz's posts are pretty much exclusively
| about the superiority of Haskell and functional
| programming, with occasional forays into C++ to explain a
| functional concept in a more familiar syntax.
| willis936 wrote:
| Because factorio is much closer to a gui-based HDL layout tool
| than a programming language. It might be fun to program a
| simulated annealer to play factorio.
| zygomega wrote:
| I often think about functional programming as a directed graph
| consuming information.
___________________________________________________________________
(page generated 2021-02-16 23:00 UTC)