jik
Knight
OK here is the working code if any one is interested. The opening comment will fill you in on how it works. As preparation for this script, I added the disable flag to all the villages, as well as commented out the section of the script that binds them (do a search for slot_village_bound_center). I have left in the test messages and test code blocks, but they have comments indicating where they are. As it is, the AI can use this as it takes a party as a param, though it will be up to you how you would handle when and where the AI plans to found a village. My final code will have other resource stipulations as well.
Here is the code:
Code:
# script_found_village. This script will move an unbound town to the party's(past param) location. Currently it is set to require that
# you are NOT within 5 distance of another village or town, that you are within 15 distance of a town you own, and there are unbound villages available.
# Input : party that is founding the village, though may not let AI use this code, may hard code AI villages and hide them.
# Output : none
# If the script finds a village, it becomes marked as bound.
("found_village",
[(store_script_param,":party_no",1), #will most likely be p_main_party, unless I get AI to work
(assign,":good_spot", 0), #if a proper spot is found, this will change to 1
(party_get_current_terrain,":cur_terrain",":party_no"),
(assign,reg24,":cur_terrain"), ###TEMP USAGE TEST. reg24 is used below!
(display_message,"@Current terrain type is {reg24}"),
###TERRAIN CHECK. USE this_or_next to broaden what terrains are allowed (this_or_next|eq|neg,...)
(try_begin), ##Terrain testing.
#(eq,1,1), ##UNCOMMENT THIS LINE AND COMMENT THE NEXT TO BYPASS TERRAIN TESTING
(this_or_next|eq,":cur_terrain",rt_plain), ##If you are not on plains (steppe, forest, water, desert... See header_terrain_types.py)
(eq,":cur_terrain",rt_snow), ##If you are not on plains (steppe, forest, water, desert... See header_terrain_types.py)
###End of TERRAIN CHECK
(try_for_range,":close_town",towns_begin, towns_end),
(store_distance_to_party_from_party,":cur_dist", ":party_no", ":close_town"), #see if it is close enough
(assign,":closest_dis", 15), ##Keep track of the closest town incase the last one checked is farther.
(try_begin),
(le, ":cur_dist", ":closest_dis"), #must be within 15 distance of one of your towns
(assign,reg24,":cur_dist"), ##Part of the below test messaging
(try_begin),
(party_slot_eq, ":close_town",slot_town_lord, ":party_no"), ##You own a town close enough, continue
(assign,":closest_dis",":cur_dist"), ##Mark this as the closest. Only check others that are closer
(str_store_party_name,s26,":close_town"), ##record the name of the town you are closest to
(party_get_slot,reg27,":close_town",slot_town_lord),
(str_store_troop_name,s27,reg27),
(display_message, "@distance measured is {reg24} from you to {s26}",0xAA0000), ###TEST MESSAGE. How far is it going?
(display_message, "@{s26}'s owner is {s27}. This should be you!"), ###TEST MESSAGE
(assign,":good_spot",":close_town"), ##looks good so far... Keep track of the closest town that you own
(try_for_range,":village_town",towns_begin, villages_end),
(try_begin),
(eq|neg, ":good_spot",-1), ##once failure state is hit, no use doing the rest...
(store_distance_to_party_from_party,":too_close", ":party_no", ":village_town"), ##make sure it's not too close to anything
(try_begin),
(le,":too_close",5), ##needs to be outside of 5 distance from other towns/villages
(assign,":good_spot",-1), ##Too close, -1 will indcate this
(display_message,"@TOWN or VILLAGE too close!",0x0000ff), ###TEST MESSAGE
(try_end),
(try_end),
(try_end), ##end of the :village_town too close loop
(try_end),
(try_end),
(try_end), ##end of the your town :close_town loop
(try_begin),
(ge, ":good_spot", 1),
(assign,":founded",0), ## last check to make sure there are enough villages to go around.
(try_for_range,":new_village",villages_begin, villages_end),
(try_begin),
(eq,":founded",0), ##this will change once a usable village is found
(try_begin),
(party_slot_eq,":new_village",slot_village_bound_center,0), ##make sure it is not used!
(display_message,"@A village is available!"), ###TEST MESSAGE
(party_relocate_near_party,":new_village","p_main_party"),
(enable_party,":new_village"),
(party_set_slot, ":new_village",slot_village_bound_center, ":good_spot"), ##this is now claimed, bind it to your town
(store_faction_of_party, ":town_faction", ":good_spot"), ##good_spot is your town, get it's faction
(call_script, "script_give_center_to_faction_aux", ":new_village", ":town_faction"), ##Give it's faction to the new village
######(party_set_slot, ":new_village",slot_town_lord, ":party_no"), ##It is now yours - wrong code for this, see above!!!
(display_message, "@You have founded a new village that is tied to {s26}"),
(assign,":founded",1),
(try_end),
(try_end),
(try_end),
(try_begin),
(eq,":founded",0), ##looks like no more free villages
(display_message, "@NO MORE VILLAGES AVAILABLE..."),
(try_end),
(else_try),
(eq, ":good_spot", 0),
(display_message,"@You cannot found a village this far from your towns. Must be within 15 distance"),
(else_try), ##Found a good spot!
(eq, ":good_spot", -1),
(display_message,"@You are too close to another town or village. Must be outside of 5 distance"),
(try_end),
#(str_store_party_name,s26,":good_spot"), ###TEST CODE: FYI causes error message if no town is found
#(assign, reg50,":good_spot"), ###TEST CODE for below message
#(display_message,"@good_spot variable is {reg50}. Close to {s26}",0x00ff00), ###TEST MESSAGE
(else_try),
(display_message, "@You cannot start a village in this terrain",0xff5555),
(end_try),
]
),
Founding Villages Kit v0.76 for M&B v1.xxx:
Features:
Setting to define radius space needed around a new village (set to 5)
Setting to define how close it needs to be to the Town (that must belong to the party past to the script) it will be bound to (set at 15)
Checks for unbound village, moves it to the spot if other checks pass, turns off the disable flag on the village.
- this means that you need to have (most likely disabled) unbound villages. slot_village_bound_center needs to be 0
Binds the village to the same faction as the town.
Test terrain, so that they cannot put villages on improper (but traversable) terrain. Can be easily turned off
Things for the future:
Actually give the town to you. Right now after traveling for a bit the message to grant the fief comes up. Don't want this for villages.
Halo around the villages showing there used area
Test for needed resources to found the village