Difference between revisions of "BHAV:Find nearest"
MogHughson (Talk | contribs) |
MogHughson (Talk | contribs) (→The Loop) |
||
Line 23: | Line 23: | ||
Then we will check if it is closer than the closest one we previously found, which for the first desk found by the loop it will be. If it isn't closer then we loop back up to look for the next desk. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again. | Then we will check if it is closer than the closest one we previously found, which for the first desk found by the loop it will be. If it isn't closer then we loop back up to look for the next desk. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again. | ||
0x0006: Expression (Temp 0x0000 < Local 0x0003), True Target: 0x0007, False Target: 0x0003 | 0x0006: Expression (Temp 0x0000 < Local 0x0003), True Target: 0x0007, False Target: 0x0003 | ||
− | If it is closer, we save the new closest distance so far into one [[Sims 2 Local|Local]] variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more desks that might be closer. | + | If it is closer, we save the new closest distance so far into one [[Sims 2 Local|Local]] variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more desks that might be closer. Note that we always loop back to the instruction that sets the value in Temp 0x0000. In many cases it might be unnecessary, since Temp 0x0000 hasn't changed, but if you call out to any function in the loop, that function may set Temp 0x0000 to something else and then your loop will be looking for a different category next time around. Certainly if you use Temp 0x0000 in your loop (as we do this time) it is essential. |
0x0007: Expression (Local 0x0003 := Temp 0x0000), True Target: 0x0008, False Target: Error | 0x0007: Expression (Local 0x0003 := Temp 0x0000), True Target: 0x0008, False Target: Error | ||
0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error | 0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error | ||
+ | |||
===After the loop=== | ===After the loop=== | ||
Once we have exhausted all the desks, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest desk. | Once we have exhausted all the desks, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest desk. | ||
0x0009: ''Your code here'' | 0x0009: ''Your code here'' |
Revision as of 15:45, 25 September 2010
Contents |
Overview
Here we show a SimAntics BHAV code snippet that will find the closest desk. It loops round looking for objects of a certain type and calculating the distance between that object and the current Sim. It tests the distance of each object with the previous lowest distance and if closer saves the new distance and the new object ID. Once all objects of the type have been cycled round, we drop out of the loop and make use of the nearest object that we have found.
Illustrating
This code snippet illustrates several primitives. Use these links to see full details about how to use these primitives in order to recreate the BHAV code below.
The BHAV code
Preparatory steps
Firstly we start by zeroing out the Stack Object so that our loop through all the objects starts from the beginning.
0x0000: Expression (Stack Object ID := Literal 0x0000), True Target: 0x0001, False Target: Error
Then we set up our Local variables to hold the closest distance so far and the object ID of the closest object of the type we are after that we have found. We set the distance to 9999 (0x270F when displayed in hex) so the first object we find is bound to be closer than that.
0x0001: Expression (Local 0x0003 := Literal 0x270F), True Target: 0x0002, False Target: Error 0x0002: Expression (Local 0x0004 := Literal 0x0000), True Target: 0x0003, False Target: Error
The Loop
Now we begin our loop round all the objects of a certain type. We are going to look for desks. We set the value for desk types in Temp variable zero as that is what the next instruction expects. We use the Global constant for this as SimPE helpfully shows us the text assigned to the constant which helps us to know we have found the correct value.
0x0003: Expression (Temp 0x0000 := Const 0x0106:0x33), True Target: 0x0004, False Target: Error 0x0004: Set to Next (Stack Object ID, obj with category == temp 0), True Target: 0x0005, False Target: 0x0009
If we find one, that is our Set to Next instruction returned True, we will calculate the distance between our Sim and this desk.
0x0005: Distance (Temp 0x0000 := My object id .. Stack Object), True Target: 0x0006, False Target: Error
Then we will check if it is closer than the closest one we previously found, which for the first desk found by the loop it will be. If it isn't closer then we loop back up to look for the next desk. We loop back to the instruction that sets the type into Temp 0x0000 since we used Temp 0x0000 for something else since we put that value into it, so we need to set it again.
0x0006: Expression (Temp 0x0000 < Local 0x0003), True Target: 0x0007, False Target: 0x0003
If it is closer, we save the new closest distance so far into one Local variable, and the object ID of the object that is this closest one, into another Local variable. Then we loop back to the run the loop again to see if there are any more desks that might be closer. Note that we always loop back to the instruction that sets the value in Temp 0x0000. In many cases it might be unnecessary, since Temp 0x0000 hasn't changed, but if you call out to any function in the loop, that function may set Temp 0x0000 to something else and then your loop will be looking for a different category next time around. Certainly if you use Temp 0x0000 in your loop (as we do this time) it is essential.
0x0007: Expression (Local 0x0003 := Temp 0x0000), True Target: 0x0008, False Target: Error 0x0008: Expression (Local 0x0004 := Stack Object ID), True Target: 0x0003, False Target: Error
After the loop
Once we have exhausted all the desks, the Set to Next instruction will return false and will jump to the instruction which is going to now use this discovered closest desk.
0x0009: Your code here