def get_best_weapon(life): _weapons = lfe.get_all_inventory_items(life, matches=[{'type': 'gun'}], ignore_actions=True) _best_wep = {'weapon': None, 'feed': None, 'rounds': 0} for _wep in _weapons: _feed_rounds = 0 _free_rounds = 0 _feeds = lfe.get_all_inventory_items(life, matches=[{'type': _wep['feed'],'ammotype': _wep['ammotype']}]) _loaded_feed_uid = weapons.get_feed(_wep) if _loaded_feed_uid: _loaded_feed = items.get_item_from_uid(_loaded_feed_uid) if len(_loaded_feed['rounds'])>_best_wep['rounds']: _best_wep['weapon'] = _wep _best_wep['feed'] = _loaded_feed _best_wep['rounds'] = len(_loaded_feed['rounds']) continue for _feed in _feeds: _feed_rounds = len(_feed['rounds']) _free_rounds = len(lfe.get_all_inventory_items(life, matches=[{'type': 'bullet', 'ammotype': _wep['ammotype']}])) if _feed_rounds+_free_rounds > _best_wep['rounds']: _best_wep['weapon'] = _wep _best_wep['feed'] = _feed _best_wep['rounds'] = _feed_rounds+_free_rounds if not _best_wep['weapon'] or not _best_wep['feed']: return False return _best_wep
def create_gib(life, icon, size, limb, velocity, color=(tcod.white, None)): _gib = {'name': 'gib', 'prefix': 'a', 'type': 'magazine', 'icon': icon, 'flags': ['BLOODY'], 'description': '%s\'s %s.' % (' '.join(life['name']), limb), 'size': '%sx1' % size, 'material': 'flesh', 'thickness': size, 'color': color} _i = items.get_item_from_uid(items.create_item('gib', position=life['pos'][:], item=_gib)) _i['velocity'] = [numbers.clip(velocity[0], -3, 3), numbers.clip(velocity[1], -3, 3), velocity[2]] logging.debug('Created gib.')
def create_gib(life, icon, size, limb, velocity, color=(tcod.white, None)): _gib = { 'name': 'gib', 'prefix': 'a', 'type': 'magazine', 'icon': icon, 'flags': ['BLOODY'], 'description': '%s\'s %s.' % (' '.join(life['name']), limb), 'size': '%sx1' % size, 'material': 'flesh', 'thickness': size, 'color': color } _i = items.get_item_from_uid( items.create_item('gib', position=life['pos'][:], item=_gib)) _i['velocity'] = [ bad_numbers.clip(velocity[0], -3, 3), bad_numbers.clip(velocity[1], -3, 3), velocity[2] ] logging.debug('Created gib.')
def get_items_for_crafting(life): return [i for i in life['inventory'] if 'craft' in items.get_item_from_uid(i)]
def bullet_hit(life, bullet, limb): _owner = LIFE[bullet["shot_by"]] _actual_limb = lfe.get_limb(life, limb) _items_to_check = [] if "player" in _owner: if bullet["aim_at_limb"] == limb: _msg = ["The round hits"] elif not limb in life["body"]: return "The round misses entirely!" else: _msg = ["The round misses slightly"] _detailed = True elif "player" in life: _msg = ["The round hits"] else: _msg = ["%s hits %s's %s" % (items.get_name(bullet), life["name"][0], limb)] for item_uid in lfe.get_items_attached_to_limb(life, limb): _items_to_check.append({"item": item_uid, "visible": True}) _item = items.get_item_from_uid(item_uid) if "storing" in _item: for item_in_container_uid in _item["storing"]: _chance_of_hitting_item = _item["capacity"] / float(_item["max_capacity"]) if random.uniform(0, 1) < _chance_of_hitting_item: break _items_to_check.append({"item": item_in_container_uid, "visible": False}) for entry in _items_to_check: _item = items.get_item_from_uid(entry["item"]) _item_damage = get_puncture_value(bullet, _item, target_structure_name=_item["name"]) _item["thickness"] = numbers.clip(_item["thickness"] - _item_damage, 0, _item["max_thickness"]) if "material" in _item and not _item["material"] == "cloth": _speed_mod = _item_damage _can_stop = True bullet["speed"] *= _speed_mod bullet["velocity"][0] *= _speed_mod bullet["velocity"][1] *= _speed_mod else: _can_stop = False if not _item["thickness"]: _msg.append(", destroying the %s" % _item["name"]) if _item["type"] == "explosive": items.explode(_item) else: items.delete_item(_item) else: if bullet["speed"] <= 1 and _can_stop: _msg.append(", lodging itself in %s" % items.get_name(_item)) _ret_string = own_language(life, _msg) if _ret_string.endswith("!"): return _ret_string else: return _ret_string + "." else: if "material" in _item: if _item["material"] == "metal": _msg.append(", puncturing the %s" % _item["name"]) else: _msg.append(", ripping through the %s" % _item["name"]) _damage = get_puncture_value(bullet, _actual_limb, target_structure_name=limb) _actual_limb["thickness"] = numbers.clip(_actual_limb["thickness"] - _damage, 0, _actual_limb["max_thickness"]) _damage_mod = 1 - (_actual_limb["thickness"] / float(_actual_limb["max_thickness"])) if limb in life["body"]: _msg.append(", " + lfe.add_wound(life, limb, cut=_damage * _damage_mod, impact_velocity=bullet["velocity"])) _ret_string = own_language(life, _msg) if _ret_string.endswith("!"): return _ret_string else: return _ret_string + "."
def bite(life, target_id, limb): logging.debug("%s bit %s in the %s." % (" ".join(life["name"]), " ".join(LIFE[target_id]["name"]), limb)) target = LIFE[target_id] _msg = ["%s" % language.get_introduction(life)] _bite_strength = random.randint(1, 3) if numbers.distance(life["pos"], target["pos"]) > 1: _msg.append("bites the air") return " ".join(_msg) + "." _items_to_check = [] for _item in lfe.get_items_attached_to_limb(target, limb): _items_to_check.append({"item": _item, "visible": True}) _actual_item = items.get_item_from_uid(_item) if "storing" in _actual_item: for _item_in_container in _actual_item["storing"]: _items_to_check.append({"item": _item_in_container, "visible": False}) for entry in _items_to_check: _item = items.get_item_from_uid(entry["item"]) if not "thickness" in _item: logging.warning("Item '%s' has no set thickness. Guessing..." % _item["name"]) _item["thickness"] = _item["size"] / 2 _thickness = _item["thickness"] _item["thickness"] = numbers.clip(_item["thickness"] - _bite_strength, 0, 100) _bite_strength -= _thickness _tear = _item["thickness"] - _thickness _limb_in_context = False if _item["material"] == "cloth": if _thickness and not _item["thickness"]: _msg.append("rips through <own> %s" % _item["name"]) elif _tear <= -3: _msg.append("rips <own> %s" % _item["name"]) elif _tear <= -2: _msg.append("tears <own> %s" % _item["name"]) elif _tear <= -1: _msg.append("slightly tears <own> %s" % _item["name"]) if _bite_strength <= 0 and _item["thickness"]: _msg.append("is stopped by <own> %s" % _item["name"]) return " ".join(_msg) # if not lfe.limb_is_cut(target, limb): if _bite_strength == 1: _msg.append(", cutting <own> %s" % limb) elif _bite_strength == 2: _msg.append(", tearing <own> %s" % limb) elif _bite_strength == 3: _msg.append(", ripping open <own> %s" % limb) if _bite_strength: lfe.add_wound(target, limb, cut=_bite_strength) # TODO: How thick is skin? _bite_strength -= 1 # if not _bite_strength: # return ' '.join(_msg) _ret_string = own_language(target, _msg) return _ret_string + "."
def get_items_for_dismantle(life): return [i for i in life['inventory'] if 'CANDISMANTLE' in items.get_item_from_uid(i)['flags']]
def fire(life, target, limb=None): #TODO: Don't breathe this! weapon = get_weapon_to_fire(life) if not weapon: return False _aim_with_limb = None for hand in life['hands']: if weapon['uid'] in lfe.get_limb(life, hand)['holding']: _aim_with_limb = hand _ooa = False _feed_uid = get_feed(weapon) if not _feed_uid: if 'player' in life: gfx.message('The weapon is unloaded.') _ooa = True return False _feed = items.get_item_from_uid(_feed_uid) if not _feed or (_feed and not _feed['rounds']): if 'player' in life: gfx.message('*Click* (You are out of ammo.)') _ooa = True return False _bullet_deviation = (1 - weapon['accuracy']) + life['recoil'] _deviation_mod = SETTINGS['aim_difficulty'] * (1 - ( (life['stats']['firearms'] / 10.0) * SETTINGS['firearms_skill_mod'])) _direction_deviation = (_bullet_deviation * SETTINGS['aim_difficulty']) * _deviation_mod life['recoil'] = bad_numbers.clip( life['recoil'] + (weapon['recoil'] * get_stance_recoil_mod(life)), 0.0, 1.0) _bullet_direction = bad_numbers.direction_to(life['pos'], target) + ( random.uniform(-_direction_deviation, _direction_deviation)) alife.noise.create(life['pos'], 120, '%s fire' % weapon['name'], 'something discharge', target=life['id']) #TODO: Clean this up... _bullet = items.get_item_from_uid(_feed['rounds'].pop()) _bullet['pos'] = life['pos'][:] _bullet['start_pos'] = life['pos'][:] _bullet['owner'] = None _bullet['shot_by'] = life['id'] _bullet['aim_at_limb'] = limb items.add_to_chunk(_bullet) if gfx.position_is_in_frame(life['pos']) or 'player' in life: effects.create_light(life['pos'], tcod.yellow, 7, 1, fade=3.2) effects.create_light(_bullet['pos'], tcod.yellow, 7, .9, fade=.65, follow_item=_bullet['uid']) effects.create_smoke_cloud(life['pos'], 3, color=tcod.light_gray) effects.create_smoke(life['pos'], color=tcod.yellow) _bullet['accuracy'] = int( round(get_accuracy(life, weapon['uid'], limb=_aim_with_limb))) print 'ACCURACY', _bullet['accuracy'] del _bullet['parent'] items.move(_bullet, _bullet_direction, _bullet['max_speed']) _bullet['start_velocity'] = _bullet['velocity'][:] items.tick_item(_bullet) for _life in [LIFE[i] for i in LIFE]: if _life['pos'][0] == target[0] and _life['pos'][1] == target[1]: life['aim_at'] = _life['id'] break if len(lfe.find_action(life, matches=[{'action': 'shoot'}])) == 1: life['firing'] = None
def fire(life, target, limb=None): #TODO: Don't breathe this! weapon = get_weapon_to_fire(life) if not weapon: return False _aim_with_limb = None for hand in life['hands']: if weapon['uid'] in lfe.get_limb(life, hand)['holding']: _aim_with_limb = hand _ooa = False _feed_uid = get_feed(weapon) if not _feed_uid: if 'player' in life: gfx.message('The weapon is unloaded.') _ooa = True return False _feed = items.get_item_from_uid(_feed_uid) if not _feed or (_feed and not _feed['rounds']): if 'player' in life: gfx.message('*Click* (You are out of ammo.)') _ooa = True return False direction = numbers.direction_to(life['pos'],target)+(random.uniform(-life['recoil'], life['recoil'])) alife.noise.create(life['pos'], 120, '%s fire' % weapon['name'], 'something discharge') #TODO: Clean this up... _bullet = items.get_item_from_uid(_feed['rounds'].pop()) _bullet['pos'] = life['pos'][:] _bullet['start_pos'] = life['pos'][:] _bullet['owner'] = None _bullet['shot_by'] = life['id'] _bullet['aim_at_limb'] = limb life['recoil'] += _bullet['recoil']*(weapon['recoil']*get_stance_recoil_mod(life)) items.add_to_chunk(_bullet) if gfx.position_is_in_frame(life['pos']): effects.create_light(life['pos'], tcod.yellow, 7, 1, fade=3.5) effects.create_light(_bullet['pos'], tcod.yellow, 7, 1, fade=0.65, follow_item=_bullet['uid']) effects.create_smoke_cloud(life['pos'], 3, color=tcod.light_gray) effects.create_smoke(life['pos'], color=tcod.yellow) _bullet['accuracy'] = int(round(get_accuracy(life, weapon['uid'], limb=_aim_with_limb))) del _bullet['parent'] items.move(_bullet, direction, _bullet['max_speed']) _bullet['start_velocity'] = _bullet['velocity'][:] items.tick_item(_bullet) for _life in [LIFE[i] for i in LIFE]: if _life['pos'][0] == target[0] and _life['pos'][1] == target[1]: life['aim_at'] = _life['id'] break if len(lfe.find_action(life, matches=[{'action': 'shoot'}])) == 1: life['firing'] = None
def bullet_hit(life, bullet, limb): _owner = LIFE[bullet['shot_by']] _actual_limb = lfe.get_limb(life, limb) if 'player' in _owner: if bullet['aim_at_limb'] == limb: _msg = ['The round hits'] elif not limb in life['body']: return 'The round misses entirely!' else: _msg = ['The round misses slightly'] _detailed = True else: _msg = ['%s shoots' % language.get_name(_owner)] _items_to_check = [] for item_uid in lfe.get_items_attached_to_limb(life, limb): _items_to_check.append({'item': item_uid, 'visible': True}) _item = items.get_item_from_uid(item_uid) if 'storing' in _item: for item_in_container_uid in _item['storing']: print '*' * 100 _chance_of_hitting_item = bullet['size']*(_item['capacity']/float(_item['max_capacity'])) print 'percent chance of hitting item:', 1-_chance_of_hitting_item if random.uniform(0, 1)<_chance_of_hitting_item: continue _items_to_check.append({'item': item_in_container_uid, 'visible': False}) for entry in _items_to_check: _item = items.get_item_from_uid(entry['item']) _item_damage = get_puncture_value(bullet, _item, target_structure_name=_item['name']) _item['thickness'] = numbers.clip(_item['thickness']-_item_damage, 0, _item['max_thickness']) _speed_mod = _item_damage bullet['speed'] *= _speed_mod bullet['velocity'][0] *= _speed_mod bullet['velocity'][1] *= _speed_mod if not _item['thickness']: _msg.append(', destroying the %s' % _item['name']) if _item['type'] == 'explosive': items.explode(_item) else: items.delete_item(_item) else: if bullet['speed']<=1: _msg.append(', lodging itself in %s' % items.get_name(_item)) _ret_string = own_language(life, _msg) if _ret_string.endswith('!'): return _ret_string else: return _ret_string+'.' else: if 'material' in _item: if _item['material'] == 'metal': _msg.append(', puncturing the %s' % _item['name']) else: _msg.append(', ripping through the %s' % _item['name']) _damage = get_puncture_value(bullet, _actual_limb, target_structure_name=limb) _actual_limb['thickness'] = numbers.clip(_actual_limb['thickness']-_damage, 0, _actual_limb['max_thickness']) _damage_mod = 1-(_actual_limb['thickness']/float(_actual_limb['max_thickness'])) if limb in life['body']: _msg.append(', '+lfe.add_wound(life, limb, cut=_damage*_damage_mod, impact_velocity=bullet['velocity'])) #return '%s punctures %s (%s)' % (bullet['name'], limb, get_puncture_value(bullet, _actual_limb, target_structure_name=limb)) _ret_string = own_language(life, _msg) if _ret_string.endswith('!'): return _ret_string else: return _ret_string+'.'
def bite(life, target_id, limb): logging.debug('%s bit %s in the %s.' % (' '.join(life['name']), ' '.join(LIFE[target_id]['name']), limb)) target = LIFE[target_id] _msg = ['%s' % language.get_introduction(life)] _bite_strength = random.randint(1, 3) if numbers.distance(life['pos'], target['pos'])>1: _msg.append('bites the air') return ' '.join(_msg)+'.' _items_to_check = [] for _item in lfe.get_items_attached_to_limb(target, limb): _items_to_check.append({'item': _item, 'visible': True}) _actual_item = items.get_item_from_uid(_item) if 'storing' in _actual_item: for _item_in_container in _actual_item['storing']: _items_to_check.append({'item': _item_in_container, 'visible': False}) for entry in _items_to_check: _item = items.get_item_from_uid(entry['item']) if not 'thickness' in _item: logging.warning('Item \'%s\' has no set thickness. Guessing...' % _item['name']) _item['thickness'] = _item['size']/2 _thickness = _item['thickness'] _item['thickness'] = numbers.clip(_item['thickness']-_bite_strength, 0, 100) _bite_strength -= _thickness _tear = _item['thickness']-_thickness _limb_in_context = False if _item['material'] == 'cloth': if _thickness and not _item['thickness']: _msg.append('rips through <own> %s' % _item['name']) elif _tear<=-3: _msg.append('rips <own> %s' % _item['name']) elif _tear<=-2: _msg.append('tears <own> %s' % _item['name']) elif _tear<=-1: _msg.append('slightly tears <own> %s' % _item['name']) if _bite_strength <= 0 and _item['thickness']: _msg.append('is stopped by <own> %s' % _item['name']) return ' '.join(_msg) #if not lfe.limb_is_cut(target, limb): if _bite_strength==1: _msg.append(', cutting <own> %s' % limb) elif _bite_strength==2: _msg.append(', tearing <own> %s' % limb) elif _bite_strength==3: _msg.append(', ripping open <own> %s' % limb) if _bite_strength: lfe.add_wound(target, limb, cut=_bite_strength) #TODO: How thick is skin? _bite_strength -= 1 #if not _bite_strength: # return ' '.join(_msg) _ret_string = own_language(target, _msg) return _ret_string+'.'
def bullet_hit(life, bullet, limb): _owner = LIFE[bullet['shot_by']] _actual_limb = lfe.get_limb(life, limb) _items_to_check = [] _msg = [] #if 'player' in _owner: # if bullet['aim_at_limb'] == limb: # _hit = True # _msg = ['The round hits'] # elif not limb in life['body']: # return 'The round misses entirely!' # else: # _msg = ['The round misses slightly'] # _detailed = True # #elif 'player' in life: # _msg = ['The round hits'] #else: # _msg = ['%s hits %s\'s %s' % (items.get_name(bullet), life['name'][0], limb)] for item_uid in lfe.get_items_attached_to_limb(life, limb): _items_to_check.append({'item': item_uid, 'visible': True}) _item = items.get_item_from_uid(item_uid) if 'storing' in _item: for item_in_container_uid in _item['storing']: _chance_of_hitting_item = _item['capacity']/float(_item['max_capacity']) if random.uniform(0, 1)<_chance_of_hitting_item: break _items_to_check.append({'item': item_in_container_uid, 'visible': False}) for entry in _items_to_check: _item = items.get_item_from_uid(entry['item']) _item_damage = get_puncture_value(bullet, _item, target_structure_name=_item['name']) _item['thickness'] = bad_numbers.clip(_item['thickness']-_item_damage, 0, _item['max_thickness']) if 'material' in _item and not _item['material'] == 'cloth': _speed_mod = _item_damage _can_stop = True bullet['speed'] *= _speed_mod bullet['velocity'][0] *= _speed_mod bullet['velocity'][1] *= _speed_mod else: _can_stop = False if not _item['thickness']: if _item['uid'] in lfe.get_all_visible_items(life): if 'player' in _owner: _msg.append('%s\'s %s is destroyed!' % (' '.join(life['name']), _item['name'])) if _item['type'] == 'explosive': items.explode(_item) else: items.delete_item(_item) #else: # if bullet['speed']<=1 and _can_stop: # #if 'player' in _owner: # # _msg.append(', lodging itself in %s' % items.get_name(_item)) # #_ret_string = own_language(life, _msg) # # if _ret_string.endswith('!'): # return _ret_string # else: # return _ret_string+'.' # #else: # # if 'material' in _item: # # if _item['material'] == 'metal': # # _msg.append(', puncturing the %s' % _item['name']) # # else: # # _msg.append(', ripping through the %s' % _item['name']) _damage = get_puncture_value(bullet, _actual_limb, target_structure_name=limb) _actual_limb['thickness'] = bad_numbers.clip(_actual_limb['thickness']-_damage, 0, _actual_limb['max_thickness']) if not _actual_limb['thickness']: lfe.sever_limb(life, limb, (0, 0, 0)) _damage_mod = 1-(_actual_limb['thickness']/float(_actual_limb['max_thickness'])) if limb in life['body']: _msg.append(lfe.add_wound(life, limb, cut=_damage*_damage_mod, impact_velocity=bullet['velocity'])) #_ret_string = own_language(life, _msg) return ' '.join(_msg)
def get_items_for_crafting(life): return [ i for i in life['inventory'] if 'craft' in items.get_item_from_uid(i) ]