Page 1 of 1

Arrays and functions

Posted: Wed Nov 02, 2016 2:37 pm
by Donfrow
Hi,

Hopefully this isn't a dumb question, but I can't seem to figure it or find it in documentation but it's completely possible I'm missing it.

How would I go about declaring an array? I have tried a few things but am always getting some sort of syntax errors. I'm hoping to pass arrays into functions and use them to make a generic buy/sell function that I could use for all NPC merchants.

Would this even be feasible? In my head, I would have a function where a few arrays are passed into it.

Example: buyItem(itemlist{}, itemlistprice{}, itemlistconversename{})

where I would then use a loop to populate converse lists and prices based on the number of elements. The thing that is keeping me from trying this is my failure to declare an array. However, I'm not sure my idea would even work? Is it possible to populate a converse list using an array, ie case itemlistconversename{1} (remove):

Thanks

Re: Arrays and functions

Posted: Wed Nov 02, 2016 7:13 pm
by marzo
Any usecode variable can be converted to/from an array at any point:

Code: Select all

var myVar = 0; // not an array
myVar[1] = "This works"; // now an array with two elements
myVar[2] = [1, "test", item]; // an element in an array can also be an array 
myVar = "Done"; // not an array anymore
item.say(myVar[0]); // but this will still work, and "Done" will be said

Re: Arrays and functions

Posted: Wed Nov 02, 2016 10:18 pm
by Donfrow
Thanks, I guess this then goes back to my lack of knowledge of programming.

If I were to store values in the arrays, do I need to then use switches to use it in the case statements?

var myVar;
myvar[0] = "Test";
myvar[1] = "Test2";

as I can't simply do

case myvar[1] (remove):
say "You used test2";

I'm in the process of trying to figure some of this out, but I guess it seems I may have gotten a bit overboard with my idea as trying to read up how this may work seems to talk about switches and enumeration?

Re: Arrays and functions

Posted: Wed Nov 02, 2016 11:17 pm
by Donfrow
Given the more I read, though not specific to usecode c since it's built for Exult, it sounds like what I want to do wouldn't be possible.

Am I correct that there would be no way to use a variable in a case statement? My other readings, not specific to usecode c, seem to indicate that in case statement you cannot use a variable and it can only be a constant?

Re: Arrays and functions

Posted: Thu Nov 03, 2016 2:58 pm
by marzo
You can't use it like that, no; cases in a converse block need to be string literals because of the way they were stored and checked in the original games (basically, a pointer to where the string is stored in the usecode function's header).

Although, to be fair, Exult and UCC could be extended to support such an usage.

Re: Arrays and functions

Posted: Fri Nov 04, 2016 2:45 pm
by Donfrow
Guess my idea for a generic buy/sell script won't work out in that case but not a big deal. As I get back into this I'm trying to make things better than I had in the past, including reusable and generic functions.

At least now I know how to declare an array, if needed.

Thanks!

Re: Arrays and functions

Posted: Fri Nov 04, 2016 6:40 pm
by Knight Captain
I could have sworn some of the buy/sell is done as generic functions way in the originals.

Re: Arrays and functions

Posted: Fri Nov 04, 2016 7:17 pm
by Donfrow
That very well may be. Is it possible to look at converted original usecode that works with the new usecode? I have never even tried to find that out as I assumed it was different and the usecode we could write was additions on top of it. If possible, does that mean at the original coding and add/remove things from it? Up until this point I had assumed all the fixes and mods and such replaced the original with new code rather than correct the original.

If this is possible that would be great. Since I'm using SI my beds don't work when in the area of the map that was originally the dream realm; as I recall Marzo had let me know that the bed shape had some code to check if you were in the map location of the dream realm. I was going to try and rewrite the bed code but never quite got it working correctly. If it's possible that I can simply copy the original bed code and replace it without the location limitations, that would probably be the better way for me to do it rather than continue to butcher it.

Same thing goes for guards attacking as such in friendly areas. One of my areas is in the area that used to be Fawn so any time you attack a friendly the Fawn guards still pop up to apprehend the Avatar. I've been trying to minimize this by making it less likely to be an area you would attack friendly NPC's but if I could remove this altogether that would be even better.

I suspect some of this goes beyond my limited knowledge though and because of that I've kind of thought about it as a "well, this is just something fun for me to do, guess it will remain that way" type of thing.

Re: Arrays and functions

Posted: Fri Nov 04, 2016 11:52 pm
by Knight Captain
Marzo has made strides in making a better Usecode Extractor, but it's by no means complete for one-to-one extract/recompile. The file is ucxt.exe.

From looking a the exported file, I can see both vertical and horizontal bed shapes point to Func087F. There Func0994 is used to UI_get_object_position(AVATAR) and then compared against Func0993, which looks to be a locations list. My guess is that this also prevents sleeping in other areas.

Re: Arrays and functions

Posted: Sat Nov 05, 2016 1:33 am
by Donfrow
I wonder if it's possible to copy it from BG then. I don't think it had any of the limitations that SI had.

I will take a look at that ucxt.exe and see if I can figure that out!

Re: Arrays and functions

Posted: Fri Apr 14, 2017 7:31 pm
by Knight Captain
The transaction functions in SI are pretty complex. The order is roughly:

NPC -> Started_Talking, choose Buy option.
buyFromWhoever has the arrays with the shapes, prices, etc.
Spell sales check for the spellbook. (0x96F)
Depending on the transaction code, haggling may be involved.

The easier var simpleSellsFor 0x967 is also included in SI.

I haven't torn apart any of the buyFromWhoever functions, but can if needed.

Re: Arrays and functions

Posted: Mon Apr 17, 2017 4:04 pm
by Donfrow
Based on what you're saying it uses unique functions per NPC?

What I ended up doing is grouping together similar merchants with a generic buy function, so anyone who wants to sell metal weapons (daggers, swords, halbreds, etc) all use the same sellMetalWeapons and I pass the individual price from the NPC,

ie, sellMetalWeapons(daggerCost, swordCost,maceCost, halbredCost)

They all say the same thing if you don't have enough gold, choose not to sell, etc, but it's a minor inconvenience to have a simplified merchant transaction.

Re: Arrays and functions

Posted: Mon Apr 17, 2017 11:44 pm
by Knight Captain
Correct.

Maybe you'll share some of the code in a repo on GitHub?

I think BG used something more like what you wrote.

Re: Arrays and functions

Posted: Wed Apr 19, 2017 5:42 pm
by Donfrow
I can do that (when I figure out how to use GitHub, never used it before) however I never finished all the vendor options yet as I've put a sort of pause on the coding in general at the moment. I'm taking a different approach of trying to design the entire empty world and then start coding from there, in an attempt to avoid some of the pitfalls and bugs that arose in Glimmerscape because of doing 5 different things at once.

Getting it up there will actually be a good thing though. As I take a look at it all of my variable and function names are terribly inconsistent and I really need to go through and standardize them (halbred_cost, arrowCost, buyMetalArmor, buy_potions, etc) so I will attempt to standardize them before putting anything up. Since the general "proper" naming from what I'm aware is firstSecond I will attempt to correct all of them to that format before putting any up.

As part of this I will also need to finish off the number of sell functions for selling all the loot off of the fallen foes... finally a chance to sell all that leather armor looted off trolls.