code request.

Freeform discussion about anything related to modding Transcendence.
Post Reply
User avatar
evilbob
Militia Captain
Militia Captain
Posts: 555
Joined: Sun Mar 05, 2006 1:23 pm

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?
User avatar
goat not sheep
Militia Captain
Militia Captain
Posts: 669
Joined: Fri May 19, 2006 8:36 pm
Location: ...
Contact:

Try the normad Salvager AI. It installs looted weapons...
>.<
Burzmali
Militia Commander
Militia Commander
Posts: 395
Joined: Tue Aug 15, 2006 12:14 am

Do you actually want the AI to dock, or can it just install the equipment?
User avatar
evilbob
Militia Captain
Militia Captain
Posts: 555
Joined: Sun Mar 05, 2006 1:23 pm

it will be dockable, so the player can give it items, which it will then install.
Burzmali
Militia Commander
Militia Commander
Posts: 395
Joined: Tue Aug 15, 2006 12:14 am

Try this:

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)
              )
            )
          )
        )
      )
    )
  ) 
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.
User avatar
evilbob
Militia Captain
Militia Captain
Posts: 555
Joined: Sun Mar 05, 2006 1:23 pm

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.
Burzmali
Militia Commander
Militia Commander
Posts: 395
Joined: Tue Aug 15, 2006 12:14 am

It should work fine with shields too, with the changes you have mentioned. Did the error log have any particular reason for the crash? The code isn't likely to be perfect, but the concept should be sound.
User avatar
evilbob
Militia Captain
Militia Captain
Posts: 555
Joined: Sun Mar 05, 2006 1:23 pm

nothing in the log. The error I get is a "Transcendence has encountered a problem and needs to close..."
Burzmali
Militia Commander
Militia Commander
Posts: 395
Joined: Tue Aug 15, 2006 12:14 am

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.
george moromisato
Developer
Developer
Posts: 2998
Joined: Thu Jul 24, 2003 9:53 pm
Contact:

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.
I think you're right. You can't use the itemCursor outside of (objEnumItems)

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))

Good luck!
User avatar
evilbob
Militia Captain
Militia Captain
Posts: 555
Joined: Sun Mar 05, 2006 1:23 pm

hmm, it dosen't crash, but it now has no effect.
george moromisato
Developer
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:

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>
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.
User avatar
evilbob
Militia Captain
Militia Captain
Posts: 555
Joined: Sun Mar 05, 2006 1:23 pm

ok, I'll try that. Thanks for your help, George!
Burzmali
Militia Commander
Militia Commander
Posts: 395
Joined: Tue Aug 15, 2006 12:14 am

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)
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: 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.
That's half the fun ;)
User avatar
evilbob
Militia Captain
Militia Captain
Posts: 555
Joined: Sun Mar 05, 2006 1:23 pm

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"
Post Reply