Trying to implement a construction system

The title has said it all. I’m creating a game about management (nothing too complicated) and I can’t figure how can I make possible the next thing:

You have differents constructions you can build, but they take some time to be ready for use. You have workers that build your structures and the more workers you have the less time it takes you to finished those buildings.

So far, I can figure that I have to use *if statement when it comes to the workers (I mean, if you have no workers you can’t do anything) but I have no clue about creating a code related to inversely proportional situations. Thanks in advance :smiley:

The answer is: Math it all up :slight_smile:

First, you’ll need to have some definition for “how long a construction takes to build.” I’m going to call this unit Man-Hours because that makes it easy to think about, but your game can call it whatever you want. Then you want to assign that construction time to things, e.g.

  • *set hut_construction_mh 40
  • *set tower_construction_mh 100
  • *set barracks_construction_mh 500

The idea behind Man-Hours being, if you have one worker, the hut will take 40 hours to complete. If you have two, it takes 20, and so on.

Thus, to figure out how long it’ll take your workers to build something, you do:

*temp hut_construction_time hut_construction_mh / workers
(Or something similar, I don’t have the CS setup on this computer right now so I can’t verify that’s the correct syntax)

And definitely keep the *if to check if the player has any workers, as otherwise you’ll be dividing by zero :slight_smile:

1 Like

Thanks for answering my request :smiley: Yeah, you’re right when it comes to the use of maths. I started with this:


*create fortaleza_edificio_horas 1800  -----> the hours it will take to be finished
*create fortaleza_edificio_construccion false ------> this variable will tell to the game that the fort is not under construction
*create fortaleza_edificio false ----> this variable will be added once the fort is finished

You did something pretty interesting and simple with that division, but I think that there is a problem with it (maybe I’m wrong. I don’t really know…) and that problem will show up when the player hire more workers. I mean, in the time you started to build the fort there were like 10 workers, but some time later you have like 50. There will be any changes when it comes to the hours needed to fiish it?

Edit: Another question came up to me when I started to figure out all this mess: When the build is finally finished, how will I tell to the game that it is not under construction anymore?

So if you’re going to be looking at a variable number of workers while the construction is complete, you do it a bit differently. I’m making some assumptions about how your game works, but I’m guessing that the construction happens in the background, while the player’s out doing other things. So at some point you’re writing things like:

*fake_choice How do you defeat the kraken?
  # Spear it
  # Punch it
  # Bribe it with Kracken-crackers

You win!

*comment TODO: progress the background construction here

*finish

And you’ll have the code to update the construction in place of that TODO I wrote, and you’ll put it everywhere you need to update construction (or, to avoid duplication and possible errors, use a gosub_scene to write it once and just call it everywhere appropriate).

So now, rather than figure out how many hours it’ll be until the job is complete and then counting down those hours, you instead count down the number of Man-hours left in the job. To re-use my previous example:

*create hut_construction_mh 40
*create hut_under_construction false
*create hut_complete false
*create hut_construction_remaining 40

So that mirrors your example but I’ve added the “remaining” variable. What that does is let us count down. Later, in your “update construction” code, you do this:

*set hut_construction_remaining -workers

Since each worker gives one man-hour of work, and one hour just passed, you can subtract that from the total MH remaining. That way if you add workers halfway through, their additional work is counted from that point on. Once this is less than or equal to zero, you’ve built the hut!

1 Like
I made a much more complex version that tracks days spent on the construction project, the hours of labor spent on the project, the number of laborers working on the project (which you may vary day-by-day if you want), and the number of hours per day the laborers work. (Click this to display it.)
*title Construction Projects
*author @Minnow
*scene_list
	startup
*create project_type "none"
*create hours_spent 0
*create days_spent 0
*create time_required 0
*create labor_count 0
*create labor_speed 0
*label start_loop_1
[b]Prepare for construction:[/b]
*line_break
Current project: $!{project_type}
*line_break
Hours required to complete current project: ${time_required}
*if (project_type != "none") and (time_required = 0)
	(Project Complete)
*line_break
Hours spent on current project: ${hours_spent}
*line_break
Days spent on current project: ${days_spent}
*line_break
Laborers available: ${labor_count}
*line_break
Laborer efficiency (hours of labor per laborer per day): ${labor_speed}
*choice
	*selectable_if (((time_required >0) and (labor_count >0)) and (labor_speed >0)) #Spend one day on construction.
		*set days_spent +1
		*if (labor_count * labor_speed) >= time_required
			*set hours_spent +time_required
			*set time_required 0
		*if (labor_count * labor_speed) < time_required
			*set hours_spent +(labor_count * labor_speed)
			*set time_required -(labor_count * labor_speed)
		*goto start_loop_1
	#Choose a construction type.
		Houses take 1,000 hours to complete.
		*line_break
		Warehouses take 5,000 hours to complete.
		*line_break
		Offices take 10,000 hours to complete.
		*line_break
		Hotels take 15,000 hours to complete.
		*choice
			#House.
				*set project_type "house"
				*set time_required 1000
				*goto start_loop_1
			#Warehouse.
				*set project_type "warehouse"
				*set time_required 5000
				*goto start_loop_1
			#Office.
				*set project_type "office"
				*set time_required 10000
				*goto start_loop_1
			#Hotel.
				*set project_type "hotel"
				*set time_required 15000
				*goto start_loop_1
	#Change number of available laborers.
		Add laborers to, or remove laborers from, the project.
		*choice
			#Add one laborer.
				*set labor_count +1
				*goto start_loop_1
			*selectable_if (labor_count >=1) #Remove one laborer.
				*set labor_count -1
				*goto start_loop_1
			#Add ten laborers.
				*set labor_count +10
				*goto start_loop_1
			*selectable_if (labor_count >=10) #Remove ten laborers.
				*set labor_count -10
				*goto start_loop_1
			#Add 100 laborers.
				*set labor_count +100
				*goto start_loop_1
			*selectable_if (labor_count >=100) #Remove 100 laborers.
				*set labor_count -100
				*goto start_loop_1
	#Change laborer efficiency.
		Increase or decrease laborer efficiency.
		*choice
			*selectable_if (labor_speed <=11) #Gain one hour per day per laborer.
				*set labor_speed +1
				*goto start_loop_1
			*selectable_if (labor_speed <=8) #Gain four hours per day per laborer.
				*set labor_speed +4
				*goto start_loop_1
			*selectable_if (labor_speed <=4) #Gain eight hours per day per laborer.
				*set labor_speed +8
				*goto start_loop_1
			*selectable_if (labor_speed >=1) #Lose one hour per day per laborer.
				*set labor_speed -1
				*goto start_loop_1
			*selectable_if (labor_speed >=4) #Lose four hours per day per laborer.
				*set labor_speed -4
				*goto start_loop_1
			*selectable_if (labor_speed >=8) #Lose eight hours per day per laborer.
				*set labor_speed -8
				*goto start_loop_1
	#Quit.
		The end!
		*ending

In my version, the number of hours worked in a day is calculated by multiplying the number of workers (called labor_count here, but you could use any name) with the number of hours the group of laborers work per day (called labor_speed here.) As examples, a group of 5 laborers working 5 hours per day will complete 25 hours of labor per day. A group of 100 laborers working 10 hours per day will complete 1000 hours of labor per day.

The number of hours worked per day is then subtracted from the number of hours required to complete the project (named time_required here.)

All that is done with a single command: *set time_required -(labor_count * labor_speed)

That’s the core of the code right there.

Everything else is extra, either for testing purposes or for allowing the extra functions of this code sample.

Also, the construction project is marked as completed when the variable time_required is reduced to zero. time_required represents the number of hours of labor required to complete the project.

I love you man, thanks for helping me with this. I understand what you are saying, and your guesses are true: that’s exactly what I’m planning. A main story and some management here and there. That would solve all the problems. But just to be sure of it: The game will have around 20 builds, so…

*comment CONSTRUCCIONES DEL JUEGO

*create tope_edificios 2 <---- this is the maximum buildings you can build at the same time (maybe i will reduce it to 1 to add some simple tech tree)
*create max_edificios 0  <--- this is the number of building under construction


*create fortaleza_edificio_horas 1800
*create fortaleza_edificio_construccion false
*create fortaleza_edificio false
*create fortaleza_edificio_horas_f 1800


*create iglesia_edificio_horas 1000
*create iglesia_edificio_horas_f 1000
*create iglesia_edificio_construccion false
*create iglesia_edificio false

(Just a little example)

So when it comes to update the remaining time, I will have to do something like this:

*if hospital_under_construction true
  *set (hospital_construction_remaining) - (workers) * (hours_that_have_passed)

Omg, so complicated and simple at the same time. This version looks far better than the old one I have been using. Can I implement it on my game and tell you later if it works? (I will change english words for spanish words)
One question remains on the air:

Can I put some kind of handicap? I mean: imagine that your kingdom have like 100 workers, and you want to build cathedral. It will take you around 10.000 hours to be finished or what it’s the same: 10 days. I find it to be a bit unrealistic, because there is no way 100 workers can finished an enormous cathedral in less than 2 weeks.

Yes, you may! Feel free to use as much or as little of it as you want.

Can I put some kind of handicap?

Yes. In that particular case, the easiest way is likely increasing the number of hours required to build the cathedral. Maybe it takes 250.000 instead of 10.000?

You could also set the limit of “hours worked per day” (labor_speed in my example) to something smaller than I did. In my version, I limited the number of hours worked per day to 12, but you could easily limit it to 8, or 5, or even 1.

You could also limit the number of workers available. My example actually fails to put a limit on the number of workers a certain project may use. Limiting the number of laborers you can add would be achieved the much same way I limited the number of hours the laborers can work.

I love you all. Thanks for the support. If I finally release the game for android I will put the name of some users of this forum <3

PD: I’ll keep you informed with how the code is working.

Tomorrow I will continue with the project because it is overwhelming. I translated all the code to spanish and it works fine in the game :D. However, I will change some things about it, because I want the player to pay for the buildings (as the materials have some cost) and I think that laborer efficiency does not make much sense the way it is now. I will create a variable called worker_unhappiness and I will use it when it comes to change the worker efficiency or maybe I will disable the option to change it completely. Besides, I will have to create more variables related to the building, because I want them to appear in another stat screen as they are already finished and ready for use.

Do take the time to look through and understand any examples you use in full, you’ll benefit more in the long run. Perhaps you’ll need to make a change or fix a bug in the future, only to find the original author is busy or no longer around.

1 Like