HELP coffee machine

Discussion about Mods for Prison Architect

Moderator: NBJeff

DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

HELP coffee machine

Postby DarknessEyes » Sat Oct 17, 2015 11:34 pm

I'm trying to make a coffee machine that requires prisoner cash to use but i cant find any way to charge cash to prisoners...
I tried with lua scripts and didn't find any way to charge prisoners cash or to change their sleep need.
Without lua scripts i tried to make a new object like shop front with a new provider

BEGIN Provider
Action BuyingCoffe
ProviderType Object
Object CoffeMachine
PrimaryNeed Sleep
PrimaryRate -20.0
ActionType Use (also tried with shopping)
Flags RequiresCash
Flags NoRoomRequired
Flags UsesEntireObject
Priority 1.05
END

It provides the sleep but doesn't take any cash to inmates...

Is there any way to do it?
Last edited by DarknessEyes on Sun Oct 18, 2015 8:13 pm, edited 1 time in total.
User avatar
aubergine18
level2
level2
Posts: 231
Joined: Sun Jul 05, 2015 3:24 pm

Re: coffee machine

Postby aubergine18 » Sun Oct 18, 2015 2:38 am

If you use scripting and you can get hold of the `Prisoner` entity using the machine, you can then do:

Code: Select all

local costPerUse = 10

prisoner.AvailableMoney = prisoner.AvailableMoney - costPerUse


For an example of how to get prisoner using a machine, see Chad's Drinks Mod.
DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

Re: coffee machine

Postby DarknessEyes » Sun Oct 18, 2015 1:59 pm

aubergine18 wrote:If you use scripting and you can get hold of the `Prisoner` entity using the machine, you can then do:

Code: Select all

local costPerUse = 10

prisoner.AvailableMoney = prisoner.AvailableMoney - costPerUse


For an example of how to get prisoner using a machine, see Chad's Drinks Mod.


what file? I cant find that line O.o

edited: I hav no idea what im doing wrong... the prisoners are "buyingcoffe", they go to nearest toilet and eat it O.o LOL

Fixed
DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

Re: coffee machine

Postby DarknessEyes » Sun Oct 18, 2015 3:43 pm

aubergine18 wrote:If you use scripting and you can get hold of the `Prisoner` entity using the machine, you can then do:

Code: Select all

local costPerUse = 10

prisoner.AvailableMoney = prisoner.AvailableMoney - costPerUse


For an example of how to get prisoner using a machine, see Chad's Drinks Mod.


Is there any way to reduce needs inside scripts?
"user.Needs.Sleep = 0;" works but "user.Needs.Sleep = user.Needs.Sleep - 20;" doesnt work...

and its possible to chose how much cash is required (flags RequiresCash) in needs.txt ?

So far i have:

Code: Select all

function GetObject(type,id,dist)
   objs = Object.GetNearbyObjects(type,dist or 1)
   for o,d in pairs(objs) do
       if o.Id.i == id then
          return o
       end
   end
end

function Create()
   this.Cost = 5.00;
   this.User = this.Id.u;
   Game.DebugOut("loaded");
end

function Update(dt)
   if tonumber(this.Slot0.i) > -1 then
      user = GetObject("Prisoner",this.Slot0.i);
      cash = user.AvailableMoney;
      if user.Id.u ~= this.User and tonumber(cash) >= tonumber(100) then
         this.User = user.Id.u;
         user.AvailableMoney = user.AvailableMoney - 100;
         user.Needs.Sleep = 0;
         Game.DebugOut("Prisoner sleep");
      end
   end
end

100 cash per item is just for testing...

* i need two function to complete the mod:
* remove the prisoners that dont have enough cash, they get stuck in coffee machine forever...
* Give me the cash used by prisoners and make it count toward the Shop revenue (finances -> Shop revenue from yesterday)
User avatar
Brento666
level3
level3
Posts: 290
Joined: Wed Sep 02, 2015 9:23 pm

Re: HELP coffee machine

Postby Brento666 » Sun Oct 18, 2015 9:54 pm

Hi DarknessEyes,

Firstly I have some minor tweaks for your current test script;

Code: Select all

--make cost a local variable at the top of your script (a 'this.Cost' isn't needed, it bloats your object and is more cumbersome to alter)
local cost = 5.0

function GetObject(type,id,dist)
   objs = Object.GetNearbyObjects(type,dist or 1)
   for o,d in pairs(objs) do
       if o.Id.i == id then
          return o
       end
   end
end

function Create()
   --minimize adding variables to this. better to use a local variable for cost, see top.
   --this.Cost = 5.00;
   this.User = this.Id.u;
   --Debug already shows the create event (in green) by default
   --Game.DebugOut("loaded");
end

function Update(dt)
   if tonumber(this.Slot0.i) > -1 then
      user = GetObject("Prisoner",this.Slot0.i);
      --user maybe could be nil: check. You can also check for already handled user at this point.
      if user~=nil and user.Id.u ~= this.User then
            --Make cash a local variable to the function. And cast money to a number right away.
            local cash = tonumber(user.AvailableMoney) or 0;
            --100 is already a number, no need to cast it (cash >= 100). But why not use your new 'cost' variable throughout your code? Changing that 1 value will effect all occurrences in your code.
            if cash >= cost then
                  this.User = user.Id.u;
                  --better to use your "cash" variable here (it is already casted to a number)
                  user.AvailableMoney = cash - cost;
                  user.Needs.Sleep = 0;
                  Game.DebugOut("Prisoner sleep need zero-ed");
          end
      end
   end
end

In your previous code, if you later changed this.Cost's value; It would only change for newly created objects, not the ones already existing in your savegame(!)
-Above code effects all machines (new and reloaded ones)

* and its possible to chose how much cash is required (flags RequiresCash) in needs.txt ?
-I don't think you can atm, sadly.

* remove the prisoners that dont have enough cash, they get stuck in coffee machine forever...
-Them staying at your machine is probably because it fulfills certain needs for them. Change your machine's need provider, so it only indicates needs, but doesn't effect needs. (See base game's "shopfront" need provider)

-Either set the effected need through script (like you're already doing).
-Or create a (new object with no script and a) need provider, for coffee supplied by the machine. That provider should indicate AND effect their needs.

* Give me the cash used by prisoners and make it count toward the Shop revenue (finances -> Shop revenue from yesterday)
-Maybe using the 'RequiresCash' flag will accumulate cash revenues. But atm you can't set a price that way.
-Doing it in script (with user.AvailableMoney), there is no way to get it counted as revenue, atleast not that I can think of...


I personally despise the taste coffee, though I can handle the smell!
->Please consider (also) creating a 'Tea' machine; perhaps it could relax prisoners a bit and ease their needs for security, environment, privacy and/or freedom. 8)
User avatar
aubergine18
level2
level2
Posts: 231
Joined: Sun Jul 05, 2015 3:24 pm

Re: HELP coffee machine

Postby aubergine18 » Sun Oct 18, 2015 10:01 pm

Just a quick tip... When checking if a value is `nil` you can treat `nil` as `false` (unless you specifically need to differentiate between the two)

Code: Select all

-- this
if something then .....

-- instead of this
if something ~= nil then ....

-- and...

-- this
if not something then ...

-- instead of this
if something == nil then ...


Also, I'm not sure that you need to cast things to numbers or strings any more - that was mainly due to API bugs prior to Alpha 36 but they should have all been fixed by now (if not, raise bugs on mantis). tonumber() and tostring() should not be needed since Alpha 36, except in rare circumstances (eg. Game.DebugOut() )

Oh and +1 for a tea machine. Make mine an Earl Grey.

As for changing prison bank balance, that can't currently be done via script. However, there is a hack that might work. Create a dummy object in materials.txt and set it's price to revenue per drink sold (positive value), then spawn it whenever a drink is sold. In it's Create() event handler, delete the dummy object. In theory it will only exist on the map for a tiny fraction of a second, if at all, and due to its +ve cost it will add some money to the prison bank account.
DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

Re: HELP coffee machine

Postby DarknessEyes » Sun Oct 18, 2015 10:22 pm

aubergine18 wrote:As for changing prison bank balance, that can't currently be done via script. However, there is a hack that might work. Create a dummy object in materials.txt and set it's price to revenue per drink sold (positive value), then spawn it whenever a drink is sold. In it's Create() event handler, delete the dummy object. In theory it will only exist on the map for a tiny fraction of a second, if at all, and due to its +ve cost it will add some money to the prison bank account.


i was trying to make the script create a grant that is automatically completed after each use O.o

But the dummy object is a nice idea... Make the machine drop coins... the workers pickup the coins and move them to exports O.o gotta try it!!!!!!
DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

Re: HELP coffee machine

Postby DarknessEyes » Sun Oct 18, 2015 10:26 pm

Brento666 wrote:prioners that dont have enough cash, they get stuck in coffee machine forever...
-Them staying at your machine is probably because it fulfills certain needs for them. Change your machine's need provider, so it only indicates needs, but doesn't effect needs. (See base game's "shopfront" need provider)


what do u mean?

set the PrimaryRate to 0.0
or
change the ActionType from "Use" to "Shopping" ?
DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

Re: HELP coffee machine

Postby DarknessEyes » Sun Oct 18, 2015 11:45 pm

omg, the result looks very good.

using
function Create()
this.Delete();
end
on money bag doesnt work. the bag is deleted but u dont get any cash...

I made each coffee costs $5, after each coffees the machine drops a bag of money worth $5, the bag is moved to exports and sold :)

Edited:

Now only need to order coffee beans, load them into slot 1 of machine and then delete one unit from the stack each time a coffee is drink.

this. variable is kept when u dismantle the object and reuse it again later?
User avatar
aubergine18
level2
level2
Posts: 231
Joined: Sun Jul 05, 2015 3:24 pm

Re: HELP coffee machine

Postby aubergine18 » Mon Oct 19, 2015 2:00 am

Maybe instead of a money bag it could be some sort of empty jar or container, and the revenue comes from recycling it? It is a real shame there's no script hook for the prison bank account.
DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

Re: HELP coffee machine

Postby DarknessEyes » Mon Oct 19, 2015 2:08 am

aubergine18 wrote:Maybe instead of a money bag it could be some sort of empty jar or container, and the revenue comes from recycling it? It is a real shame there's no script hook for the prison bank account.


Its working very good with the money bags, they stack to 10 and get moved to exports.
Now my problem is the next step...
machine needs coffee beans to be used
Im gonna set the machine to autoorder the coffee beans
The coffee beans going to be loaded into "slot1" of the machine
Each time the machine is used one unit of coffee beans is deleted.

My problem is... I cant find a way to:
* Temporary remove the machine from needs.txt...
OR
* Force the prisoner to stop using the machine and go do something else.

Code: Select all

BEGIN Provider
    Action           BuyingCoffee
    ProviderType   Object
    Object           CoffeeMachine
    PrimaryNeed       Sleep
    PrimaryRate       -1.0
    ActionType       Use
    Slot            0
    Flags           UsesEntireObject
    Flags           RequiresCash
    Priority        1.05
END


The PrimaryRate is set to -1.0 because sometimes the user gets "* hungry" right after using the machine... So he gets stuck on machine.
The RequiresCash seems to prevent anyone with less than $5 from using the machine.
User avatar
Brento666
level3
level3
Posts: 290
Joined: Wed Sep 02, 2015 9:23 pm

Re: HELP coffee machine

Postby Brento666 » Mon Oct 19, 2015 2:18 am

Those are some nice advancements, your mod could prove to be a valuable example for others!

As a future improvement I would advise creating (during Create()) and updating a "this.Collected" value.
-when that value reaches say 250 bucks, create a (very short) workman job, with 'keys' at your machine.
-in the 'job complete handler' spawn a pile of coins worth 250, or a 'money pouch' or somethin cool!
-chances are the workman will engage and proceed to immediately move the object to exports.
(!)your previously placed machines in savegame will bug, but don't worry just remove them an build new improved ones

That would make it more realistic and allot less work instead of having to collect loose change all the time. And said change just dropping for any inmate to snatch (realistically)


About this.Values after dismantle, I am not sure... but the scripted object's totally (and without warning) stop to be updated! And start again when placing.
-I wouldn't worry about this too much. I think objects that get dismantled will be brought to the storage. They can be replaced, script will be reset I believe.
-You can create additional production rules for your 'beans' and other objects so they also have places to go if your machines are full or unavailable. Leading rules have priority over following rules, so firstly bring to machine, secondly bring to storage room (or just leave them on the bean-plantations floor :lol: )

-Maybe later I'll test what the deal is with the values being truly lost or just overridden by the new create event...

Btw. I was meaning to comment on the storefronts '0.0 need increase', but also shopping seems applicable to your machine.. Which did you use, both?
Edit: shopping might give unwanted or conflicting troubles with the 'prebaked' shopping routine... Interested what will be the final setup of your objects materials/needs!
User avatar
aubergine18
level2
level2
Posts: 231
Joined: Sun Jul 05, 2015 3:24 pm

Re: HELP coffee machine

Postby aubergine18 » Mon Oct 19, 2015 5:36 pm

I'd recommend picking through the scripts and .txt files of Chad's Drinks Mod - it has a machine that runs out of stock and needs restocking (orange juice machine IIRC).

http://steamcommunity.com/sharedfiles/f ... =373758059
DarknessEyes
level2
level2
Posts: 105
Joined: Sat Apr 26, 2014 11:47 pm

Re: HELP coffee machine

Postby DarknessEyes » Mon Oct 19, 2015 5:42 pm

Brento666 wrote:Those are some nice advancements, your mod could prove to be a valuable example for others!

As a future improvement I would advise creating (during Create()) and updating a "this.Collected" value.
-when that value reaches say 250 bucks, create a (very short) workman job, with 'keys' at your machine.
-in the 'job complete handler' spawn a pile of coins worth 250, or a 'money pouch' or somethin cool!
-chances are the workman will engage and proceed to immediately move the object to exports.
(!)your previously placed machines in savegame will bug, but don't worry just remove them an build new improved ones

That would make it more realistic and allot less work instead of having to collect loose change all the time. And said change just dropping for any inmate to snatch (realistically)


About this.Values after dismantle, I am not sure... but the scripted object's totally (and without warning) stop to be updated! And start again when placing.
-I wouldn't worry about this too much. I think objects that get dismantled will be brought to the storage. They can be replaced, script will be reset I believe.
-You can create additional production rules for your 'beans' and other objects so they also have places to go if your machines are full or unavailable. Leading rules have priority over following rules, so firstly bring to machine, secondly bring to storage room (or just leave them on the bean-plantations floor :lol: )

-Maybe later I'll test what the deal is with the values being truly lost or just overridden by the new create event...

Btw. I was meaning to comment on the storefronts '0.0 need increase', but also shopping seems applicable to your machine.. Which did you use, both?
Edit: shopping might give unwanted or conflicting troubles with the 'prebaked' shopping routine... Interested what will be the final setup of your objects materials/needs!


I asked if this.Values remain after dismantle because of money bags that get dropped. I could make an increasing int and spawn a bag worth $50 after 10 uses but if this.values are not kept the money inside the machine is lost after dismantle... This also apply to coffee beans...

Now i still have two problems...

Currently the machine works without beans but after adding beans i need to "disable" the machine because the prisoners will get stuck in it..
* Temporary remove the machine from needs.txt
OR
* Changing the action of the prisoner, in savefile the current action shows up under needs but i cant find a way to change it...
OR
Change the whole system to the following

Machine with 2 slots
Slot0 = Coffee Beans loadded
Slot1 = Coffee mug

The machine deletes one unit of coffee beans and spawn one coffee mug
The needs will direct the user to coffee mug instead to machine.

aubergine18 wrote:I'd recommend picking through the scripts and .txt files of Chad's Drinks Mod - it has a machine that runs out of stock and needs restocking (orange juice machine IIRC).

http://steamcommunity.com/sharedfiles/f ... =373758059


Thats what im doing since begin :)
User avatar
Brento666
level3
level3
Posts: 290
Joined: Wed Sep 02, 2015 9:23 pm

Re: HELP coffee machine

Postby Brento666 » Mon Oct 19, 2015 7:43 pm

Hiya!

DarknessEyes wrote:I asked if this.Values remain after dismantle because of money bags that get dropped. I could make an increasing int and spawn a bag worth $50 after 10 uses but if this.values are not kept the money inside the machine is lost after dismantle... This also apply to coffee beans...

-I meant to imply no-one's really going to care losing 0-250 bucks IF and when they dismantle/move an uber-awesome-cool coffee machine! I wouldn't loose a wink of sleep over an amount like that ! That's like peanuts to my prison budget! 8)
-How often do you think it will be dismantled/moved after extended use (full of money)?
-See it as a penalty for users being iffy with your creation! :mrgreen:

(I also promised to test it if I get round to it. But haven't yet had time.)

Moving to a setup with multiple slots is absolutely needed; every ingredient needs a slot/marker.
-And don't forget to have a dedicated empty slot for prisoners!
-You can specify the slot for prisoners in the need provider.
tip: Don't make huge changes in script all at once; add the slots, then work on 1 script handle at a time. Errors are easier to find if you don't rescript the whole thing all at once.

(so a) Machine with 3 slots
Slot0 = Coffee Beans loadded
Slot1 = Coffee mug
Slot2 = empty/prisoner

Ps. I read somewhere that int-values don't seem to exist in lua -> every number is a float. (just a detail, I got your points)

Hope this helps! Don't let all my rambling hold you back from releasing it already tho! (I should focus on my own release now :roll: )

Return to “Modding”

Who is online

Users browsing this forum: No registered users and 14 guests