Author Topic: Linked lists  (Read 855 times)

0 Members and 1 Guest are viewing this topic.

svaucher

  • Veteran
  • *
    • View Profile
    • http://www-etud.iro.umontreal.ca/~vauchers
  • M&B
Linked lists
« on: January 12, 2007, 10:30:20 PM »
This might get a bit technical, but I was wondering the feasibility of creating data structures now, since we have slots and local variables.

This is a bit of a brainstorm:

1/ have a null troop that acts as a placeholder
2/ have a node troop with slot 1 pointing to the next node, or a null troop and slot 2 to the element.

This would simplify iterations significantly because we could simulate closures.
You would invoke a script called "invoke" with two params: the list and a script. The script would be invoked on every element (passing it as the first parameter).

I know there are a few guys here with some coding experience. lisp anyone ;), I'm looking for feedback?

Now for the questions, I haven't modded in a while:

1/ Is there a direct instantiation mechanism (f.e. new troop returned as reg(0)) for troops? or do I need to add them to a party and then retrieve them?
2/ Can slots contain any sort of element?
3/ Has anyone confirmed if recursion functions?
4/ Am I reinventing the wheel?

If this is possible and I implement this, it would arguably become the most boring mod : DATASTRUCTURE MOD !!!

I'm currently trying a bit of code, I'll post what I discover.

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: Linked lists
« Reply #1 on: January 12, 2007, 11:00:38 PM »
This might get a bit technical, but I was wondering the feasibility of creating data structures now, since we have slots and local variables.

This is a bit of a brainstorm:

1/ have a null troop that acts as a placeholder
2/ have a node troop with slot 1 pointing to the next node, or a null troop and slot 2 to the element.

This would simplify iterations significantly because we could simulate closures.
You would invoke a script called "invoke" with two params: the list and a script. The script would be invoked on every element (passing it as the first parameter).

I know there are a few guys here with some coding experience. lisp anyone ;), I'm looking for feedback?

Now for the questions, I haven't modded in a while:

1/ Is there a direct instantiation mechanism (f.e. new troop returned as reg(0)) for troops? or do I need to add them to a party and then retrieve them?
2/ Can slots contain any sort of element?
3/ Has anyone confirmed if recursion functions?
4/ Am I reinventing the wheel?

If this is possible and I implement this, it would arguably become the most boring mod : DATASTRUCTURE MOD !!!

I'm currently trying a bit of code, I'll post what I discover.

I don't know what half of the stuff you're talking about means, or how it relates to M&Bscript, but I'll do what I can.

As far as I can figure out about your description and Wikipedia's entry on linked lists, it might be possible in the following way:

Use two troops arrays; temp_array_a and temp_array_b
Fill a troop slot on temp_array_a with the number of the next node (the next troop slot, I suppose)
Fill the same troop slot on temp_array_b with the data associated with the node

Both these slots can then be easily called from anywhere in the module system.


1) The troops in module_troops are a template for the creation of individual agents. Any troop in any party in-game takes after this template. Making any changes to this troop via the module system, to the module_troops tuple or via operations called in-game, will affect all troops of this type.

2) If I understand the word 'element' right in this context:
Slots can contain any reference to anything in the module system. "itm_horse_meat" is just as valid as slot content as the number 6. With regards to the module system, all modular entries are treated as numbers. For example, if you were to perform the following operation:

(troop_add_item,"trp_player",1),

the player would receive a horse meat, since "horse_meat" is the second entry in module_items.py.

3) Recursion has always worked perfectly. I've been using it since 0.731. There are no problems with calling a script from within itself.

4) I think maybe you are, but you could do some interesting things with it.

Cortically,
Winter

STREET: Losing your mind has never been so much fun . . .

svaucher

  • Veteran
  • *
    • View Profile
    • http://www-etud.iro.umontreal.ca/~vauchers
  • M&B
Re: Linked lists
« Reply #2 on: January 12, 2007, 11:51:12 PM »
Thanks for the answer :)

I don't know what half of the stuff you're talking about means, or how it relates to M&Bscript, but I'll do what I can.

I've got an aversion to global variables and I dislike the general organization of data in the modding system. I want to see if I can make it more elegant.
If I can add datastructures, it could simplify modding in general.

]
As far as I can figure out about your description and Wikipedia's entry on linked lists, it might be possible in the following way:

Use two troops arrays; temp_array_a and temp_array_b
Fill a troop slot on temp_array_a with the number of the next node (the next troop slot, I suppose)
Fill the same troop slot on temp_array_b with the data associated with the node

1) The troops in module_troops are a template for the creation of individual agents. Any troop in any party in-game takes after this template. Making any changes to this troop via the module system, to the module_troops tuple or via operations called in-game, will affect all troops of this type.

I got an answer here form fisheye:
http://forums.taleworlds.net/index.php/topic,15499.15.html

The problem with troop arrays is that you cannot dynamically create new ones since they're global. It's possible to have some
code to create lists of dynamic size from arrays, so that might be a possibility.

Has anyone written some scripts to use these arrays?

I see that it is possible to spawn parties. Since these parties have unique ids, I assume their slots are not shared, so you could always spawn a party and use its slots.

Anyone know if there's a limit on how many parties we can have?
Also the disabled_flag, does it destroy the party, or could we spawn disabled parties to use their slots?

2) If I understand the word 'element' right in this context:
Slots can contain any reference to anything in the module system. "itm_horse_meat" is just as valid as slot content as the number 6. With regards to the module system, all modular entries are treated as numbers. For example, if you were to perform the following operation:

I was hoping so. That means you can add scripts to parties which is very sweet :)

I had been working on adding messengers (1 year ago). Depending on the message you'd send you could store the script of what should be done when the messenger arrives to it's destination or is intercepted. Without slots it was too much of a kludge :(

4) I think maybe you are, but you could do some interesting things with it.

I know they could be interesting, but is hacking MBscript to make it work worth it and can it support it since it might be heavy.
« Last Edit: January 13, 2007, 12:38:41 AM by svaucher »

Hellequin

  • Sergeant at Arms
  • *
    • View Profile
Re: Linked lists
« Reply #3 on: January 13, 2007, 12:43:14 AM »
Given that scripts are assigned numerical values like everything else, yes, what you describe should be quite possible.  So, for instance:

[In module_constants.py:]
Code: [Select]
slot_script_on_intercept = 30
slot_script_on_arrival = 31

[In an operations block when the messenger is created:]
Code: [Select]
(spawn_around_party, ":Source_town", "pt_messenger"),                                            # Stores the partyID of the spawned party in reg0.
(assign, ":Messenger", reg0),                                   # For clarity and safety - reg0 is not a safe storage spot, it's written to secretly too often.
(troop_set_slot, ":Messenger", slot_script_on_intercept, "script_bite_cyanide_capsule"),
(troop_set_slot, ":Messenger", slot_script_on_arrival, "script_mobilize_the_peasant_militia"),

[In a different operations block when he arrives at his destination:]
Code: [Select]
(troop_get_slot, ":Messenger_script", ":Messenger_that_just_arrived", slot_script_on_arrival),
(call_script, ":Messenger_script", ":This_town"),

... and so forth.

Yes, I think this should work fine, and it's a good thought - one which hadn't occurred to me.  I don't think it will exactly make anything possible which wasn't before, but it may make certain code much cleaner.  Obviously test first...

- Hellequin

svaucher

  • Veteran
  • *
    • View Profile
    • http://www-etud.iro.umontreal.ca/~vauchers
  • M&B
Re: Linked lists
« Reply #4 on: January 13, 2007, 01:20:25 AM »
Hellequin - I was doing that in 7.11 without slots, with global variables, then items. Unless something has deteriorated, it should still work.

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: Linked lists
« Reply #5 on: January 13, 2007, 01:59:46 AM »
The problem with troop arrays is that you cannot dynamically create new ones since they're global. It's possible to have some
code to create lists of dynamic size from arrays, so that might be a possibility.

Has anyone written some scripts to use these arrays?

I've used many arrays for many different things. What exactly are you looking for?


Quote
I see that it is possible to spawn parties. Since these parties have unique ids, I assume their slots are not shared, so you could always spawn a party and use its slots.

Yes, that's entirely possible.


Quote
Anyone know if there's a limit on how many parties we can have?

No idea, testing would be required.


Quote
Also the disabled_flag, does it destroy the party, or could we spawn disabled parties to use their slots?

Neither pf_disabled nor the disable_party can destroy a party. Only remove_party can do that. Disabled parties are still parties and fully accessible, they're just disabled and don't exist on the world map.


Quote
I was hoping so. That means you can add scripts to parties which is very sweet :)

If you wanted to, yeah.

Fatalistically,
Winter

STREET: Losing your mind has never been so much fun . . .

svaucher

  • Veteran
  • *
    • View Profile
    • http://www-etud.iro.umontreal.ca/~vauchers
  • M&B
Re: Linked lists
« Reply #6 on: January 13, 2007, 06:42:47 AM »
The problem with troop arrays is that you cannot dynamically create new ones since they're global. It's possible to have some
code to create lists of dynamic size from arrays, so that might be a possibility.

Has anyone written some scripts to use these arrays?

I've used many arrays for many different things. What exactly are you looking for?
[/quote]

It's a bit complicated to explain and rather perverse. I'll see if I can get my stuff to work as I want it to, then I'll add the application.

Quote
I was hoping so. That means you can add scripts to parties which is very sweet :)
If you wanted to, yeah.

This is actually very powerful. Most modern programming languages provide this sort of functionality. It's kind of hard to explain these since I've been coding for >10 years and it seems perfectly natural to me. I'll give you two scenarios, I hope will cause for reflection (which are superb, I might add)  :

1/ If you have a party, you can give it a destination, behaviour, and so on, but for more complex actions, you'll have probably need to code stuff with conditional blocks. It's generally (programming-wise) to keep those actions close to the actor as possible and in small blocks that can be substituted.

for example, f you are commanding the Swadians, you could have general actions that you could ask of any Swadian troops you meet. You could tell it to go to a city and join the garrison. It could simply go there and execute a script stored in a specified slot. If you asked it to attack an enemy city, you'd only have to change the script, but not the code to get there.

2/ You could easily modularise a mod using this technique. For example, if you had a convention of executing the script of every NPC in the party in slot X everytime you entered a city. The script would verify the condition to make sure it should do something.

In any case, when I find time, I might continue my strategy-based M&B which is organised heavily following this principle. I'll have to take it out of moth-balls ;)

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: Linked lists
« Reply #7 on: January 13, 2007, 11:30:55 AM »
This is actually very powerful. Most modern programming languages provide this sort of functionality. It's kind of hard to explain these since I've been coding for >10 years and it seems perfectly natural to me. I'll give you two scenarios, I hope will cause for reflection (which are superb, I might add)  :

1/ If you have a party, you can give it a destination, behaviour, and so on, but for more complex actions, you'll have probably need to code stuff with conditional blocks. It's generally (programming-wise) to keep those actions close to the actor as possible and in small blocks that can be substituted.

for example, f you are commanding the Swadians, you could have general actions that you could ask of any Swadian troops you meet. You could tell it to go to a city and join the garrison. It could simply go there and execute a script stored in a specified slot. If you asked it to attack an enemy city, you'd only have to change the script, but not the code to get there.

Fair enough. I can't think of anything I'm able to do now that I wasn't before, but then I've been using the module system for a while. :-P


Quote
In any case, when I find time, I might continue my strategy-based M&B which is organised heavily following this principle. I'll have to take it out of moth-balls ;)

Actually, what you describe is very similar to the way Storymod Chapter 2 handles things, which is strategy-based M&B. You should consider joining our team.

Intersegmentally,
Winter

STREET: Losing your mind has never been so much fun . . .

Hellequin

  • Sergeant at Arms
  • *
    • View Profile
Re: Linked lists
« Reply #8 on: January 13, 2007, 08:48:38 PM »
"Intersegmentally"?

Sir, you go too far.  The honour of the English language can take no more such insult.  Choose your second.

(I choose 12:34:05.  Always liked him.)
- Hellequin