Skip to content

Instantly share code, notes, and snippets.

View FixedPointNumber.hpp
#pragma once
#include <TypeTraitsExtensions.hpp>
#include <Util/FixedPointNumberTemplateForwards.hpp>
#include <cstdlib> // keep me
#include <limits> // keep me
#include <stdint.h>
class Deserialiser;
class Serialiser;
template<class T, uint32_t PrecisionBits>
View CraftingMachine.cpp
void CraftingMachine::update()
{
assert(this->isActive());
assert(!this->isToBeDeconstructed());
/** Attempts to craft the current recipe (setting up the recipe if its a furnace) and returns the results of the attempt. */
ProductionResult result = this->useRecipeOnSource();
bool goToSleep = false;
switch (result)
{
@Rseding91
Rseding91 / Rail.cpp
Last active Apr 17, 2019
Factorio Rail bounding box logic
View Rail.cpp
std::pair<BoundingBox, BoundingBox> Rail::calculateBoundingBoxes(MapPosition position,
Direction direction,
RailBendingType bendingType,
const BoundingBox& collisionBoundingBox,
const BoundingBox* secondaryCollisionBoundingBox)
{
std::pair<BoundingBox, BoundingBox> boundingBoxes;
if (bendingType == RailBendingType::Straight)
{
View save-load.md

Deterministic save load in Factorio

One of the key parts of the save/load process in Factorio is that it must be deterministic. This means that for a given save file (when no external factors change) saving, exiting, and loading the save shouldn't change any observable behavior.

There are a few reasons and benefits for this strict requirement:

  • Without it: You couldn't join a running multiplayer game (and by proxy save, exit, and resume one)
  • Without it: the replay system wouldn't work if you ever saved, exited, and resumed playing.
  • With it: we can easily test that saving and loading produces no observable change letting us know we implemented save/load correctly.
  • With it: you won't see things change randomly as a result of "reloading" like you do in so many other games.
View Pure-virtual-base.hpp
struct base
{
enum class Type
{
A,
B,
C
};
virtual ~base() = default;
View MacroVariant.cpp
#include <MacroVariant.hpp>
bool MacroVariant::isCorrectDataTypeForAction(MacroVariantType action, const std::type_info& type)
{
switch (action)
{
#define ADD_CASE(ACTION, PREFIX, TYPE, VALUE) \
case MacroVariantType::ACTION: return typeid(std::remove_cv<PREFIX TYPE>) == type;
CALL_ON_EVERY_TYPE_WITH_DATA(ADD_CASE)
#undef ADD_CASE
View Car.cpp
void Car::update()
{
if (this->tickOfLastUpdate == this->getEntityTick())
return;
this->tickOfLastUpdate = this->getEntityTick();
if (!this->hasBeltImmunity())
this->moveByTransportBelt();
const CarPrototype& prototype = *this->getPrototype();
View TransportBelt.cpp
Vector TransportBelt::calculateEntityMovement(MapPosition oldPosition) const
{
RotationDefinition rotationDefinition = this->rotationPoint;
if (rotationDefinition.empty())
return Vector(this->getPrototype()->speed, this->direction);
Vector originalVector(rotationDefinition.center, oldPosition);
double distance = originalVector.distance();
double orientation = originalVector.getOrientation().getFraction();
View PipeConnectionDefinition.cpp
Direction PipeConnectionDefinition::calculateDirection(const Vector& offset, const BoundingBox& boundingBox)
{
// diagonal direction in order to fail when load is not successful
Direction direction(Direction::NorthEast);
MapPosition leftTop = boundingBox.getRotatedLeftTop();
MapPosition rightBottom = boundingBox.getRotatedRightBottom();
// calculate direction from the bounding box
if (leftTop.y < offset.dy && rightBottom.y > offset.dy)
{
@Rseding91
Rseding91 / TrainPathFinder.cpp
Last active Sep 21, 2020
Factorio TrainPathFinder
View TrainPathFinder.cpp
#include <Entity/Rail.hpp>
#include <Entity/RailSignalBase.hpp>
#include <Rail/RailBlock.hpp>
#include <Rail/RailPath.hpp>
#include <Rail/TrainPathFinder.hpp>
#include <Rail/RailSegment.hpp>
#include <Rail/RailUtil.hpp>
#include <Rail/Train.hpp>
#include <Util/Container/MinHeap.hpp>
#include <Log.hpp>