Hard Light Productions Forums

Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Test Builds => Topic started by: karajorma on October 30, 2014, 01:32:38 am

Title: SEXP Containers - Now in Beta!
Post by: karajorma on October 30, 2014, 01:32:38 am
Okay, I'm going to call this feature in Beta now. Although I'm likely to add features, what is already there probably isn't going to change much so FREDDERS can feel pretty confident that I'm not going to pull the rug out from under them.

What is this?

For those who didn't read the previous thread, what you've got here is an implementation of two standard C++ features (deques and hash maps). Think of these as variables on steroids in terms of what they can do and how much power they have.

A deque is a double ended queue. That means you basically can make a list of ships, messages, subsystems or any other string or number you wish and then tell FRED "Give me the first / last thing from the list." Basically it's similar to the argument lists you can use with when-argument except that they exist independently of the event and thus can be used in several events at the same time (With an argument list you'd have to copy it to every single event).

A map allows you to specify pairs of variables. The key and the value. That means if you specify Alpha 1 as the key you can simply ask the game to retrieve whatever value you've associated with it (a message, another ship, whatever you want). You could then add Beta 1, 2, 3 etc and have them have different values.

Both containers support multidimentionality, that means you can have a map or deque with each entry being another container.

Although there is still quite a lot to do I'm basically I'm putting this up so that FREDders can play with it and help me find bugs / ask for features. As always, these are more likely to get attention while I still remember code than weeks or months later when I'll have to relearn how it all works. I'll be writing a tutorial on how to do cool stuff with this feature at some point but I wanted to make this available to play with as it's almost certain to still have some bugs due to the way it required quite a large edit of sexp_tree.cpp (It should be a lot more stable in FS2_Open than FRED though).


Since the feature is still in beta the modify container dialogue hasn't been implemented yet (I plan to work on it next!). You can add containers with the Add Container option but if you want to edit them you'll have to do it via notepad (or use the add-to-list / add-to-map SEXPs). Pretty much everything else should work though. I've tried to implement everything so that it looks similar to the way FRED handles variables so that it feels familiar. There are a couple of things that might need some explanation though.

If you have this SEXP

Code: [Select]
when
- true
- self-destruct
-- Ship_List
--- Get First


The game will go into the Ship_List container, get the first ship in the queue and give that to the self-destruct SEXP. If you have this though


Code: [Select]
when
- true
- self-destruct
-- Ship_List
--- Get First
--- Get Last


The game will assume that the first entry in Ship_List is another list and the last entry in that second list is the actual name of the ship. So in order to use multidimentionality you must use Add Data from the menu to add an extra Get First and you must have an entry in the Ship_List which is the name of a list with '&' before and after the name of the container (e.g &Another_Ship_List&).


The other issue people might encounter is when using the new SEXPs that expect the name of a container.

Code: [Select]
when
- is-container-empty
-- Ship_List

If you have this event and you right click on Ship_List you will notice that both the Replace Container and Replace Data options are available. They are not the same! Replace Data is the one you probably want. If you use the Replace Container option the game will assume you mean the contents of the container (i.e you don't want to check if Ship_List is empty, but rather that Ship_List is a multidimentional container and that it contains the name of a second container you want to check). You can tell what the game is thinking because if you use Replace Container it will add a modifier and will instead look like this.

Code: [Select]
when
- is-container-empty
-- Ship_List
--- Get First



Anyway, here's the build (http://fs2downloads.com/Misc-Downloads/Builds/SEXP_Collection_Classes_Build_10.7z) for you FREDders to play with. I can't post the code in here as it actually breaks the forum limit. I'll add it as an attachment though.

If you have any questions, please ask. I really want this feature to get some heavy testing now. Report any bugs you find on this thread or on IRC (if I'm online).

Future features :-
I want to be able to output containers to messages the same way you can use $variable_name in a message to output the value of the variable. Of course, this is likely to be more complicated than with a variable since I'll need to code to pick which entry in the container to output. - Done
Containers need to be tied into the event log. At the moment they don't output anything. - Done
I plan to add persistence to containers in the same way that you can have persistent variables.
I'll be adding more SEXPs to allow you to interact with containers. For instance one SEXP I'll add soon is the ability to output all the keys from a map into a list. - Done
I'll also be adding multiplayer support at some point.

[attachment kidnapped by pirates]

[attachment deleted by nobody]
Title: Re: SEXP Containers - Now in Beta!
Post by: pecenipicek on October 30, 2014, 01:57:15 am
So, in effect, you just implemented PHP's particular flavor of "arrays" into FSO :p


 :yes2: in any case :D
Title: Re: SEXP Containers - Now in Beta!
Post by: Colonol Dekker on October 30, 2014, 02:06:13 am
It made my head hurt but I know that is because coding stuff is my bane.

Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on October 30, 2014, 02:23:12 am
Maps aren't really much more complicated than variables really. In reality all a map is, is a collection of SEXP variables in one package. The advantage is that you can do something like this

when-argument
- Any-of
-- Alpha 2
-- Alpha 3
-- Alpha 4
- <
-- distance
--- Argument
--- SC Sathanas
-- 4000
- Send-Message
-- Argument
-- High
-- Ship_Map
--- Argument

That allows you to have each ship send a different message. You would just make a map and assign each ship a different message
Code: [Select]
Alpha 2 - Alpha 2 Message
Alpha 3 - Alpha 3 Message
Alpha 4 - Alpha 4 Message

I'm sure that's a problem many FREDders have seen before.

Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on October 30, 2014, 06:20:53 am
Thanks to MageKing for testing. Two bugs have now been fixed. I've noticed that using the At modifier is borked though. I'll fix that in a bit. 

EDIT : It should work now. New builds uploaded. Linked in the top post.
Title: Re: SEXP Containers - Now in Beta!
Post by: AdmiralRalwood on October 30, 2014, 01:22:27 pm
So I wanted to make a quick test to make sure things were working, so I created a list and a map defined thusly:
Code: [Select]
$Lists
$Name: Ship_List
$Data Type: String
$Data: ( "Alpha 2" "Alpha 3" "Alpha 4" )

$End Lists

$Maps
$Name: Message_Map
$Data Type: String
$Data: ( "Alpha 3" "Alpha 3 Says Something" "Alpha 4" "Alpha 4 Says Something" "Alpha 2" "Alpha 2 Says Something" )

$End Maps
I then created this (I thought) simple event to go with them:
Code: [Select]
$Formula: ( when
   ( and
      ( not
         ( is-container-empty "Ship_List" )
      )
      ( >
         ( mission-time )
         @Message_Timer[5]
      )
   )
   ( send-message
      &Ship_List& ( "Get_First" )
      "High"
      &Message_Map& ( &Ship_List& ( "Remove_First" ) )
   )
   ( modify-variable
      @Message_Timer[5]
      ( + @Message_Timer[5] 5 )
   )
)
+Name: Send Contained Messages
+Repeat Count: -1
+Trigger Count: 3
+Interval: 1
Upon actually testing this mission, I get Alpha 4 sending a message (at the expected time), and nobody else, no matter how long I wait.
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on October 30, 2014, 08:54:00 pm
Fixed it. The problem is that there are calls to CTEXT in the mission loading code.

I wrapped the call to actually remove entries from the container in a "if (Game_mode & GM_IN_MISSION) " and it worked perfectly after that.

I'll upload new builds in a couple of hours once I'm not on my phone.

EDIT: New build uploaded.
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on November 04, 2014, 10:38:35 am
Build and patch updated.

- Added the new get-map-keys SEXP which will output all the keys from a map into a list.
- Added support for using containers in messages, briefings, debriefings, etc. You need to write the container name in this format &Container_Name&Key_Name& for maps.
For lists use &Container_Name&Modifier& where modifier is one of the list modifiers such as Get_First or Remove_Last. If you want to use the At modifier you must also add an index. Remember that indexes are 0th order so &Container_Name&At1& will supply the 2nd entry in the list you need a 0 not a 1 for the first entry.
- Added preliminary support to output to the event.log whenever a container is used. Same as for variable or arguments.
- Various bug fixes from the thread, IRC or which I found myself.

I've decided I'm going to dump or alter large sections of the current code for the Add Container menu option. I was for the most part copying how the SEXP variable code does it, but the further in I got, the more I realised how deeply limited and stupid the current variable dialogs are (I can only edit or delete one variable at a time? Seriously?). I should at least try to write something better than that. Both Add Container and Modify Container can hopefully be rolled into a single dialog that can add, modify and delete containers as the FREDder wishes. But since I've already given FREDders the Add Container dialog and you can modify containers fairly easily in notepad, it's not a huge priority compared with other things I'd like to see.
Title: Re: SEXP Containers - Now in Beta!
Post by: AdmiralRalwood on November 04, 2014, 12:08:23 pm
- Added support for using containers in messages, briefings, debriefings, etc. You need to write the container name in this format &Container_Name&Key_Name& for maps.
For lists use &Container_Name&Modifier& where modifier is one of the list modifiers such as Get_First or Remove_Last. If you want to use the At modifier you must also add an index. Remember that indexes are 0th order so &Container_Name&At1& will supply the 2nd entry in the list you need a 0 not a 1 for the first entry.
Is there support for lists of lists with this syntax? Can you say &Container_Name&Get_First&Get_First& or something?
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on November 04, 2014, 07:59:31 pm
Not for that one yet. I may add that later though.

EDIT: Added support for multidimentional containers in messages. I'll update the code + builds later though got other things to work on for now.
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on January 20, 2015, 05:00:45 am
:bump:

Looks like it's been a while since I updated this thread. New builds and patch file attached.

The old Add Container, Modify Container, Replace Container paradigm which I copied from variables has been consigned to the dustbin of history. Instead you have an Edit Containers option which allows you to add and edit them (and eventually remove them once I get around to adding a button for that!) and the standard Replace Container to change data into a container. Various other bugs have been fixed too.
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on January 21, 2015, 01:22:05 am
Deleting containers should now work too!
Title: Re: SEXP Containers - Now in Beta!
Post by: Rheyah on March 17, 2015, 11:34:09 am
Hiya,

How recent is the build you used?  I'm looking at using this for a particular event (involving a series of randomised locations which the player must visit in a particular order) but I also have flags which were implemented during January, I think.  Is there any way a more recent build could be used?

I am not familiar with the patching process.
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on March 24, 2015, 10:44:31 pm
I'll try to get an up to date build up by the end of the day. I had some computer issues and haven't reinstalled MSVC or SVN yet. Once I have those it should be pretty easy to build for you.

EDIT : New version based on SVN checked out today available here (http://fs2downloads.com/Misc-Downloads/Builds/SEXP_Collection_Classes_Build_11.7z).
Title: Re: SEXP Containers - Now in Beta!
Post by: LaineyBugsDaddy on April 16, 2015, 02:00:56 pm
kara, correct me if I'm wrong here, but based on your description in the first post, deqs and maps are arrays, and an array is, as you desribed these items, a variable on steroids. Not only should you be able to get the first or last item, it ought to also be possible to get a specific range of items, or to iterate though the array, at least in theory. In practice it would depend on the implementation, obviously.
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on April 16, 2015, 07:57:31 pm
Deques are basically similar to an array but maps are unordered. With a map getting the first object out of the map is going to give you a random object since there is no way to be certain which item will be first. Even when running the same mission again you can't be certain that the map will contain objects in the same order.

Theoretically it is possible to iterate through the entire array via a command similar to Get First, etc. However in practice it proved to be quite hard to implement so I left it up to the FREDder to do it. It is definitely possible to write a SEXP that will iterate through the array.
Title: Re: SEXP Containers - Now in Beta!
Post by: LaineyBugsDaddy on April 20, 2015, 01:48:18 pm
Ah. I missed that maps are unordered. Cool. I didn't even know you could do that. But deqs are definitely a sort of array. Which means that you should not only be able to get first or last, but any item you have the index of, at least in theory. In practice, I have no idea if the system is set up that way.
Title: Re: SEXP Containers - Now in Beta!
Post by: AdmiralRalwood on April 20, 2015, 02:47:50 pm
Ah. I missed that maps are unordered. Cool. I didn't even know you could do that. But deqs are definitely a sort of array. Which means that you should not only be able to get first or last, but any item you have the index of, at least in theory. In practice, I have no idea if the system is set up that way.
There's an "At" modifier for retrieving the entry at a specific index of a list container.
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on April 21, 2015, 07:33:20 am
While we're on the subject, does anyone have the patch file I attached earlier? The most recent version I have is on my broken computer and I don't fancy having to take it apart, sticking the drive in an enclosure and then putting it all back together again when I am able to get the PC fixed.
Title: Re: SEXP Containers - Now in Beta!
Post by: tomimaki on April 21, 2015, 08:14:18 am
This file?

[attachment deleted by nobody]
Title: Re: SEXP Containers - Now in Beta!
Post by: jr2 on April 21, 2015, 11:04:58 am
Out of curiosity, what broke?
Title: Re: SEXP Containers - Now in Beta!
Post by: karajorma on April 21, 2015, 06:33:49 pm
Not sure, it won't turn on. It's still under warrenty so I don't want to fiddle, but this being small town China the guy who does the repairs won't be around for another couple of weeks.

This file?

I don't think that's the newest version but it's definitely newer than the one I had on my netbook. I should be able to work with it and merge in the changes from the other one later.