Author Topic: PYTHON SCRIPT/SCHEME EXCHANGE  (Read 112113 times)

0 Members and 4 Guests are viewing this topic.

Angarato

  • Regular
  • *
    • View Profile
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #15 on: March 28, 2006, 02:15:16 AM »
Don't we already have one?
"I don't like half of you half as well as I should like, and I like less than half of you half as well as you deserve." --Bilbo Baggins

Raybur Ravenloc

  • Regular
  • *
    • View Profile
  • Faction: Sarranid
  • MP nick: Greenwolf
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #16 on: March 28, 2006, 03:05:45 AM »
wiki might work better, but it would be up to someone else, cause i have zero experience with wikipedia, feel free to do it, and since i have only gotten one positive response so far, i think its safe for me to take down my poor attempt..
rawr

Deus Ex

  • Elder Statesman
  • Administrator
  • *
  • Poor Impulse Control
    • View Profile
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #17 on: March 28, 2006, 04:33:29 AM »
*I* thought it was a good idea...

DE
What are you looking here for??  What are you looking for here??

Vhaeos

  • Veteran
  • *
    • View Profile
    • Calradia.com
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #18 on: April 04, 2006, 11:11:01 PM »
well, i was thinking it would be nice to have a page full of code snippets with small descriptions of what they do for coders to just slap in their mod
i have something like 10Mb of space for a website, i could use that, and post up whatever code modders send to me via a post here, a PM, or an email
not an HTML master, so it won't be pretty unless someone wants to show me a better example and how to update it

heres what I slapped up in about five minutes (after brushing up on HTML tags)
just used the sample fujiwara put up.. guess i should put credits to him too.. guess i will if anyone actually wants to use this, otherwise i'll just take it down
http://myweb.bloomu.edu/bpdowd/M&Bcode.htm

so what do you think? it can be changed however, this is just something i spent about ten minutes thinking about and putting up
probably would add various pages and organization if it picked up, put up the snippets in whatever format they are given to me or i am asked to put them in..

You should make it a wiki. Would be much better and a lot less work.

Agreed, a Wiki would work best for a place to post code snippets. Why not take advantage of the Calradia.com free hosting for it?

The Mount&Blade Mod Wiki would probably be suited well enough for that. If anyone wants to try doing that, feel free. If there's something you can't do due to lack of privledges, just private message me and I'll try to get it done for you.

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #19 on: April 12, 2006, 09:42:21 AM »
Code: [Select]
  [anyone,"prisoner_chat", [], "What? You've already put me in chains, what more do you want to do?", "prisoner_chat_2",[]],
  [anyone|plyr,"prisoner_chat_2", [], "Well, as it happens, you look rather tasty . . . Let's have a bite.", "prisoner_chat_3",[]],
  [anyone|plyr,"prisoner_chat_2", [], "Nothing.", "close_window",[]],
  [anyone,"prisoner_chat_3", [], "No! Nooooooooo!", "prisoner_chat_4",
[
(store_conversation_troop,reg(3)),
(remove_troops_from_prisoners,reg(3),1),
(try_begin),
(store_free_inventory_capacity,reg(4),"trp_player"),
(ge,reg(4),1),
(troop_add_item,"trp_player","itm_dried_meat"),
(end_try),
]],
  [anyone|plyr,"prisoner_chat_4", [], "There. Someone bring me my boning knife!", "close_window",[]],

Because the people in the Suggestions forum kept asking for it. Anyone who wants it, feel free.

Wastefully,
Winter

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

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #20 on: April 19, 2006, 08:26:37 PM »
For conditionally adding Marnid and/or Borcha to a scene depending on if they're in your party:

Code: [Select]
(modify_visitors_at_site,"scn_<your scene here>"),
(reset_visitors),
(try_begin),
(main_party_has_troop,"trp_borcha"),
(set_visitor,<mission template entry number>,"trp_borcha"),
(end_try),
(try_begin),
(main_party_has_troop,"trp_marnid"),
(set_visitor,<different mission template entry number>,"trp_marnid"),
(end_try),

Electrically,
Winter

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

Fujiwara

  • Sergeant at Arms
  • *
    • View Profile
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #21 on: May 16, 2006, 07:23:41 PM »
For conditionally adding Marnid and/or Borcha to a scene depending on if they're in your party:

Code: [Select]
(modify_visitors_at_site,"scn_<your scene here>"),
(reset_visitors),
(try_begin),
(main_party_has_troop,"trp_borcha"),
(set_visitor,<mission template entry number>,"trp_borcha"),
(end_try),
(try_begin),
(main_party_has_troop,"trp_marnid"),
(set_visitor,<different mission template entry number>,"trp_marnid"),
(end_try),

Electrically,
Winter

And for any number of heroes in the party:

Code: [Select]
(modify_visitors_at_site,"scn_<your scene here>"),
(reset_visitors),
(try_for_range,reg(1),heroes_begin,heroes_end)),
(main_party_has_troop,reg(1)),
(assign,reg(2),<inital mission template entry number>),
(set_visitor,reg(2),reg(1)),
(val_add,reg(2),1),
(end_try),

Thanks for the brainstorm, Winter :)
M&B: Onin no Ran - THE Japan Mod

Pythonic Module Editor

Johnny is a hacker's son
But Johnny is not 133t
What Johnny thought was ADD_AND_SHIFT
Instead was REG_DELETE

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #22 on: May 16, 2006, 08:35:27 PM »
And for any number of heroes in the party:

Code: [Select]
(modify_visitors_at_site,"scn_<your scene here>"),
(reset_visitors),
(try_for_range,reg(1),heroes_begin,heroes_end)),
(main_party_has_troop,reg(1)),
(assign,reg(2),<inital mission template entry number>),
(set_visitor,reg(2),reg(1)),
(val_add,reg(2),1),
(end_try),

Thanks for the brainstorm, Winter :)

No prob, but it's not so good to add your own mod code to scripts in the script exchange, Fujiwara. heroes_begin and heroes_end are mod constants, not found in the Native module system. You should really point that out in your post.

Other than that, nice script. I love all the crazy things try_for_range can do.

Futilitarianly,
Winter

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

Janus

  • *spicy* *camper*
  • Administrator
  • *
    • View Profile
    • Mount&Blade Unofficial File Repository
  • Faction: Rhodok
  • M&BWBWF&S
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #23 on: May 17, 2006, 05:51:49 AM »
I'm posting a script here for other more serious scripters. The purpose of this script is to get a list of every troop stack in a given party along with of course the number of troop stacks. It currently only counts the non-prisoner troops (I think), but could be adapted to count all troops or only prisoners if needed.
If you don't know why this would be helpful, the script is probably not for you.

Included with the main script is an example script using it, which will print a list of troop stacks in the player's party on-screen. This isn't a particularly useful thing to do, but serves as a good example and base for whatever you need the script to do for you.

Special thanks to fisheye for a bit of scripting help on a more elegant way to store and incrementally reference variables using the slots of a dummy troop.

Add these lines to the end of the troop list in module_troops.py. They should be after the last troop but before the ending bracket:
Code: [Select]
#do not place a troop past this point, it marks the end of the troops
["end_troops","end_troops","end_troops",tf_hero,no_scene,reserved,fac_commoners,[],def_attrib,0,knows_common,0],

Add these string into module_strings.py, they're just used by the script example:
Code: [Select]
  ("party_contains","The party '{s3}' contains {reg9} troop stacks:"),
  ("string3","{s3}"),

Add these scripts into module_scripts.py:
Code: [Select]
  ("list_party_troops", [ #this script will check the target party (stored in "$party_to_check") and return a list of the troop types in the party
   (try_begin),[assign,"$party_stack_count",0],
(try_for_range,reg(6), "trp_player", "trp_end_troops"),
[party_count_companions_of_type, reg(7), "$party_to_check", reg(6)], [gt, reg(7), 0],
[troop_set_slot,"trp_end_troops","$party_stack_count",reg(6)],
[val_add, "$party_stack_count", 1],
(end_try),
#at this point, "$party_stack_count" contains the number of troop stacks in the "$party_to_check" party; "$party_stack_count" is also to be used as the end value for
#a try_for_range loop (1 higher than the last reg with troop data), then the troop slots for the "trp_end_troops" dummy entry contain the troop id of each stack in the party.
   (end_try),
  ]),

 
  ("player_has_troops", [ #this script is an example of how to use the list_party_troops script; it will determine and print out the player's troop list on screen
   (try_begin), [assign, "$party_to_check", "p_main_party"], #set "$party_to_check" to the player's party, used for input in the list_party_troops script
    (call_script, "script_list_party_troops"), #run the script to get a list of troops in the player's party
    [str_store_party_name, 3, "$party_to_check"],(display_message, "str_party_contains"), #print out the string indicating the party name and number of troop slots
(try_for_range, reg(6), 0, "$party_stack_count"), #loop through each troop. "$party_stack_count" contains the total number of troop references, thus where to end the loop
[troop_get_slot,reg(7), "trp_end_troops", reg(6)], [str_store_troop_name, 3, reg(7)], (display_message, "str_string3"),
#the list of troop references were stored in the dummy troop "trp_end_troops", starting with slot 0. Reg(7) is set to the current troop reference,
#then the name of the troop is obtained and stored in the temporary string s(3). The troop name is then printed to the screen.
(end_try),
   (end_try),
  ]),


Finally, add this into an operation block (in a dialog, a menu, a trigger, or wherever you want) to run the script example:
Code: [Select]
(call_script,"script_player_has_troops")


EDIT: updated the script to not rely on reg(5) and reg(9) but instead use its own variables, "$party_to_check" and "$party_stack_count".
« Last Edit: May 18, 2006, 05:49:50 AM by Janus »
Tomas asked Dolgan, "What place is this?"
The dwarf puffed on his pipe. "It is a glory hole, laddie. When my people mined this area, we fashioned many such areas."
     - Raymond E. Feist, Magician: Apprentice  (Riftwar Saga)

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #24 on: May 17, 2006, 02:22:21 PM »
I'm posting a script here for other more serious scripters. The purpose of this script is to get a list of every troop stack in a given party along with of course the number of troop stacks. It currently only counts the non-prisoner troops (I think), but could be adapted to count all troops or only prisoners if needed.
If you don't know why this would be helpful, the script is probably not for you.

Ha, your counting script is the exact same one I've been using for my new Craftmod features! Now we know why you wanted to increment registers, very clever.

Unfortunately the code where I've used my scripts doesn't quite work yet, due to the fact that I need some operations from the new version of M&B, but when that arrives let me assure you you're going to love it. ;)

Pilocarpinely,
Winter
« Last Edit: May 17, 2006, 02:25:42 PM by Winter »

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

Fujiwara

  • Sergeant at Arms
  • *
    • View Profile
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #25 on: May 17, 2006, 07:43:56 PM »
No prob, but it's not so good to add your own mod code to scripts in the script exchange, Fujiwara. heroes_begin and heroes_end are mod constants, not found in the Native module system. You should really point that out in your post.

Good point. Thanks :)

Quote
Other than that, nice script. I love all the crazy things try_for_range can do.

Futilitarianly,
Winter

I'd be dead in the water without try_for_range :)

Janus, brillant idea using trp_end_troops as a dummy storage unit. I've done the same thing with dummy scenes, storing various bits of data needed to change item abundance based on season, town location, and town size. I can definitely use this script. good job!

M&B: Onin no Ran - THE Japan Mod

Pythonic Module Editor

Johnny is a hacker's son
But Johnny is not 133t
What Johnny thought was ADD_AND_SHIFT
Instead was REG_DELETE

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #26 on: May 17, 2006, 07:52:20 PM »
Janus, brillant idea using trp_end_troops as a dummy storage unit. I've done the same thing with dummy scenes, storing various bits of data needed to change item abundance based on season, town location, and town size. I can definitely use this script. good job!

You should see what I've managed to cook up with it . . . ;)

I've integrated it with the rest of my new feature to create something really astounding. Watch out for the next release for Craftmod -- I've coded some interim scripts that will let me move up the timetable. All I need to do is a little more scene design and a little more mission template code, and we'll be ready for the final beta.

Obeisantly,
Winter

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

Janus

  • *spicy* *camper*
  • Administrator
  • *
    • View Profile
    • Mount&Blade Unofficial File Repository
  • Faction: Rhodok
  • M&BWBWF&S
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #27 on: May 18, 2006, 03:35:01 AM »
I've got a few small changes to it to make it slightly more friendly by making it use it's own variables instead of registers.

I've also got another script which uses it that I'll post in this spot (by modifying the post) later after I get it tested. I'll update the post above at the same time, of course.

--------------------
My post above is updated, and the changes are needed for the below script to work.

Ok, the following script is to be placed in module_scripts.py and requires my script posted above.
The purpose of the script is to determine the highest skill values in the player party for every skill by checking every member of the party, including non-heroes. So, for instance, if you want to check the highest "pathfinding" skill in the party and in this case the party contains a stack of "trailblazer" units with very high pathfinding skills (higher than any other unit in the party), their pathfinding skill will be what is returned. Note that the script doesn't give the -1 to skill that .750 introduced for party skills of units other than the player (it gives the exact skills), and doesn't limit itself to only heroes. It can however be modified to do so easily enough, if that is your desire.
This script returns all 21 skills for the party. The usage is documented decently by the notes in the script.
I have tested the script with a few party configurations and verified that all 21 skills were properly identified for the party in each case.

So, without further ado, here's the script to go in module_scripts.py:
Code: [Select]
  ("read_party_skills", [ #this script determines the highest skill values in the player party for every skill by checking every member of the party, including non-heroes
   (try_begin),
    [assign, "$party_to_check", "p_main_party"], (call_script,"script_list_party_troops"),
[assign,reg(0),0],[assign,reg(1),0],[assign,reg(2),0],[assign,reg(3),0],[assign,reg(4),0],[assign,reg(5),0],[assign,reg(6),0],
[assign,reg(7),0],[assign,reg(8),0],[assign,reg(9),0],[assign,reg(10),0],[assign,reg(11),0],[assign,reg(12),0],[assign,reg(13),0],
[assign,reg(14),0],[assign,reg(15),0],[assign,reg(16),0],[assign,reg(17),0],[assign,reg(18),0],[assign,reg(19),0],[assign,reg(20),0],
(try_for_range, reg(23), 0, "$party_stack_count"), #loop through each stored troop reference. reg(9) contains the total number of troop references, thus where to end the loop
[troop_get_slot,reg(21), "trp_end_troops", reg(23)],
[store_skill_level,reg(22),0,reg(21)],[val_max,reg(0),reg(22)],[store_skill_level,reg(22),1,reg(21)],[val_max,reg(1),reg(22)],
[store_skill_level,reg(22),2,reg(21)],[val_max,reg(2),reg(22)],[store_skill_level,reg(22),3,reg(21)],[val_max,reg(3),reg(22)],
[store_skill_level,reg(22),4,reg(21)],[val_max,reg(4),reg(22)],[store_skill_level,reg(22),5,reg(21)],[val_max,reg(5),reg(22)],
[store_skill_level,reg(22),6,reg(21)],[val_max,reg(6),reg(22)],[store_skill_level,reg(22),7,reg(21)],[val_max,reg(7),reg(22)],
[store_skill_level,reg(22),8,reg(21)],[val_max,reg(8),reg(22)],[store_skill_level,reg(22),9,reg(21)],[val_max,reg(9),reg(22)],
[store_skill_level,reg(22),10,reg(21)],[val_max,reg(10),reg(22)],[store_skill_level,reg(22),11,reg(21)],[val_max,reg(11),reg(22)],
[store_skill_level,reg(22),12,reg(21)],[val_max,reg(12),reg(22)],[store_skill_level,reg(22),13,reg(21)],[val_max,reg(13),reg(22)],
[store_skill_level,reg(22),14,reg(21)],[val_max,reg(14),reg(22)],[store_skill_level,reg(22),15,reg(21)],[val_max,reg(15),reg(22)],
[store_skill_level,reg(22),16,reg(21)],[val_max,reg(16),reg(22)],[store_skill_level,reg(22),17,reg(21)],[val_max,reg(17),reg(22)],
[store_skill_level,reg(22),18,reg(21)],[val_max,reg(18),reg(22)],[store_skill_level,reg(22),19,reg(21)],[val_max,reg(19),reg(22)],
[store_skill_level,reg(22),20,reg(21)],[val_max,reg(20),reg(22)],
(end_try), #The party's skills are stored in reg(0)-reg(20) at this point, so don't change those registers until you've checked what you need from them.
   (end_try), #You can check each skill by referring to "reg(skl_trade)", "reg(skl_leadership)", and so forth as listed in header_skills.py, as long as you have the file
  ]), # in question set to import from header_skills.py at the top (in other words, "from header_skills import *" at the top of the file your "reg(skl_trade)" reference is in)

EDIT: I could have stored those values in another dummy troop and slimmed this script down, but for simplicity of usage I decided to leave it using reg(0)-reg(20).

EDIT 2: updated to use val_max as fisheye suggested.
« Last Edit: May 18, 2006, 09:30:47 AM by Janus »
Tomas asked Dolgan, "What place is this?"
The dwarf puffed on his pipe. "It is a glory hole, laddie. When my people mined this area, we fashioned many such areas."
     - Raymond E. Feist, Magician: Apprentice  (Riftwar Saga)

fisheye

  • Back in Black
  • Grandmaster Knight
  • *
    • View Profile
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #28 on: May 18, 2006, 06:08:29 AM »
Janus: Instead of (try_begin), (gt, ) blah blah blah, you can do it all in one operation with val_max.

Here's a fragment of the buying code in my NPCs mod:

Code: [Select]
     ## first get highest trade skill in party
    (assign, reg(14),0),
    (try_for_range,reg(12),"trp_marnid","trp_ymira"),
      (main_party_has_troop, reg(12)),   
      (store_skill_level, reg(13),skl_trade,reg(12)),
      (val_max, reg(14),reg(13)),
    (end_try,0),
« Last Edit: May 18, 2006, 06:11:28 AM by fisheye »

Winter

  • I am Tek Jansen!
  • Developer
  • *
    • View Profile
    • STREET: A Cyberpunk Trilogy
  • Faction: Neutral
Re: PYTHON SCRIPT/SCHEME EXCHANGE
« Reply #29 on: May 18, 2006, 06:37:58 AM »
Janus: Instead of (try_begin), (gt, ) blah blah blah, you can do it all in one operation with val_max.

Would you mind explaining what val_max does? And the other strange val operations, val_mod and val_min? I'm a retard when it comes to maths, and I cannot figure it out.

Subversively,
Winter

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