okies, My new big codeing plan needs some code I can't get hold of myself
1) a code to make an AI chack its hold for items that have a higher level than their currently installed gear, then install it if it is better. ( think this can be broken down into stages- Add gear to hold (via looting or being sold gear) > Check hold for gear of level <currently installed +1> > if level is higher, uninstall old, install new > if level is equal or lower, ignore.) something like that. can anyone help?
code request.
- goat not sheep
- Militia Captain
- Posts: 669
- Joined: Fri May 19, 2006 8:36 pm
- Location: ...
- Contact:
Try the normad Salvager AI. It installs looted weapons...
>.<
Try this:
That code will "optimize" the ship's weapons whenever it runs. You could put it in a reoccuring event, or dockscreen depending on how you want it. This code will check each weapon that is installed on the ship for better weapons in the hold, and will install the best.
[EDIT] Revised code to prevent problems with the outer objEnum.
[EDIT2] Revised code again to remove more glaring errors. This should work better, and it should replace each weapon only once.
Code: Select all
(block (curWeapon)
; Enumerate all weapons
(objEnumItems gSource "w" itemCursor
(block Nil
; If the current weapon is installed we will check if a better one is available
(setq curWeapon (itmAtCursor itemCursor))
(if (itmIsInstalled curWeapon)
(block (tempCursor weaponLevel)
; Save the weapon's level and clear the tempCursor
(setq weaponLevel (itmGetLevel curWeapon))
(setq tempCursor Nil)
; Enumerate all weapons (again)
(objEnumItems gSource "w" itemCursor2
(block (newWeapon)
; If the weapon is a higher level AND uninstalled select it
(setq newWeapon (itmAtCursor itemCursor2))
(if (and (gr (itmGetLevel newWeapon) weaponLevel) (not (itmIsInstalled newWeapon)))
(block Nil
; Set the tempCursor to point at the new weapon and set the threshold to the new weapon's level
(setq tempCursor itemCursor2)
(setq weaponLevel (itmGetLevel newWeapon))
)
)
)
)
; If a better weapon is available, remove the current weapon and install it
(if tempCursor
(block Nil
(shpRemoveDevice gSource itemCursor)
(shpInstallDevice gSource tempCursor)
)
)
)
)
)
)
)
[EDIT] Revised code to prevent problems with the outer objEnum.
[EDIT2] Revised code again to remove more glaring errors. This should work better, and it should replace each weapon only once.
splendid! thanks muchly! I assume I can make this into a ship order. I'll find out soon enough...
EDIT: will it work with shields if I replace "w" with the shield tag, and replace "weapon" with "Shield"?
EDIT: yeah, I think I can do the shield version myself, but what about armour? I need to know how to get it to install in a certain armour slot.
ANOTHER EDIT: I got a game crash when I used the order to try yo make it install a dwarg xiphon cannon.
EDIT: will it work with shields if I replace "w" with the shield tag, and replace "weapon" with "Shield"?
EDIT: yeah, I think I can do the shield version myself, but what about armour? I need to know how to get it to install in a certain armour slot.
ANOTHER EDIT: I got a game crash when I used the order to try yo make it install a dwarg xiphon cannon.
-
- Developer
- Posts: 2998
- Joined: Thu Jul 24, 2003 9:53 pm
- Contact:
I think you're right. You can't use the itemCursor outside of (objEnumItems)Burzmali wrote:I am guessing it has something to do with trying to hold an itemCursor (tempCursor) outside the (objEnumItems). George would probably know better though. Give me a day to think on it.
But I believe (shpInstallDevice) does take an item structure; thus you could try this:
Code: Select all
In the inner loop, instead of:
(setq tempCursor itemCursor2)
try:
(setq tempCursor (itmAtCursor itemCursor2))
-
- Developer
- Posts: 2998
- Joined: Thu Jul 24, 2003 9:53 pm
- Contact:
Seems to work fine for me.
The code might fail if the weapon you are trying to install requires more power than the reactor can output.
Try it for a simple case first (one weapon) before trying a more complicated case. That might help to debug the problem.
This is the code that I tried:
It is just like Burzmali's original code (which was sound) except for two things:
1. Instead of checking for (itmIsInstalled) I add that in the criteria for (objEnumItems)
2. I don't install more than one weapon (I use the installDone variable to stop after one installation)
Neither of the above are fundamental changes--it should work even without it.
The code might fail if the weapon you are trying to install requires more power than the reactor can output.
Try it for a simple case first (one weapon) before trying a more complicated case. That might help to debug the problem.
This is the code that I tried:
Code: Select all
<?xml version="1.0" ?>
<TranscendenceExtension UNID="0xA1060000" version="0.97a">
<Globals>
(setq dbgUpgradeWeapons (lambda (shipToUpgrade)
(block (curWeapon installDone)
; Enumerate all weapons
(objEnumItems shipToUpgrade "wI" itemCursor
(block (tempCursor weaponLevel)
(setq curWeapon (itmAtCursor itemCursor))
; Save the weapon's level and clear the tempCursor
(setq weaponLevel (itmGetLevel curWeapon))
(setq tempCursor Nil)
; Enumerate all weapons (again)
(objEnumItems shipToUpgrade "wU" itemCursor2
(block (newWeapon)
; If the weapon is a higher level AND uninstalled select it
(setq newWeapon (itmAtCursor itemCursor2))
(if (gr (itmGetLevel newWeapon) weaponLevel)
(block Nil
; Set the tempCursor to point at the new weapon and set the threshold to the new weapon's level
(setq tempCursor newWeapon)
(setq weaponLevel (itmGetLevel newWeapon))
;(dbgOutput "Found better weapon: " (itmGetName tempCursor 0))
)
)
)
)
; If a better weapon is available, remove the current weapon and install it
(if (and (not installDone) tempCursor)
(block Nil
(shpRemoveDevice shipToUpgrade itemCursor)
;(dbgOutput "Removed weapon: " (itmGetName (itmAtCursor itemCursor) 0))
(shpInstallDevice shipToUpgrade tempCursor)
;(dbgOutput "Installed new weapon: " (itmGetName tempCursor 0))
(setq installDone True)
)
)
)
)
)
))
</Globals>
</TranscendenceExtension>
1. Instead of checking for (itmIsInstalled) I add that in the criteria for (objEnumItems)
2. I don't install more than one weapon (I use the installDone variable to stop after one installation)
Neither of the above are fundamental changes--it should work even without it.
I specifically removed that because I was worried that if the list of installed weapons changed during the enumeration, bad things would happen.george moromisato wrote:It is just like Burzmali's original code (which was sound) except for two things:
1. Instead of checking for (itmIsInstalled) I add that in the criteria for (objEnumItems)
That's half the fungeorge moromisato wrote: 2. I don't install more than one weapon (I use the installDone variable to stop after one installation)
Neither of the above are fundamental changes--it should work even without it.

hrmm, no luck this time.
if I set that as an order, It has no effect, and If I set it as a global
using (dbgUpgradeWeapons) on invoke, (I assume that's correct, since the same system was used on the Corporate cruiser), I get "unknown function dbgUpgradeWeapons"
if I set that as an order, It has no effect, and If I set it as a global
using (dbgUpgradeWeapons) on invoke, (I assume that's correct, since the same system was used on the Corporate cruiser), I get "unknown function dbgUpgradeWeapons"