| 90 | } |
| 91 | |
| 92 | void FarmableObject::enterStage(int newStage) { |
| 93 | newStage = clamp<int>(newStage, 0, m_stages.size() - 1); |
| 94 | |
| 95 | // attempt to consume water from the soil if needed |
| 96 | if (m_consumeSoilMoisture && newStage > m_stage) { |
| 97 | if (auto orientation = currentOrientation()) { |
| 98 | auto assets = Root::singleton().assets(); |
| 99 | auto materialDatabase = Root::singleton().materialDatabase(); |
| 100 | auto wetToDryMods = assets->json("/farming.config:wetToDryMods"); |
| 101 | |
| 102 | // try to transform all anchor spaces, back out and reset stage time if |
| 103 | // they're not wet |
| 104 | for (auto anchor : orientation->anchors) { |
| 105 | auto pos = tilePosition() + anchor.position; |
| 106 | if (auto newMod = wetToDryMods.optString(materialDatabase->modName(world()->mod(pos, anchor.layer)))) { |
| 107 | world()->modifyTile(pos, PlaceMod{anchor.layer, materialDatabase->modId(*newMod), MaterialHue()}, true); |
| 108 | } else { |
| 109 | Vec2F durationRange = jsonToVec2F(m_stages.get(m_stage).get("duration", JsonArray({0, 0}))); |
| 110 | m_nextStageTime = world()->epochTime() + Random::randf(durationRange[0], durationRange[1]); |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // TODO: remove this hacky tree stuff and make plants handle it |
| 119 | if (m_stages.get(newStage).getBool("tree", false)) { |
| 120 | String stemName = configValue("stemName").toString(); |
| 121 | float stemHueShift = configValue("stemHueShift", 0).toFloat(); |
| 122 | String foliageName = configValue("foliageName", "").toString(); |
| 123 | float foliageHueShift = configValue("foliageHueShift", 0).toFloat(); |
| 124 | Vec2I position = tilePosition(); |
| 125 | |
| 126 | TreeVariant tv; |
| 127 | auto plantDatabase = Root::singleton().plantDatabase(); |
| 128 | if (!foliageName.empty()) |
| 129 | tv = plantDatabase->buildTreeVariant(stemName, stemHueShift, foliageName, foliageHueShift); |
| 130 | else |
| 131 | tv = plantDatabase->buildTreeVariant(stemName, stemHueShift); |
| 132 | |
| 133 | auto plant = plantDatabase->createPlant(tv, Random::randi64()); |
| 134 | plant->setTilePosition(position); |
| 135 | |
| 136 | if (anySpacesOccupied(plant->spaces()) || !allSpacesOccupied(plant->roots())) { |
| 137 | newStage = 0; |
| 138 | } else { |
| 139 | world()->timer(2.f / 60.f, [plant](World* world) { |
| 140 | world->addEntity(plant); |
| 141 | }); |
| 142 | |
| 143 | m_finalStage = true; |
| 144 | breakObject(true); |
| 145 | return; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (newStage == (int)m_stages.size() - 1) { |
nothing calls this directly
no test coverage detected