NML: build an industry next to water

Discussions about the technical aspects of graphics development, including NewGRF tools and utilities.

Moderator: Graphics Moderators

Post Reply
User avatar
Skarfester
Engineer
Engineer
Posts: 53
Joined: 07 Jun 2022 02:09

NML: build an industry next to water

Post by Skarfester »

Hi,

I'm starting to learn some NML coding. I'm trying to define an industry that appears next to water, so I used this code:

Code: Select all

switch (FEAT_INDUSTRIES, SELF, loc_check_fact, (nearby_tile_water_class(0,0) == WATER_CLASS_SEA))
	{1: return CB_RESULT_LOCATION_ALLOW; return CB_RESULT_LOCATION_DISALLOW;}
but it always end with the placing error during map generation, no matter how high the sea level or how big the map. So I added this line to the industry section:

Code: Select all

spec_flags: bitmask(IND_FLAG_BUILT_ON_WATER);
but then the industry is placed over the water, far from the land.
For this example I'm replacing the temperate factory.
I don't know if the problem is with the tile checking or that the factory is too big to find the exact spot.
User avatar
ChillCore
Tycoon
Tycoon
Posts: 2822
Joined: 04 Oct 2008 23:05
Location: Lost in spaces

Re: NML: build an industry next to water

Post by ChillCore »

i'll give you credits for re-using oil rig code ... but then try to build on land instead of ....


don't use WATER_CLASS_SEA
-- .- -.-- / - .... . / ..-. --- .-. -.-. . / -... . / .-- .. - .... / -.-- --- ..- .-.-.-
--- .... / -.-- . .- .... --..-- / .- -. -.. / .--. .-. .- .. ... . / - .... . / .-.. --- .-. -.. / ..-. --- .-. / .... . / --. .- ...- . / ..- ... / -.-. .... --- --- -.-. .... --- --- ... .-.-.- / ---... .--.

Playing with my patchpack? Ask questions on usage and report bugs in the correct thread first, please.
All included patches have been modified and are no longer 100% original.
User avatar
jfs
Tycoon
Tycoon
Posts: 1763
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: NML: build an industry next to water

Post by jfs »

Consider using nearby_tile_is_water instead yes. Unless you very explicitly want it next to natural zero-level water, and don't want to count rivers or canals.

When you write nearby_tile_water_class(0,0) you are asking for the tile at offset (0,0) from the north corner of the industry layout. But usually that north corner is actually part of the industry building, which is the part you want to be on land. You should probably instead check something like nearby_tile_water_class(8,0) to check the tile located 8 coordinates southeast from the north tile. That is, check various tiles that are not inside the industry building.
User avatar
Skarfester
Engineer
Engineer
Posts: 53
Joined: 07 Jun 2022 02:09

Re: NML: build an industry next to water

Post by Skarfester »

Thank you JFS, specifying the correct tile coordinates did the trick. With the correct expressions I was able to place an industry on land next to the sea and also place an industry on the sea but next to the coast. The only problem is that the coordinates can't be negative, so I'm only able to check SW and SE tiles but not the ones to the NW and NE, thus limiting placing positions.
User avatar
jfs
Tycoon
Tycoon
Posts: 1763
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: NML: build an industry next to water

Post by jfs »

You are supposed to be able to use negative offsets, I'm quite sure.
ebla71
Transport Coordinator
Transport Coordinator
Posts: 346
Joined: 14 Apr 2021 21:48
Location: Earth

Re: NML: build an industry next to water

Post by ebla71 »

In my little Prohibition industry NewGRF there is an "industry" called "Coast Guard station" that can only be built on flat land directly next to water, not on the water itself and not on the "slope tiles".

This is the code used for that behaviour:

Code: Select all

	switch(FEAT_INDUSTRYTILES, SELF, Coast_Guard_Station_tile_1_lc_0,
		(nearby_tile_class( 1,  1) == TILE_CLASS_WATER) |
	  (nearby_tile_class( 1,  0) == TILE_CLASS_WATER) |
	  (nearby_tile_class( 1, -1) == TILE_CLASS_WATER) |
	  (nearby_tile_class( 0, -1) == TILE_CLASS_WATER) |
	  (nearby_tile_class(-1, -1) == TILE_CLASS_WATER) |
	  (nearby_tile_class(-1,  0) == TILE_CLASS_WATER) |
	  (nearby_tile_class(-1,  1) == TILE_CLASS_WATER) |
	  (nearby_tile_class( 0,  1) == TILE_CLASS_WATER)) {
	    0: return string(STR_ERROR_ONLY_NEXT_TO_WATER);
	    Coast_Guard_Station_tile_1_lc_1;
	  }
It checks all eight tiles around the central tile, which has coordinates (0, 0). Full NML source code in the attachment.

The only limit it has is that it can be built not only next to "ocean tiles" but also "river" and "channel" tiles - but as far as I know, there is no (or no simple) way to distinguish between these.
Attachments
prohibition-v0.1.nml
(90.44 KiB) Downloaded 21 times
ebla71
Transport Coordinator
Transport Coordinator
Posts: 346
Joined: 14 Apr 2021 21:48
Location: Earth

Re: NML: build an industry next to water

Post by ebla71 »

jfs wrote: 20 Feb 2024 09:47 You are supposed to be able to use negative offsets, I'm quite sure.
Yes, see my example above
User avatar
Skarfester
Engineer
Engineer
Posts: 53
Joined: 07 Jun 2022 02:09

Re: NML: build an industry next to water

Post by Skarfester »

jfs wrote: 20 Feb 2024 09:47 You are supposed to be able to use negative offsets, I'm quite sure.
When I put negative coordinates I get the error: nmlc ERROR: "example_industry3.nml", line 46: Argument of 'nearby_tile_water_class' out of range 0..15, encountered -1
It gives the same error with nearby_tile_water_class and nearby_tile_class . The only difference I see between my code and ebla71's is that ebla uses FEAT_INDUSTRYTILES while I'm using FEAT_INDUSTRIES, so I suppose that's a limitation of that feature.
Thank you for the help and the code examples!
ebla71
Transport Coordinator
Transport Coordinator
Posts: 346
Joined: 14 Apr 2021 21:48
Location: Earth

Re: NML: build an industry next to water

Post by ebla71 »

Skarfester wrote: 21 Feb 2024 06:34 It gives the same error with nearby_tile_water_class and nearby_tile_class . The only difference I see between my code and ebla71's is that ebla uses FEAT_INDUSTRYTILES while I'm using FEAT_INDUSTRIES, so I suppose that's a limitation of that feature.
Why do you want to use FEAT_INDUSTRIES and not simply switch to FEAT_INDUSTRYTILES ?!?
User avatar
Skarfester
Engineer
Engineer
Posts: 53
Joined: 07 Jun 2022 02:09

Re: NML: build an industry next to water

Post by Skarfester »

ebla71 wrote: 21 Feb 2024 16:42 Why do you want to use FEAT_INDUSTRIES and not simply switch to FEAT_INDUSTRYTILES ?!?
I was modifying a base industry. If I code a new one I'll definitively use FEAT_INDUSTRYTILES.
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: Bing [Bot] and 12 guests