WB Coding WRECK full plugin system python code how to integrate?

Currently viewing this thread:

Vetrogor

Sergeant at Arms
I need to integrate python code in the fully integrated WRECK via plugin system. This code creates duplicate noswing weapons for motomotaru formations.
I don't know python well. So how can I do it? This code doesn't work.

Python:
from copy import deepcopy
from compiler import *
register_plugin()

def preprocess_entities(glob):
   # Create noswing weapons
   add_item = deepcopy(glob['items'])
   for i_item in range(1,len(add_item)):
      type = add_item[i_item][3] & 0x000000ff
      # if itp_type_one_handed_wpn <= type <= itp_type_polearm and add_item[i_item-1][3] & itp_next_item_as_melee == 0 and (get_thrust_damage(add_item[i_item][6]) % 256) > 0 and "tutorial" not in add_item[i_item][0] and #"arena" not in add_item[i_item][0] and "practice" not in add_item[i_item][0] and "tpe" not in add_item[i_item][0]:
      if itp_type_one_handed_wpn <= type <= itp_type_polearm and (get_thrust_damage(add_item[i_item][6])&0xff) > 0 and "tutorial" not in add_item[i_item][0] and "arena" not in add_item[i_item][0] and "practice" not in add_item[i_item][0] and "tpe" not in add_item[i_item][0]:
         #Above checks that it is a weapon with thrust damage; also checks that it isn't a tournament-type weapon by checking the item ID (just to prevent not-used items)
         add_item[i_item][0] = 'noswing_'+add_item[i_item][0]                  #add noswing_ to the item's name
         add_item[i_item][6] = add_item[i_item][6] & ~(ibf_damage_mask << iwf_swing_damage_bits) #should set new item's swing damage to 0
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_overswing_polearm  #remove itcf_ capabilties to prevent swinging without damage  
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_slashright_polearm                     
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_slashleft_polearm
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_overswing_onehanded   
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_slashright_onehanded                     
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_slashleft_onehanded
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_overswing_twohanded
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_slashright_twohanded                     
         add_item[i_item][4] = add_item[i_item][4] & ~itcf_slashleft_twohanded
         if type == itp_type_polearm and add_item[i_item][3] & itp_two_handed == 0:
            add_item[i_item][4] = add_item[i_item][4] | itcf_thrust_onehanded  #so that the polearms use 'bent elbow' with shields, but normal without
         add_item[i_item][3] = add_item[i_item][3] & ~itp_merchandise
         #orig_items.append(add_item[i_item])
         glob['items'].append(add_item[i_item])

def make_noswing_weapons(items):
   noswing_weapons = []
   for i_item in xrange(len(items)):
      noswing_name = 'noswing_' + items[i_item][0]
      i_noswing = find_object (items, noswing_name)
      if i_noswing > -1:
         noswing_weapons.append((item_set_slot, i_item, slot_item_alternate, i_noswing))
         noswing_weapons.append((item_set_slot, i_noswing, slot_item_alternate, i_item))
   return noswing_weapons[:]

scripts = [
   ("init_noswing_weapons", make_noswing_weapons(get_globals()['items'])),
]
 

Eärendil Ardamírë

Subforum Moderator
WBWF&SM&B
Perhaps this is useful to you, recalled it when reading your problem but needed a bit time to refind it.
Aside from the OP being ancient, just wanted to clarify...
Are you certain about this? I'm almost 100% positive that same itp_type_*s can be itp_next_item_as_melee'd to one another. I did so in Python test code to lazily create alternate "no swing"/high thrust versions of weapons for use in formations and it worked fine without any crashing.
Code:
        add_item = deepcopy(orig_items)
        for i in reversed(range(1,len(orig_items))):
            itm_flags = add_item[i][3]
            type = itm_flags & 0x000000ff
            dmg = get_thrust_damage(add_item[i][6]) % 256
            dmg_type = get_thrust_damage(add_item[i][6]) / 256
            if type == itp_type_polearm and itm_flags & itp_couchable == 0 and dmg > 2 and dmg_type == pierce and itm_flags & itp_next_item_as_melee == 0 and add_item[i-1][3] & itp_next_item_as_melee == 0 :
                #Above checks that it is a polearm with piercing thrust damage that isn't already involved in a 'X' toggled alt-weapon
                orig_items[i][3] = orig_items[i][3] | itp_next_item_as_melee #flag native item as changeable
                add_item[i][0] = add_item[i][0]+'_alt'                  #add *_alt to the new item's name
                itm_flags = itm_flags & ~itp_merchandise      #disallow from merch list
                itm_flags = itm_flags | itp_crush_through     #add crush-through a la WFaS
                if (get_swing_damage(add_item[i][6]) % 256 > 0):        #if weapon wasn't already thrust-only
                    speed = get_speed_rating(add_item[i][6])
                    add_item[i][6] = add_item[i][6] & ~(ibf_armor_mask << iwf_speed_rating_bits) #clear speed
                    speed +=5                                                                    #boost speed by 5
                    add_item[i][6] = add_item[i][6] | spd_rtng(speed)                            #write increased speed
                if itm_flags & itp_two_handed == 0:
                    add_item[i][4] = add_item[i][4] | itcf_thrust_onehanded  #so that the polearms use 'bent elbow' with shields
                add_item[i][4] = add_item[i][4] | itcf_thrust_twohanded      #overhead thrust
                add_item[i][6] = add_item[i][6] & ~(ibf_damage_mask << iwf_thrust_damage_bits) #erase damage
                dmg -= 2                                                                       #reduce damage by 2
                add_item[i][6] = add_item[i][6] | thrust_damage(dmg, pierce)                   #write reduced damage
                orig_items.insert(i+1, add_item[i])        #add right after the native item, so it can be switched to
Bottom line is that I add itp_next_item_as_melee to the Native item and then add a duplicated item of the same type, but with changed stats, to the items list following the Native item and the toggle between the two worked fine when last tested on 1.143. Haven't tested on 1.153
Thread:
 
Upvote 0

Vetrogor

Sergeant at Arms
So deepcopy function seems don't work. I wonder why?

This is the error screen
M0oKVMq.jpg


This is my test code
Python:
# -*- coding: UTF-8 -*-
from compiler import *
register_plugin()

def preprocess_entities(glob):
  add_items = deepcopy(glob['items'])

This is WRECK code
Python:
	try:
		stage = 0
		# Pre-processing (note that all entity-level injections are already done but script-level injections are not).
		glob = get_globals()
		preprocess_entities_internal(glob)
		stage = 1
		for plugin in WRECK.plugins:
			processor = getattr(glob[plugin], 'preprocess_entities', None)
			if processor:
				try: processor(glob)
				except Exception, e: raise MSException('Error in %r pre-processor script.' % plugin, formatted_exception())
		# Compiling...
		stage = 2
		for entity_name, entity_def in parsers.iteritems():
			stage = 3
			entities = get_globals()[entity_name]
			stage = 4
			for index in xrange(len(entities)):
				entities[index] = entity_def['processor'](entities[index], index)
			stage = 5
			setattr(WRECK, entity_name, entity_def['aggregator'](entities))
		# Post-processing (plugins are NOT allowed to do anything here as we are dealing with already compiled code)
		stage = 6
		postprocess_entities()
	except Exception, e:
		print '{1}FAILED.'.format(*COLORAMA)
		if isinstance(e, MSException):
			if stage == 0:
				print 'COMPILER PREPROCESSOR ERROR:\n{error!s}{0}'.format(*COLORAMA, error = e.formatted())
			if stage == 1:
				print 'PLUGIN {module!s} PREPROCESSOR ERROR:\n{error!s}{0}'.format(*COLORAMA, module = plugin, error = e.formatted())
			elif stage == 3:
				print 'MODULE {module!s} ENTITY #{index} COMPILATION ERROR:\n{error!s}{0}'.format(*COLORAMA, module = entity_name, index = index, error = e.formatted())
			elif stage == 4:
				print 'MODULE {module!s} AGGREGATOR ERROR:\n{error!s}{0}'.format(*COLORAMA, module = entity_name, error = e.formatted())
			elif stage == 5:
				print 'COMPILER POSTPROCESSOR ERROR:\n{error!s}{0}'.format(*COLORAMA, error = e.formatted())
		else:
			print 'COMPILER INTERNAL ERROR:\n{error!s}{0}'.format(*COLORAMA, error = formatted_exception())
		WRECK.time_compile = gettime()
		raise MSException()
	print '{2}DONE.{0}'.format(*COLORAMA)
	WRECK.time_compile = gettime()
 
Upvote 0

Vetrogor

Sergeant at Arms
deepcopy doesn't eat factions of items. How to workaround this? This happens because WRECK fully uses dynamic tags.

I have tried to slice
Python:
add_items = glob['items'][:]
but it gives error that "agregation and int are not compatible" in get_thrust_damage.
 
Last edited:
Upvote 0
Top Bottom