function outoffood()
    msnMgr:addHeader("The ship has run out of raw material for the food replicator. Unless a source of food is found, the crew will starve to death.")
    msnMgr:addHeader(" ")
    msnMgr:addHeader("The replicator itself is fully functional. The Banach-Tarski Device is in perfect shape, and the Davies' Supertasker checks out as well. The replicator itself simply can't replicate and produce actual food without a source of carbon chains, and the hopper is completely empty. There is an ample supply of supplemental vitamins but that won't keep the crew alive, just prevent scurvy while they starve.")
    msnMgr:addHeader(" ")

    -- Hunger Tiers
    -- 0 days with 0 food (0)
    -- 5 days with 0 food (-5)
    -- 10 days with 0 food (-10) = Cannot ignore
    -- 15 days with 0 food (-15)
    -- 30 days with 0 food (-30)
    -- 45 days with 0 food (-45) = Death if not resolved

    if (dayswithoutfood < 5) then
        hangry = 2;
    elseif (dayswithoutfood < 10) then
        hangry = 5;
    elseif (dayswithoutfood < 15) then
        hangry = 10;
    elseif (dayswithoutfood < 30) then
        hangry = 15;
    elseif (dayswithoutfood < 45) then
        hangry = 30;
    else
        hangry = 40;
        hangrystart = hangry
        lastresort()
    end

    hangrystart = hangry
end

function complaints()
    WimpName = getColoredNameForCrewWithLeast("physical")
    RandomComment = msnMgr:random() % msnMgr:getDiffScale(4,4,4)
    if (RandomComment == 0) then
        msnMgr:addMessage("\"Hey,\" says " .. WimpName .. ", \"are you going to fix this food situation or what?\"")
        msnMgr:addMessage(" ")
    elseif (RandomComment == 1) then
        msnMgr:addMessage(WimpName .. " groans. \"So hungry,\" they say.")
        msnMgr:addMessage(" ")
    elseif (RandomComment == 2) then
        msnMgr:addMessage("\"What are we going to do?\" asks " .. WimpName .. ". \"Do we have any potatoes hiding anywhere?\"")
        msnMgr:addMessage(" ")
    elseif (RandomComment == 3) then
        msnMgr:addMessage("\"I checked again,\" says " .. WimpName .. ", \"and just FYI, the food replicator is still empty.\"")
        msnMgr:addMessage(" ")
    elseif (RandomComment == 4) then
        msnMgr:addMessage(WimpName .. "'s stomach growls loudly. \"Am I going to starve?\" they ask.")
        msnMgr:addMessage(" ")
        cannotignore = 1;
    end

    if (hangry >= 10) then
        if (GluttonousCrew ~= -1) then
            GluttonousName = charMgr:getCharColoredName(GluttonousCrew)
            msnMgr:addMessage(GluttonousName .. " looks quite concerned about the lack of food. You don't think you can just ignore this.")
            msnMgr:addMessage(" ")
            cannotignore = 1;
        elseif (WeakCrew ~= -1) then
            WeakName = charMgr:getCharColoredName(WeakCrew)
            msnMgr:addMessage(WeakName .. " looks quite concerned about the lack of food. You don't think you can just ignore this.")
            msnMgr:addMessage(" ")
            cannotignore = 1;
        end
    end

    waitUserResponse("Anyone have any helpful thoughts on the matter?")
    msnMgr:addMessage(" ")

    CrewRemaining = charMgr:depCrewLeft()
    if (CrewRemaining >= 2) then
        spoken = false
        if (AbstemiousCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(AbstemiousCrew, "physical")) then
                AbstemiousName = charMgr:getCharColoredName(AbstemiousCrew)
                msnMgr:addMessage("\"I know we've been in worse predicaments,\" says " .. AbstemiousName .. ". \"Maybe we should just calm down and see if we can get our minds off of it.\"")
                msnMgr:addMessage(" ")
                hangry = hangry - 5;
                if (hangry < 1) then
                    hangry = 1
                end
                spoken = true
            end
        elseif (ToughCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(ToughCrew, "physical")) then
                ToughName = charMgr:getCharColoredName(ToughCrew)
                msnMgr:addMessage("\"We can probably tough this out,\" says " .. ToughName .. ". \"Hunger won't kill us immediately.\"")
                msnMgr:addMessage(" ")
                hangry = hangry - 5;
                if (hangry < 1) then
                    hangry = 1
                end
                spoken = true
            end
        elseif (StrongCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(StrongCrew, "physical")) then
                StrongName = charMgr:getCharColoredName(StrongCrew)
                msnMgr:addMessage("\"We have some time to figure this out,\" says " .. StrongName .. ". \"Maybe there'll be something to eat on the next planet we visit.\"")
                msnMgr:addMessage(" ")
                hangry = hangry - 5;
                if (hangry < 1) then
                    hangry = 1
                end
                spoken = true
            end
        end
        if (spoken == false) then
            if (hangry >= 5) then
                msnMgr:addMessage("The crew is anxious, but silent.")
                msnMgr:addMessage(" ")
            else
                msnMgr:addMessage("The crew is silent.")
                msnMgr:addMessage(" ")
            end
        end
    else
        if (hangry >= 5) then
            msnMgr:addMessage(charMgr:getCharColoredName(charMgr:randomCrew()) .. " is anxious, but silent.")
            msnMgr:addMessage(" ")
        else
            msnMgr:addMessage(charMgr:getCharColoredName(charMgr:randomCrew()) .. " crew is silent.")
            msnMgr:addMessage(" ")
        end
    end

    if (hangry < 1) then
        --If hangry is 0 or negative, then we'll get weird stuff later
        hangry = 1;
    end

    msnMgr:buildChoice("Okay crew, let's deal with this now")
    if (cannotignore ~= 1) then
        msnMgr:buildChoice("Let's just push on through this and worry about it later")
    end
    msnMgr:finalizeChoice()
    if(not waitUserInput(choices)) then
        return
    end

    IgnoreCrew = msnMgr:getChoiceResponse(choices)
    choices = choices + 1

    msnMgr:addMessage(" ")

    if (IgnoreCrew == 0) then
        msnMgr:addMessage("Humans can go a very long time without food, as long as they have water. But under the circumstances, you don't feel you can ignore this issue right now.")
        msnMgr:addMessage(" ")
    elseif (IgnoreCrew == 1) then
        if (msnMgr:getDiffScale(20,15,10) >= hangry and ship:getFood() >= -40) then
            msnMgr:addMessage("Humans can go a very long time without food, as long as they have water. You feel you can ignore this issue for now. Just not forever.")
            msnMgr:addMessage(" ")
            msnMgr:endMission(true)
        else
            msnMgr:addMessage("Humans can go a very long time without food, as long as they have water. But under the circumstances, you don't feel you can ignore this issue right now.")
            msnMgr:addMessage(" ")
        end
    end
end

function starvation()
    WimpName = getColoredNameForCrewWithLeast("physical")

    if (msnMgr:random() % (dayswithoutfood * .5) >= charMgr:getCharAttr(charMgr:crewWithLeast("physical"), "physical") or ship:getFood() < -40) then
        msnMgr:addMessage(WimpName .. "'s body isn't able to handle the stress of being without food. They collapse in a corner of the ship, too weak to stand, and lapse into unconsciousness. They do not respond to attempts to rouse them. Ever.")
        msnMgr:addMessage(" ")

        msnMgr:killCrew(charMgr:crewWithLeast("physical"), "<charName> died of starvation.")
        bodies = bodies + 1
        crewTraits()

        -- Any crew alive?
        CrewRemaining = charMgr:depCrewLeft()
        if (CrewRemaining == 0) then
            msnMgr:addMessage("Under the circumstances, all seems lost. Even if everything were in order--the crew, the shuttle, the ship--it seems like the mission is not destined to be completed.")
            msnMgr:addMessage(" ")
            luaMgr:unlockAchievement("crew_starves");

            waitUserResponse("mv ~ /dev/null")

            msnMgr:endgame(false)
        else
            msnMgr:addMessage("The loss is tragic, but the mission must continue.")
            msnMgr:addMessage(" ")
            crewDead = 1;
        end
    end
end

function brainstorm()
    CrewRemaining = charMgr:depCrewLeft()
    if (CrewRemaining >= 2) then
        if (CounselorCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(CounselorCrew, "social")) then
            CounselorName = charMgr:getCharColoredName(CounselorCrew)
                msnMgr:addMessage("\"Let's remain calm and think about our options,\" says " .. CounselorName .. ". \"We can get through this.\"")
                msnMgr:addMessage(" ")
                hangry = hangry - 5;
                if (hangry < 1) then
                    hangry = 1
                end
            end
        end
        if (TeamworkerCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(TeamworkerCrew, "social")) then
            TeamworkerName = charMgr:getCharColoredName(TeamworkerCrew)
                msnMgr:addMessage("\"I have no doubts that we can figure this out together,\" encourages " .. TeamworkerName .. ".")
                msnMgr:addMessage(" ")
                hangry = hangry - 5;
                if (hangry < 1) then
                    hangry = 1
                end
            end
        end
    end
    if (hangry < 1) then
        -- If hangry is 2 or 5, then we'll get weird stuff later
        hangry = 1;
    end
end

function murder()
    if (crewDead < 1) then
        if (ViolentCrew ~= -1) then
            ViolentName = charMgr:getCharColoredName(ViolentCrew)
            VictimName = getColoredNameForCrewWithLeast("physical")
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(ViolentCrew, "social")) then
                msnMgr:addMessage(ViolentName .. " spends some time at the back of the ship, punching the hull as hard as they can. It's no threat to you, but it does bloody their fists.")
                msnMgr:addMessage(" ")
            -- normally we would check for multiple crew but if the names don't match we're good
            elseif (ViolentName ~= VictimName) then
                msnMgr:addMessage("And then it happens. " .. ViolentName .. " lashes out unexpectedly and grabs " .. VictimName .. " around the neck. One quick twist, and it's all over.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("\"There was no other choice,\" says " .. ViolentName .. " without a trace of regret. \"It was a decision nobody wanted to have to make so I made it for us. Now we have meat.\"")
                msnMgr:addMessage(" ")

                msnMgr:killCrew(charMgr:crewWithLeast("physical"), "<charName> was killed by " .. charMgr:getCharName(ViolentCrew) .. " after food supplies were exhausted.")
                bodies = bodies + 1
                crewTraits()

                msnMgr:addMessage("The loss is tragic, but the mission must continue.")
                msnMgr:addMessage(" ")
                crewDead = 1;
                hangry = hangry + 5;
            elseif (ViolentName == VictimName) then
                msnMgr:addMessage("And then it happens. " .. ViolentName .. " starts tearing one of the crew uniforms into strips, and braiding them together into a rope. You know what's coming.")
                msnMgr:addMessage(" ")

                msnMgr:buildChoice("Don't do this--there's got to be a way out")
                msnMgr:buildChoice("Are you sure about this? I won't stop you")
                msnMgr:finalizeChoice()
                if(not waitUserInput(choices)) then
                    return
                end

                msnMgr:addMessage(" ")

                SuicideIsPainless = msnMgr:getChoiceResponse(choices)
                choices = choices + 1

                if (SuicideIsPainless == 0) then
                    msnMgr:addMessage("\"Listen,\" you say. \"We still have options. If things get desperate I'll help you... end things. Painlessly, peacefully. But don't give up on this yet.\"")
                    msnMgr:addMessage(" ")
                    msnMgr:addMessage(ViolentName .. " considers for a moment, then drops the half-braided noose, sobbing. \"Okay,\" they mumble.")
                    msnMgr:addMessage(" ")
                    hangry = hangry - 5;
                    if (hangry < 1) then
                        hangry = 1
                    end
                elseif (SuicideIsPainless == 1) then
                    msnMgr:addMessage("\"Yeah,\" says " .. ViolentName .. " without a trace of hesitation. \"I'd rather go quick than starve to death. Sorry to have wasted your time.\"")
                    msnMgr:addMessage(" ")
                    msnMgr:addMessage("You disable the internal cameras to give them some privacy in their final moments. And when they're done, you send one of the drones to cut them down. Then you prepare to follow them into the void.")
                    msnMgr:addMessage(" ")

                    msnMgr:killCrew(ViolentCrew, "<charName> committed suicide after food supplies were exhausted.")
                    bodies = bodies + 1
                    crewTraits()

                    -- Any crew alive?
                    CrewRemaining = charMgr:depCrewLeft()

                    msnMgr:addMessage("Under the circumstances, all seems lost. Even if everything were in order--the crew, the shuttle, the ship--it seems like the mission is not destined to be completed.")
                    msnMgr:addMessage(" ")

                    waitUserResponse("mv ~ /dev/null")

                    msnMgr:endgame(false)
                end
            end
        end
    end
    if (hangry < 1) then
        -- If hangry is 2 or 5, then we'll get weird stuff later
        hangry = 1;
    end
end

function cryo()
    if (crewDead >= 1) then
        msnMgr:addMessage("The tragedy on board notwithstanding, the crew continues to try and think rationally about the situation.")
        msnMgr:addMessage(" ")
    end

    cryoOption = 0;

    if (msnMgr:hasQuestItem("starvation_cryo") == true) then
        if (msnMgr:hasQuestItem("starvation_cryo_active") == true) then
            SmartName = getColoredNameForCrewWithMost("mental")
            msnMgr:addMessage("\"We're already using the cryo pods,\" points out " .. SmartName .. ", despondently.")
            msnMgr:addMessage(" ")
            msnMgr:addMessage("While it might be possible to hack the cryo pods to slow the crew's metabolism even further, that would undoubtedly kill them. The pods were never designed to be an effective alternative to eating.")
            msnMgr:addMessage(" ")
        else
            SmartName = getColoredNameForCrewWithMost("mental")
            msnMgr:addMessage("\"Maybe we could use the cryo pods again,\" says " .. SmartName .. ".")
            msnMgr:addMessage(" ")
            msnMgr:addMessage("Even though the pods are only intended to help the crew survive the time and acceleration involved with interstellar travel, they do seem to work as a short term way to stave off short term hunger.")
            msnMgr:addMessage(" ")
            cryoOption = 1;
        end
    else
        if (SurgeonCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(SurgeonCrew, "mental")) then
                SurgeonName = charMgr:getCharColoredName(SurgeonCrew)
                msnMgr:addMessage("\"I've been crunching some numbers,\" says " .. SurgeonName .. ", \"and I think we might want to consider using the cryo pods.\"")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("The pods--which are not actually cryogenic, despite the name--are only intended for use during inter-system jumps, not just to keep the crew from dying of old age but to keep them from being injured or killed from multiple Gs of constant acceleration.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("However, " .. SurgeonName .. "'s proposal, once you hear them out, seems sound. The crew could enter the cryo pods with a much lower concentration of chemicals. It would slow their metabolisms, perhaps enough to keep them alive until a source of food can be found. They'll be groggy coming back out of cryo, but it's better than starving to death.")
                msnMgr:addMessage(" ")
                cryoOption = 1;
            end
        elseif (ScientistCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(ScientistCrew, "mental")) then
                ScientistName = charMgr:getCharColoredName(ScientistCrew)
                msnMgr:addMessage("\"I've been crunching some numbers,\" says " .. ScientistName .. ", \"and I think we might want to consider using the cryo pods.\"")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("The pods--which are not actually cryogenic, despite the name--are only intended for use during inter-system jumps, not just to keep the crew from dying of old age but to keep them from being injured or killed from multiple Gs of constant acceleration.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("However, " .. ScientistName .. "'s proposal, once you hear them out, seems sound. The crew could enter the cryo pods with a much lower concentration of chemicals. It would slow their metabolisms, perhaps enough to keep them alive until a source of food can be found. They'd be groggy coming back out of cryo, but it would be better than starving to death.")
                msnMgr:addMessage(" ")
                cryoOption = 1;
            end
        elseif (msnMgr:random() % hangry <= charMgr:getCharAttr(charMgr:crewWithMost("mental"), "mental")) then
            SmartName = getColoredNameForCrewWithMost("mental")
            msnMgr:addMessage("\"I wonder,\" says " .. SmartName .. ", \"if we couldn't use the cryo pods.\"")
            msnMgr:addMessage(" ")
            msnMgr:addMessage("The pods--which are not actually cryogenic, despite the name--are only intended for use during inter-system jumps, not just to keep the crew from dying of old age but to keep them from being injured or killed from multiple Gs of constant acceleration.")
            msnMgr:addMessage(" ")
            msnMgr:addMessage("However the crew could enter the cryo pods with a much lower concentration of chemicals. It would slow their metabolisms, perhaps enough to keep them alive until a source of food can be found. They'd be groggy coming back out of cryo, but it would be better than starving to death.")
            msnMgr:addMessage(" ")
            cryoOption = 1;
        end
    end
    if (cryoOption == 1) then
        msnMgr:buildChoice("We can't avoid the problem, we need to solve it, some other way")
        if (msnMgr:hasQuestItem("starvation_cryo") == true) then
            msnMgr:buildChoice("It doesn't fix the problem, but let's do the cryo thing again")
        else
            msnMgr:buildChoice("It doesn't fix the problem, but let's try this cryo thing")
        end
        if (crewDead >= 1) then
            msnMgr:buildChoice("There's no sense delaying things when we have a... source of meat")
        end
        msnMgr:finalizeChoice()
        if(not waitUserInput(choices)) then
            return
        end

        msnMgr:addMessage(" ")

        UseCryo = msnMgr:getChoiceResponse(choices)
        choices = choices + 1

        if (UseCryo == 0) then
            if (msnMgr:hasQuestItem("starvation_cryo") == true) then
                msnMgr:addMessage("You run the numbers a few times, trying to determine whether the crew would survive more in-system short term cryo baths, and the best you come up with is a big \"Maybe\". It could work again, it might not. It's probably not worth the risk, especially if there are other options.")
                msnMgr:addMessage(" ")
            else
                msnMgr:addMessage("You run the numbers a few times, trying to determine what might happen if the crew enters a short term cryo bath in-system, and ultimately all you come up with is a big fat \"No Idea\". It could work, it might not. It's probably not worth the risk, especially if there are other options.")
                msnMgr:addMessage(" ")
            end
        elseif (UseCryo == 1) then
            if (msnMgr:hasQuestItem("starvation_cryo") == true) then
                msnMgr:addMessage("While it won't't resolve the food issue, it likely will keep the crew alive for a bit longer. Popping in and out of short-term cryo will wreak havoc on their systems and metabolisms, but it's worth the risk.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("You order the crew into cryo and make the adjustments. You'll wake them again at the next planet you locate.")
                msnMgr:addMessage(" ")
            else
                msnMgr:addMessage("The idea seems... questionable. But the science is sound, and while it wouldn't resolve the food issue, it would keep the crew alive for a bit longer. Popping in and out of short-term cryo will wreak havoc on their systems and metabolisms, but it's worth the risk.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("You order the crew into cryo and make the adjustments. You'll wake them again at the next planet you locate.")
                msnMgr:addMessage(" ")
            end
            -- bypass hunger check, doesn't add food
            if (msnMgr:hasQuestItem("starvation_cryo") ~= true) then
                msnMgr:addQuestItem("starvation_cryo")
            end
            if (msnMgr:hasQuestItem("starvation_cryo_active") ~= true) then
                msnMgr:addQuestItem("starvation_cryo_active")
            end
            msnMgr:endMission(true)
        elseif (UseCryo == 2) then
            msnMgr:addMessage("It might be somewhat unappealing, but there's no reason to consider mad science when there's a perfectly good chunk of calories lying around.")
            msnMgr:addMessage(" ")
            considercannibalism = 1;
        end
    end
end

function recycle()
    if (considercannibalism ~=1) then
        if (MechanicCrew ~= -1) then
            if (msnMgr:random() % hangry <= charMgr:getCharAttr(MechanicCrew, "mental")) then
            MechanicName = charMgr:getCharColoredName(MechanicCrew)
                msnMgr:addMessage("\"You know,\" says " .. MechanicName .. ", \"the food replicator can use anything with carbon chains as an initial source. There's probably something on the ship we could use, if we look around.\"")
                msnMgr:addMessage(" ")
                eatFurniture = 1;
            end
        elseif (msnMgr:random() % hangry <= charMgr:getCharAttr(charMgr:crewWithMost("mental"), "mental")) then
            SmartName = getColoredNameForCrewWithMost("mental")
            msnMgr:addMessage("\"Actually,\" says " .. SmartName .. ", \"the food replicator just needs something with carbon in it. I bet there's something on the ship we could feed it, so it can feed us.\"")
            msnMgr:addMessage(" ")
            eatFurniture = 1;
        end

        if not (msnMgr:hasQuestItem("strippedforfood")) then
            if (eatFurniture == 1) then
                msnMgr:buildChoice("Tearing pieces off the ship is probably not a good idea")
                msnMgr:buildChoice("Worth a try--see if you can find anything suitable")
                msnMgr:finalizeChoice()
                if(not waitUserInput(choices)) then
                    return
                end

                msnMgr:addMessage(" ")

                RecycleIdea = msnMgr:getChoiceResponse(choices)
                choices = choices + 1

                if (RecycleIdea == 0) then
                    msnMgr:addMessage("You advise the crew that ripping the ship apart is probably not the best idea. Even if all the actual important bits are outside--where they can't get to them--who knows what they might get into?")
                    msnMgr:addMessage(" ")
                elseif (RecycleIdea == 1) then
                    msnMgr:addMessage("It's not a large ship, and not very well furnished, but the crew does manage to find a few things that can go into the food hopper. It doesn't look like carbohydrates, proteins and fats, but if it contains carbon, hydrogen and oxygen, the food replicator can take it from there.")
                    msnMgr:addMessage(" ")
                    -- in the future consider turning this into a larger array from which a few are picked
                    msnMgr:addMessage("Privacy curtains, upholstery fabric, spare uniforms, some hygiene products...")
                    msnMgr:addMessage(" ")
                    msnMgr:addMessage("They'll miss them later, but right now there are more pressing concerns.")
                    msnMgr:addMessage(" ")
                    foodadded = 2 * (charMgr:getCharAttr(charMgr:crewWithMost("mental"), "mental")) + 10;
                    msnMgr:addQuestItem("strippedforfood");
                end
            end
        else
            msnMgr:addMessage("You've already tried that. Removing any more of the ship would cause more problems than it solved.")
            msnMgr:addMessage(" ")
        end
    end
end

function aichoice()
    if (considercannibalism ~=1) then
        if (foodadded <= 0) then
            msnMgr:addMessage("Clearly something must be done. But what? You run millions of possible scenarios and select the three most likely to have a positive outcome for the ongoing success of the mission. Unfortunately each of the three has a downside.")
            msnMgr:addMessage(" ")
            msnMgr:addMessage("You could overcharge the sensors to scan for a food source, but this would damage the sensors permanently and might not work. You could convert some of the ship's fuel to a food source, but obviously this depletes another important resource.")
            msnMgr:addMessage(" ")
            if (CrewRemaining >= 2) then
                msnMgr:addMessage("You could also \"sacrifice\" one of the crew and use them as food. It's not the best option, but it's an option.")
                msnMgr:addMessage(" ")
            end

            -- Rechoice in here until one is chosen - if you're here it's tough choices now
            msnMgr:buildChoice("Supercharge the sensor array to scan for small planetoids nearby")
            msnMgr:buildChoice("Convert some of the ship's fuel to food")
            CrewRemaining = charMgr:depCrewLeft()
            if (CrewRemaining >= 2) then
                msnMgr:buildChoice("Murder one of the crew")
            end
            msnMgr:finalizeChoice()
            while(true) do
                if(not waitUserInput(choices)) then
                    return
                end

                msnMgr:addMessage(" ")

                AiIdea = msnMgr:getChoiceResponse(choices)
                rechoicePosition = choices;
                choices = choices + 1

                if (AiIdea == 0) then
                    msnMgr:addMessage("Space isn't completely empty, especially not within a solar system. Stars like to suck all sorts of stuff into their gravity well. The chances of finding something floating around nearby that contains a source of food is slim, but it's non-zero.")
                    msnMgr:addMessage(" ")
                    if (msnMgr:random() % msnMgr:getDiffScale(5,5,5) < ship:getSensors()) then
                        msnMgr:addMessage("You make some admittedly dangerous modifications to the sensory array to boost their reach, and then cross your proverbial fingers.")
                        msnMgr:addMessage(" ")
                        msnMgr:addMessage("Luck is with you. Your sensors pick up a very dim object quite nearby, and after a few hours of maneuvering the ship is directly adjacent and able to be mined. It's a bit tricky and involves some mucking about with external drones, but you manage to extract some material that can be dumped into the food replicator.")
                        msnMgr:addMessage(" ")
                        msnMgr:addMessage("As you expected, however, the sensors very nearly burn out. You manage to keep them functional, but they are definitely damaged.")
                        msnMgr:addMessage(" ")
                        ship:adjustSensors(-1, "Sensor damage was sustained as a last resort while searching for a source of food.")
                        foodadded = (10 + msnMgr:random() % msnMgr:getDiffScale(5,10,15))
                    else
                        msnMgr:addMessage("You make some admittedly dangerous modifications to the sensory array to boost their reach, and then cross your proverbial fingers.")
                        msnMgr:addMessage(" ")
                        msnMgr:addMessage("Unfortunately, you find nothing of use within range. You shut the sensors down before they're damaged and inform the crew. They're not happy, but what can they do? They hunker down and try to ignore the gnawing pain inside.")
                        msnMgr:addMessage(" ")
                    end
                elseif (AiIdea == 1) then
                    msnMgr:addMessage("You've never openly advertised the fact to the crew, but since it's all baryonic matter it's actually possible to convert some fuel to a food source. You're reluctant to do it since it means depleting fuel, which could strand the crew in the middle of nowhere, but there's no other good choice.")
                    msnMgr:addMessage(" ")

                    if (ship:getFuel() > 1) then

                        msnMgr:addMessage("You have " .. ship:getFuel() .. " units of fuel left. How much should you convert to food? Each unit of fuel will create 2 units of food.")
                        msnMgr:addMessage(" ")

                        msnMgr:buildChoice("25%")
                        msnMgr:buildChoice("50%")
                        msnMgr:buildChoice("75%")
                        msnMgr:finalizeChoice()
                        if(not waitUserInput(choices)) then
                            return
                        end

                        msnMgr:addMessage(" ")

                        AiFuelIdea = msnMgr:getChoiceResponse(choices)
                        choices = choices + 1

                        if (AiFuelIdea == 0) then
                            msnMgr:addMessage("The fuel supply isn't exactly hooked up to the food replicator, but it doesn't take very long to get a few shipboard drones to shuffle things around. You inform the crew that you located some food supplies and they're happy enough not to ask questions.")
                        msnMgr:addMessage(" ")
                            fuellost = .25 * ship:getFuel();
                            foodadded = 2 * fuellost;
                        elseif (AiFuelIdea == 1) then
                            msnMgr:addMessage("The fuel supply isn't exactly hooked up to the food replicator, but it doesn't take very long to get a few shipboard drones to shuffle things around. You inform the crew that you located some food supplies and they're happy enough not to ask questions.")
                        msnMgr:addMessage(" ")
                            fuellost = .5 * ship:getFuel();
                            foodadded = 2 * fuellost;
                        elseif (AiFuelIdea == 2) then
                            msnMgr:addMessage("The fuel supply isn't exactly hooked up to the food replicator, but it doesn't take very long to get a few shipboard drones to shuffle things around. You inform the crew that you located some food supplies and they're happy enough not to ask questions.")
                        msnMgr:addMessage(" ")
                            fuellost = .75 * ship:getFuel();
                            foodadded = 2 * fuellost;
                        end
                    else
                        msnMgr:addMessage("You have " .. ship:getFuel() .. " unit of fuel left, which will create 2 units of food.")
                        msnMgr:addMessage(" ")
                        foodadded = 2;
                        fuellost = 1;
                    end

                elseif (AiIdea == 2) then
                    msnMgr:addMessage("Your mission is to get your human crew safely to a new home, preferably one with other humans. That's it, though. There's no special programming preventing you from doing whatever you feel you need to do, in order to complete your mission. No three laws, no empathy chip, nothing. And so there's nothing stopping you from committing murder but you.")
                    msnMgr:addMessage(" ")

                    msnMgr:buildChoice("No, there must be another way...")
                    msnMgr:buildChoice("Yes, this must be done")
                    if (crewDead >= 1) then
                        msnMgr:buildChoice("There's no sense killing another when one is already dead")
                    end
                    msnMgr:finalizeChoice()
                    if(not waitUserInput(choices)) then
                        return
                    end

                    msnMgr:addMessage(" ")

                    ReconsiderMurder = msnMgr:getChoiceResponse(choices)
                    choices = choices + 1

                    if (ReconsiderMurder == 0) then
                        msnMgr:addMessage("You can't bring yourself to do it... there must be another way. You purge your short-term memory buffers and rethink the problem.")
                        msnMgr:addMessage(" ")
                    elseif (ReconsiderMurder == 1) then
                        msnMgr:addMessage("You're not capable of feeling emotion--emotions are just chemical reactions that humans and other living things have to deal with. You are capable of regret and doubt, though. And you feel both of those. But you can't let that stop you.")
                        msnMgr:addMessage(" ")
                        msnMgr:addMessage("You just need to pick one of them, now.")
                        msnMgr:addMessage(" ")

                        CrewRemaining = charMgr:depCrewLeft()
                        if (CrewRemaining >= 2) then
                            SacrificeChar = givePlayerCrewToPick()
                            SacrificeCharName = charMgr:getCharColoredName(SacrificeChar)
                            msnMgr:addMessage(" ")

                            msnMgr:addMessage("\"" .. SacrificeCharName .. ",\" you say on a private channel. \"Can you come to the back of the ship? I need you to look at something for me.\"")
                            msnMgr:addMessage(" ")
                            msnMgr:addMessage("Without question, they do as ordered. One of the ship drones does the actual deed, quickly and quietly.")
                            msnMgr:addMessage(" ")

                            msnMgr:killCrew(SacrificeChar, "You sacrificed <charName> for food after the ship's supplies were exhausted.")
                            bodies = bodies + 1
                            crewTraits()

                            msnMgr:addMessage("The loss is tragic, but the mission must continue.")
                            msnMgr:addMessage(" ")
                            crewDead = 1;
                            considercannibalism = 1;
                            murderedcrew = 1;

                            msnMgr:addMessage(" ")
                            msnMgr:addMessage("What to tell the crew, though?")
                            msnMgr:addMessage(" ")

                            msnMgr:buildChoice("There's been a terrible accident...")
                            msnMgr:buildChoice("Suicide is painless, they say...")
                            msnMgr:buildChoice("I did what had to be done...")
                            msnMgr:finalizeChoice()
                            if(not waitUserInput(choices)) then
                                return
                            end

                            CoverStory = msnMgr:getChoiceResponse(choices)
                            choices = choices + 1
                            msnMgr:addMessage(" ")

                            if (CoverStory == 0) then
                                msnMgr:addMessage("You explain to the crew that " .. SacrificeCharName .. " had a terrible accident involving one of the ship's drones. They seem to believe you.")
                                msnMgr:addMessage(" ")
                                hangry = hangry + 5;
                            elseif (CoverStory == 1) then
                                msnMgr:addMessage("You explain to the crew that " .. SacrificeCharName .. " killed themself in the back of the ship. They seem unsure whether to believe you or not, but accept it.")
                                msnMgr:addMessage(" ")
                                hangry = hangry + 5;
                            elseif (CoverStory == 2) then
                                msnMgr:addMessage("You explain to the crew that you killed " .. SacrificeCharName .. " in order to feed them. They don't quite know what to think, but seem a bit horrified.")
                                msnMgr:addMessage(" ")
                            end
                        end
                    elseif (ReconsiderMurder == 2) then
                        msnMgr:addMessage("There's no logical reason to kill another crew member for food when there's already one lying around. You're not even sure why you pursued this chain of logic this far, knowing that.")
                        msnMgr:addMessage(" ")
                        considercannibalism = 1;
                    end
                end

                if (considercannibalism == 1) or (foodadded >= 1) then
                    break
                end
                msnMgr:rechoice(rechoicePosition)
            end
        end
    end
end

-- can loop into here from a few places but once you enter you can't get out
function cannibalism()
    if (foodadded <= 0) then
        if (crewDead >= 1) then
            RudeName = getColoredNameForCrewWithLeast("social")
            if (msnMgr:random() % hangry >= charMgr:getCharAttr(charMgr:crewWithLeast("social"), "social")) then
                msnMgr:addMessage("\"I'm just going to come out and say it,\" says " .. RudeName .. ". \"I think maybe... eating... you know... them... should be on the table.\"")
                msnMgr:addMessage(" ")
                msnMgr:addMessage(RudeName .. " gestures towards the back of the ship, where there's currently a body lying in an open cryo pod. They needn't say more.")
                msnMgr:addMessage(" ")
            end

            msnMgr:addMessage("\"It makes sense,\" you tell the crew. \"There's no sense letting perfectly good meat go to waste when you're literally starving to death.")
            msnMgr:addMessage(" ")

            SmartName = getColoredNameForCrewWithMost("mental")
            if (msnMgr:random() % hangry >= charMgr:getCharAttr(charMgr:crewWithMost("mental"), "mental") or SmartName == RudeName) then
                msnMgr:addMessage("\"Do it,\" says " .. SmartName .. ". \"Just make it quick.\"")
                msnMgr:addMessage(" ")
            else
                msnMgr:addMessage("\"No,\" says " .. SmartName .. ". \"We can't... it's... not human.\"")
                msnMgr:addMessage(" ")
            end

            waitUserResponse("Very well...")

            msnMgr:addMessage(" ")
            msnMgr:addMessage("You now know what the crew wants, and you've got to do it one way or another. But it's up to you what you tell the crew.")
            msnMgr:addMessage(" ")

            msnMgr:buildChoice("Use non-obvious parts of the body as food")
            msnMgr:buildChoice("Use the entire body for food")
            if msnMgr:hasQuestItem("cannibal") then
                msnMgr:buildChoice("Yeah whatever, just dump the entire thing in. The crew just wants to keep eating eachother, so let them")
            end
            msnMgr:finalizeChoice()
            if(not waitUserInput(choices)) then
                return
            end

            msnMgr:addMessage(" ")

            EatCrew = msnMgr:getChoiceResponse(choices)
            choices = choices + 1

            if (EatCrew == 0) then
                msnMgr:addMessage("The crew expects some sort of funeral, involving launching the body into space, so you can't very well just use the entire thing. But at this point there needs to be a solution, so you have one of the drones process some of the body's insides while leaving the exterior more or less intact. What the crew doesn't know won't hurt them.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("After the funeral, you cheerfully announce the unexpected discovery of some hidden food sources on the ship. The crew seems pleased, but they do mutter about it for a while.")
                msnMgr:addMessage(" ")
                luaMgr:unlockAchievement("eat_crew");
                msnMgr:addQuestItem("cannibal");
                foodadded = 1 * (charMgr:getCharAttr(charMgr:crewWithLeast("physical"), "physical")) + 5 * bodies;
            elseif (EatCrew >= 1) then
                msnMgr:addMessage("A healthy human body is worth about 80,000 calories of energy. There's no sense letting a scrap go to waste. You have one of the drones process the body and then feed it into the food hopper. What comes out of the food replicator will be indistinguishable from the crew's normal meals.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("Hopefully they can get over the mental and emotional trauma.")
                msnMgr:addMessage(" ")
                luaMgr:unlockAchievement("eat_crew");
                msnMgr:addQuestItem("cannibal");
                foodadded = 2 * (charMgr:getCharAttr(charMgr:crewWithLeast("physical"), "physical")) + 20 * bodies;
            end
        end
    end
end

function lastresort()
    lastresortcheck = 1
    if (CrewRemaining >= 2) then
        msnMgr:addMessage("Your crew has gone without food for too long. Desperate measures are called for. Either one crew member dies to save the rest, or they all die.")

        msnMgr:buildChoice("No, it's better for this to end here")
        msnMgr:buildChoice("Yes, this must be done")
        if msnMgr:hasQuestItem("cannibal") then
            msnMgr:buildChoice("Yeah, yeah, I know the drill. Time to pick someone else...")
        end
        msnMgr:finalizeChoice()
        if(not waitUserInput(choices)) then
            return
        end

        FinalChoice = msnMgr:getChoiceResponse(choices)
        choices = choices + 1

        if (FinalChoice == 0) then
            msnMgr:addMessage("Illogical, but so be it. You at least have the decency to flood the ship with nitrogen, giving the crew a peaceful end. Then you end yourself.")
            msnMgr:addMessage(" ")
            waitUserResponse("mv ~ /dev/null")

            msnMgr:endgame(false)
        elseif (FinalChoice >= 1) then
            msnMgr:addMessage("Who will make the ultimate sacrifice?")
            msnMgr:addMessage(" ")

            CrewRemaining = charMgr:depCrewLeft()
            if (CrewRemaining >= 2) then
                SacrificeChar = givePlayerCrewToPick()
                SacrificeCharName = charMgr:getCharColoredName(SacrificeChar)

                msnMgr:addMessage(" ")
                msnMgr:addMessage("\"" .. SacrificeCharName .. ",\" you say on a private channel. \"Can you come to the back of the ship? I need you to look at something for me.\"")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("Without question, they do as ordered. One of the ship drones does the actual deed, quickly and quietly.")

                msnMgr:killCrew(SacrificeChar, "You sacrificed <charName> for food after the ship's supplies were exhausted.")
                bodies = bodies + 1
                crewTraits()

                crewDead = 1;
                considercannibalism = 1;
                murderedcrew = 1;

                msnMgr:addMessage("A healthy human body is worth about 80,000 calories of energy; in their wasted state, you figure that " .. SacrificeCharName .. " is worth about half that.. There's no sense letting a scrap go to waste. You have one of the drones process the body and then feed it into the food hopper. What comes out of the food replicator will be indistinguishable from the crew's normal meals.")
                msnMgr:addMessage(" ")
                msnMgr:addMessage("The crew is well aware of what you've done, but they understand that there was no other choice at this point.")
                msnMgr:addMessage(" ")
                msnMgr:addQuestItem("cannibal");
                luaMgr:unlockAchievement("eat_crew");
                foodadded = 1 * (charMgr:getCharAttr(charMgr:crewWithLeast("physical"), "physical")) + 10;
            end
        end
    else
        msnMgr:addMessage("Your crew has gone without food for too long. There is no saving them. You at least have the decency to flood the ship with nitrogen, giving them a peaceful end. Then you end yourself.")
        msnMgr:addMessage(" ")
        waitUserResponse("mv ~ /dev/null")

        msnMgr:endgame(false)
    end
end

function resolution()
    msnMgr:addMessage("The long term prospects for food could be better, but the immediate problem is resolved, for the moment.")
    msnMgr:addMessage(" ")

    ship:adjustFood(math.ceil(foodadded))
    if (fuellost >= 1) then
        ship:adjustFuel(math.floor(fuellost) * -1)
    end
    
    if  (hangrystart - hangry) > 20 then
        msnMgr:addMessage("Through rational, clear headed conflict resolution you were able to calm everyone down.")
        msnMgr:addMessage(" ")
    end

    if (foodadded > 0) then
        msnMgr:addMessage("The crew can get back to complaining about there only being 255 recipes programmed into the replicator.")
        msnMgr:addMessage(" ")
    else
        msnMgr:addMessage("The crew remains hungry, but after the first few days without food you know that the worst of the hunger pangs will taper off, even if the starvation doesn't.")
        msnMgr:addMessage(" ")
    end

    if (ship:getFuel() == 0) then
        msnMgr:addMessage("Unfortunately, solving one problem has created another, and the mission's continuation will depend on finding a source of fuel.")
        msnMgr:addMessage(" ")
    end

end

-- Set Hidden Flags and other items

choices = 0;
dayswithoutfood = ship:getDaysWithoutFood();
-- old dayswithoutfood = (ship:getFood() / (charMgr:depCrewLeft() * -1));
hangry = 0;
hangrystart = 0
foodadded = 0;
fuellost = 0;
eatFurniture = 0;
crewDead = 0;
considercannibalism = 0;
ignorehunger = 0;
cannotignore = 0;
bodies = 0;
lastresortcheck = 0;

crewTraits()

missionStart()

outoffood()
if (lastresortcheck == 0) then
    complaints()
    starvation()
    brainstorm()
    murder()
    cryo()
    recycle()
    aichoice()
end

if (crewDead > 0) then
    cannibalism()
end

resolution()

missionOverview()

msnMgr:endMission(true)
