C coding tutorials

What would you like to see in Freeciv? Do you have a good idea what should be improved or how?
Post Reply
Elefant
Hardened
Posts: 212
Joined: Sat May 28, 2022 3:55 am

C coding tutorials

Post by Elefant »

I have tried several times to work with the freeciv source code, but every time I am unable to due to the complexity of it. Consequently, I think that some c coding tutorials for freeciv would lower the required skill level to contribute to freeciv.
A few things I would like to see tutorials for are adding an action, adding a variable to an object (unit type, building, etc.), and adding a function to be called by Lua.
Civ 3 tileset: viewtopic.php?t=92953
3d Irrlicht desktop client development: viewtopic.php?t=92289&start=20
vego
Veteran
Posts: 75
Joined: Thu May 26, 2022 10:49 am

Re: C coding tutorials

Post by vego »

If it is any tutorial to easy support a work on freeciv i go
cazfi
Elite
Posts: 3069
Joined: Tue Jan 29, 2013 6:54 pm

Re: C coding tutorials

Post by cazfi »

Meanwhile, if you know an already implemented patch that does similar thing to what you want to do, check through that patch to see where and how it implements the changes.
Ignatus
Elite
Posts: 644
Joined: Mon Nov 06, 2017 12:05 pm
Location: St.Petersburg, Russia
Contact:

Re: C coding tutorials

Post by Ignatus »

Github search by commits generally helps you. Also, data structures and some algorithms are mostly documented somewhere in the code, read it.
Elefant
Hardened
Posts: 212
Joined: Sat May 28, 2022 3:55 am

Re: C coding tutorials

Post by Elefant »

That's a great idea! I have previously tried to find patches for what I wanted to do on OSDN, but many older patches are not on there (or at least I couldn't find them). However, the patch's age means that many of them are very out of date and no longer applicable. For example, trying to make a nation req, the most recent requirement patch for units is the improvement req patch, from 2003.
https://github.com/freeciv/freeciv/comm ... ff=unified
As you can see, some of the affected files don't exist and the rest are very different, which makes it a lot harder for someone with no c coding experience to figure out. However, it is certainly better than nothing!
Civ 3 tileset: viewtopic.php?t=92953
3d Irrlicht desktop client development: viewtopic.php?t=92289&start=20
cazfi
Elite
Posts: 3069
Joined: Tue Jan 29, 2013 6:54 pm

Re: C coding tutorials

Post by cazfi »

Elefant wrote: Fri Jun 17, 2022 3:11 amFor example, trying to make a nation req, the most recent requirement patch for units is the improvement req patch, from 2003.
It's certainly more work, and maybe still not feasible, but it would make sense to evaluate what are the remaining obstacles preventing using full requirements vectors for units, resolving those issues, and then taking requirement vectors to use, instead of adding another single(!) requirement support as a special case.
Elefant
Hardened
Posts: 212
Joined: Sat May 28, 2022 3:55 am

Re: C coding tutorials

Post by Elefant »

It makes sense, but I have very little c coding experience, and it would be a lot easier to add a nation req (all that would be needed is a nation_req field for units, and to prevent them from being built if it is not satisfied. The AI wouldn't need to be notified because there is nothing anyone could do to build a uu unless they are of the right nation)
Civ 3 tileset: viewtopic.php?t=92953
3d Irrlicht desktop client development: viewtopic.php?t=92289&start=20
cazfi
Elite
Posts: 3069
Joined: Tue Jan 29, 2013 6:54 pm

Re: C coding tutorials

Post by cazfi »

Ok, I guess there's already bunch to learn about freeciv code even for the "simple" addition of nation requirement. The full implementation will require something like: (but you can test parts of it with less)

1) Add the nation requirement to the unit_type structure
2) Check that requirement in relevant function(s) ("can_...build...")
3) Read the nation requirement from the ruleset, in the server side (server/ruleset.c)

At this point the server side should already enforce the rule. The client side UI is not aware of the restrictions (so likely it allows you to try illegal things, which server then rejects)

4) Add that requirement to network protocol (common/networking/packets.def, ruleset_unit or some such packet)
5) Bump network protocol capstring (fc_version in the source root directory)
6) Fill that network packet with the new field, in the server side (server/ruleset.c, send_...() )
7) Set the value to unit_type structure from the packet, in the client side (client/packhand.c)

Now also client side should work sensibly.

8) Add documentation comments to supplied rulesets & data/ruledit/comments.txt
9) Add saving the new nation requirement informatino to the rulesets (from ruledit or ruleup) to tools/ruleutil/rulesave.c
cazfi
Elite
Posts: 3069
Joined: Tue Jan 29, 2013 6:54 pm

Re: C coding tutorials

Post by cazfi »

cazfi wrote: Sat Jun 18, 2022 9:02 am 2) Check that requirement in relevant function(s) ("can_...build...")
https://files.freeciv.org/nightly/weekl ... f649c77cb1
Post Reply