def do_exchange(self, args): """Exchange an Item with another with an NPC. EXCHANGE goblin potion bandage <means give potion to goblin and take bandage from him>""" args = parse(args) if not self.check_length(args, 3): return item_holder = NPC.get_or_none(NPC.name == args[0]) item_to_give = Item.get_or_none(Item.name == args[1]) item_to_take = Item.get_or_none(Item.name == args[2]) if not self.set_inputs(action=T.exchange, args=[item_holder, item_to_give, item_to_take]): return found = terminals.exchange(item_holder=item_holder, item_to_give=item_to_give, item_to_take=item_to_take) if not found: print("failed!") return print("player's belongings updated.") print_player_belongings() print("NPC belongings updated.") print_npc_belongings(item_holder) self.last_action_doable = True
def capture_1(target: NPC): """ Get something, go someplace and use it to capture somebody :param target: :return: """ item_to_fetch = Item.get_or_none(type=ItemTypes.tool.name, usage=T.capture.value) if not item_to_fetch: # No item usable for capture, left in the world item_to_fetch = Item.create( name='arbitrary_capture_tool_' + str(randint(100, 999)), type=ItemTypes.tool.name, usage=T.capture.value, generic=GenericItem.get_or_create( name=ItemTypes.singleton.name)[0], place=Place.select().order_by(fn.Random()).get(), impact_factor=1.0, worth=0.75) Message.debug( "No item usable for 'capture' left in the world, create a new one '%s'" % item_to_fetch) # steps: # get # goto # T.capture steps = [[item_to_fetch], [target.place, target], [target]] Message.instruction("Get '%s', then goto '%s' and capture '%s'" % (item_to_fetch, target, target)) return steps
def take_loot(item_to_take: Item, loot_npc: NPCDead = None): player = Player.current() # check if the item belongs to dead if not loot_npc: # NPC confirmed dead already, object among dead is given try: holder = item_to_take.belongs_to except: # holder not found = dead Message.debug("Item '%s' holder is dead") holder = None if holder: # holder is alive return take(item_to_take, item_holder=holder) # remove item from holder's belongings and add to player's item_to_take.belongs_to = None item_to_take.belongs_to_player = player item_to_take.save() # FavoursBook.construct(item_holder, -item_to_take.worth_(), player) Message.achievement("Item '%s' has taken by looting" % item_to_take) return True
def use(item_to_use: Item, target: NPC): player = Player.current() # check if player has the item if item_to_use.belongs_to_player != player: Message.error("You don't have the item (%s)" % item_to_use) return False # check if player at target's place_location if target.place != player.place: Message.debug("Player is not at the target '%s's location (%s)" % (target, target.place)) Message.error("You are not at the target '%s's location" % target) return False # check if player know where the receiver is if not PlayerKnowledgeBook.get_or_none( player=player, intel=Intel.construct(npc_place=target)): Message.debug("Player does not know where the NPC (%s) is located" % target) Message.error("Player does not know where the NPC is located") return False item_to_use.use(npc=target) # depending on positive or negative impact_factor of the item usage, target record in player's favour gets updated FavoursBook.construct(target, float(item_to_use.impact_factor or 0.0)) Message.achievement("Item '%s' used on the '%s'" % (item_to_use, target)) return True
def give(item: Item, receiver: NPC): # check if player has the item player = Player.current() if item.belongs_to_player != player: Message.error("You don't have the item (%s) to give" % item) return False # check if player is at receiver's location if player.place != receiver.place: Message.debug("Player is not at the receiver NPC (%s) location (%s)" % (receiver, receiver.place)) Message.error("You are not at the receiver's (%s) location" % receiver) return False # check if player know where the receiver is if not PlayerKnowledgeBook.get_or_none( player=player, intel=Intel.construct(npc_place=receiver)): Message.debug("Player does not know where the NPC (%s) is located" % receiver) Message.error("Player does not know where the NPC is located") return False item.belongs_to_player = None item.belongs_to = receiver item.save() # update favours book FavoursBook.construct(npc=receiver, owe_factor=item.worth_(), player=player) Message.achievement("Item '%s' has been given to the NPC '%s'" % (item, receiver)) return True
def gather(item_to_gather: Item): player = Player.current() # check if player is at item location if item_to_gather.place != player.place: Message.debug( "Player is not at the item '%s's location (%s) to gather it" % (item_to_gather, item_to_gather.place)) Message.error("You are not at the item '%s's location to gather it" % item_to_gather) return False # check if player know where the item is if not PlayerKnowledgeBook.get_or_none( player=player, intel=Intel.construct(item_place=item_to_gather)): Message.debug("Player does not know where the item (%s) is located" % item_to_gather) Message.error("Player does not know where the item is located") return False # update Player's belongings item_to_gather.belongs_to_player = player item_to_gather.save() Message.achievement("Item '%s' gathered" % item_to_gather) return True
def get_1(item_to_fetch: Item): # if not, add it to player's belongings item_to_fetch.belongs_to_player = Player.current() item_to_fetch.save() Message.event("Item '%s' acquired" % item_to_fetch) return []
def goto_1(destination: Place, npc: NPC = None, item: Item = None): """ You are already there. :return: """ player = Player.current() if npc or item: event_dest_str = str(npc if npc else item) + ' (' + str( destination) + ')' else: event_dest_str = str(destination) if npc: # move the npc npc.place = player.place npc.save() intel = Intel.construct(npc_place=npc) PlayerKnowledgeBook.get_or_create(player=player, intel=intel) Message.debug("NPC (%s) moved to the player location (%s)" % (npc, player.place)) Message.event("%s moved to your location" % npc) elif item: # move item or the holder if item.belongs_to: holder = item.belongs_to # type: NPC holder.place = player.place holder.save() intel = Intel.construct(npc_place=holder) PlayerKnowledgeBook.get_or_create(player=player, intel=intel) Message.debug( "Item (%s) holder (%s) moved to the player location (%s)" % (item, item.belongs_to, player.place)) Message.event("Item '%s's holder moved to your location" % item) elif item.belongs_to_player: Message.debug("Player already has the item %s" % item) Message.event("You already have the item '%s'" % item) else: item.place = player.place item.save() intel = Intel.construct(item_place=item) PlayerKnowledgeBook.get_or_create(player=player, intel=intel) Message.debug("Item (%s) moved to the player location (%s)" % (item, player.place)) Message.event("Item '%s' has been moved to your location" % item) else: # update player's location player.place = destination player.save() Message.debug("Player moved to %s location" % event_dest_str) Message.event("Player has been moved to %s location" % destination) return [[]]
def do_take(self, args): """Take something from an NPC. TAKE bandage Goblin""" args = parse(args) if not self.check_length(args, 2): return item = Item.get_or_none(Item.name == args[0]) npc = NPC.get_or_none(NPC.name == args[1]) dead = NPCDead.get_or_none(NPCDead.name == args[1]) if not npc and dead: # npc is dead, loot him if not self.set_inputs(action=T.take, args=[item]): return found = terminals.take_loot(item_to_take=item, loot_npc=dead) else: # npc is alive, or not found among dead, can't loot if not self.set_inputs(action=T.take, args=[item, npc]): return found = terminals.take(item_to_take=item, item_holder=npc) if not found: print("failed!") return print("player's belongings updated.") print_player_belongings() print("NPC belongings updated.") print_npc_belongings(npc) self.last_action_doable = True
def get_2(item_to_fetch: Item): """ Steal it from somebody. :return: """ if not item_to_fetch.belongs_to: holder = NPC.select().get() item_to_fetch.belongs_to = holder item_to_fetch.save() # steps: # steal: steal item[1] from NPC[1] steps = [[item_to_fetch, item_to_fetch.belongs_to]] Message.instruction("Steal '%s' from '%s'" % (item_to_fetch, item_to_fetch.belongs_to)) return steps
def print_player_belongings(player: Player = None): if not player: player = Player.current() results = Item.select().where(Item.belongs_to_player == player) print("items:") print_indented(results)
def exchange(item_holder: NPC, item_to_give: Item, item_to_take: Item): player = Player.current() # check if player has the item_to_give and holder has the item_to_take if item_to_give.belongs_to_player != player: Message.debug("item_to_give: '%s', belongs_to_player: '%s'" % (item_to_give, item_to_give.belongs_to_player)) return False if item_to_take.belongs_to != item_holder: Message.debug( "item_to_take: '%s', belongs_to: '%s', item_holder: '%s'" % (item_to_take, item_to_give.belongs_to, item_holder)) return False # check if player is at item_holder's place_location if item_holder.place != player.place: Message.debug( "Player is not at the item_holder (%s) place_location (%s)" % (item_holder, item_holder.place)) Message.error("You are not at the item holder (%s) location" % item_holder) return False # check if player know where the receiver is if not PlayerKnowledgeBook.get_or_none( player=player, intel=Intel.construct(npc_place=item_holder)): Message.debug("Player does not know where the NPC (%s) is located" % item_holder) Message.error("Player does not know where the NPC is located") return False # update Player's belongings item_to_take.belongs_to = None item_to_take.belongs_to_player = player item_to_take.save() item_to_give.belongs_to_player = None item_to_give.belongs_to = item_holder item_to_give.save() npc_owing = item_to_give.worth_() - item_to_take.worth_() FavoursBook.construct(item_holder, npc_owing, player) Message.achievement("Item '%s' exchanged for '%s', with NPC '%s'" % (item_to_give, item_to_take, item_holder)) return True
def goto_2(destination: Place, npc: NPC = None, item: Item = None): """ Explore destination to find npc or item :return: """ # place_location[1] is destination # area around location[1] is given to player to explore and find location[1] player = Player.current() # ensure player doesn't already know where the NPC or item is if npc: intel = Intel.construct(npc_place=npc) if PlayerKnowledgeBook.get_or_none(player=player, intel=intel): # player knows where the NPC is, move the NPC then npc.place = Place.select().where(Place.id != destination).order_by( fn.Random()).get() npc.save() destination = npc.place Message.event( "You just missed '%s' he has moved to somewhere else" % npc) elif item: intel = Intel.construct(item_place=item) if PlayerKnowledgeBook.get_or_none(player=player, intel=intel): # player knows where the Item is, move the item or item's holder if item.belongs_to: holder = item.belongs_to holder.place = Place.select().where( Place.id != destination).order_by(fn.Random()).get() holder.save() destination = holder.place Message.event( "Item '%s's holder just has been moved to somewhere else" % item) else: item.place = Place.select().where( Place.id != destination).order_by(fn.Random()).get() item.save() destination = item.place Message.event( "Item '%s' just has been moved to somewhere else" % item) # steps: # T.explore steps = [[destination, npc, item]] Message.instruction("Explore around '%s'" % destination) return steps
def comfort_1(motivated: NPC) -> list: """ Obtain luxuries for the motivated NPC :param motivated: :return: """ # get item useful for motivated needed_item = Item.select().join(Need).where(Need.npc == motivated, Item.belongs_to_player.is_null())\ .order_by(fn.Random()).limit(1) if needed_item: # motivated NPC need some items item = needed_item[0] else: # motivated NPC doesn't have any needs items_in_world = Item.select().where( Item.belongs_to_player.is_null()).order_by( Item.worth.desc()).limit(1) if items_in_world: item = items_in_world[0] else: # item not found in the world place = Place.select().order_by(fn.Random()).limit(1) if place: place = place[0] else: Message.debug("No place found in the World!") place = helper.create_place() item = Item.create(type=ItemTypes.unknown.name, generic=GenericItem.get_or_create( name=ItemTypes.singleton.name)[0], name='arbitrary_item_' + str(randint(100, 999)), place=place, worth=1) Message.debug("No Item found in the world, item created: %s" % item) # steps: # get # goto # give steps = [[item], [motivated.place, motivated], [item, motivated]] Message.instruction("%s: Get luxury item '%s' for me" % (motivated, item)) return steps
def conquest_2(motivated: NPC) -> list: """ Steal stuff :param motivated: :return: """ # find something an enemy to motivated has item = None enemy_ids = NPC.select(NPC.id).where(NPC.clan != motivated.clan) if enemy_ids: items = Item.select().where(Item.belongs_to.in_(enemy_ids)).order_by( fn.Random()).limit(1) if items: item = items[0] if not item: place = Place.select().order_by(fn.Random()).limit(1) if place: place = place[0] else: Message.debug("No place found in the World!") place = helper.create_place() enemy_clan = Clan.select().where(Clan.id != motivated.clan).order_by( fn.Random()).limit(1).get() target = NPC.create(clan=enemy_clan, name=NPCName.fetch_new(), place=place) item = Item.create(type=ItemTypes.singleton.name, generic=GenericItem.get_or_create( name=ItemTypes.singleton.name)[0], name='arbitrary_item_' + str(randint(100, 999)), belongs_to=target) # steps # goto # steal # goto # give steps = [[item.place_(), None, item], [item, item.belongs_to], [motivated.place, motivated], [item, motivated]] Message.instruction("%s: Steal item '%s' from '%s' for me" % (motivated, item, item.belongs_to)) return steps
def serenity_5(motivated: NPC) -> list: """ Check on NPC(2) :param motivated: :return: """ # find ally NPC to motivated get an item from he/she allies = NPC.select().where(NPC.clan == motivated.clan, NPC.id != motivated).order_by( fn.Random()).limit(1) if allies: target = allies[0] else: place = Place.select().order_by(fn.Random()).limit(1) if place: place = place[0] else: Message.debug("No place found in the World!") place = helper.create_place() target = NPC.create(clan=motivated.clan, name=NPCName.fetch_new(), place=place) belongings = Item.select().where(Item.belongs_to == target.id).order_by( fn.Random()).limit(1) if belongings: item = belongings[0] else: item = Item.create(type=ItemTypes.singleton.name, generic=GenericItem.get_or_create( name=ItemTypes.singleton.name)[0], name='arbitrary_item_' + str(randint(100, 999)), belongs_to=target) # steps # goto # take # goto # give steps = [[target.place, target], [item, target], [motivated.place, motivated], [item, motivated]] Message.instruction("%s: Get item '%s' from my friend, '%s' for me" % (motivated, item, target)) return steps
def knowledge_1(NPC_target: NPC): """ Deliver item for study :param NPC_target: :return: """ player = Player.current() results = Item.select().where(Item.belongs_to != NPC_target, Item.belongs_to_player != player) if results: locations_scores = [player.distance(res.place_()) for res in results] results = sort_by_list(results, locations_scores) item = results[0] else: results = NPC.select().where(NPC.id != NPC_target) if results: locations_scores = [player.distance(res.place) for res in results] results = sort_by_list(results, locations_scores) new_item_holder = results[0] else: # No NPC left in the world except the target new_item_holder = NPC.create( place=Place.select().order_by(fn.Random()).get(), clan=Clan.select().order_by(fn.Random()).get(), name=NPCName.fetch_new()) item = Item.create(type=ItemTypes.unknown.name, generic=GenericItem.get_or_create( name=ItemTypes.singleton.name)[0], name='arbitrary_item_unknown_' + str(randint(100, 999)), place=None, belongs_to=new_item_holder) # steps: # get item # goto target place # give item to target steps = [[item], [NPC_target.place, NPC_target], [item, NPC_target]] Message.instruction("%s: Get item '%s' for me, I want to study it" % (NPC_target, item)) return steps
def take(item_to_take: Item, item_holder: NPC = None): if item_holder is None: return take_loot(item_to_take) player = Player.current() # check if NPC has the item if item_to_take.belongs_to != item_holder: Message.debug( "NPC '%s' doesn't have the item '%s' to give. It belongs to '%s'" % (item_holder, item_to_take, item_to_take.belongs_to)) Message.error("NPC '%s' doesn't have the item '%s' to give" % (item_holder, item_to_take)) return False # check if player is at item_holder's place_location if item_holder.place != player.place: Message.debug( "Player is not at the item_holder (%s) place_location (%s)" % (item_holder, item_holder.place)) Message.error("You are not at the NPC '%s's location" % item_holder) return False # check if player know where the receiver is if not PlayerKnowledgeBook.get_or_none( player=player, intel=Intel.construct(npc_place=item_holder)): Message.debug("Player does not know where the NPC (%s) is located" % item_holder) Message.error("Player does not know where the NPC is located") return False # remove item from holder's belongings and add to player's item_to_take.belongs_to = None item_to_take.belongs_to_player = player item_to_take.save() FavoursBook.construct(item_holder, -item_to_take.worth_(), player) Message.achievement("Item '%s' taken" % item_to_take) return True
def finish_quest(self): print("Congratulations!! Quest completed!!!!") self.progress.current_node = self.progress.quest self.quest_in_progress = False self.quest_done = True Intel.delete_all_arbitrary() query = Item.delete().where(Item.name.contains('arbitrary')) query.execute() query = Place.delete().where(Place.name.contains('arbitrary')) query.execute() query = NPC.delete().where(NPC.name.contains('arbitrary')) query.execute()
def reputation_1(motivated: NPC) -> list: """ Obtain rare items :param motivated: :return: """ # find high worth item worthy_item = Item.select().where( Item.belongs_to_player.is_null()).order_by(Item.worth.desc()).limit(1) if worthy_item: # item found for NPC item = worthy_item[0] else: # item not found in the world place = Place.select().order_by(fn.Random()).limit(1) if place: place = place[0] else: Message.debug("No place found in the World!") place = helper.create_place() item = Item.create(type=ItemTypes.unknown.name, generic=GenericItem.get_or_create( name=ItemTypes.singleton.name)[0], name='arbitrary_item_' + str(randint(100, 999)), place=place, worth=1) Message.debug("No Item found in the world, item created: %s" % item) # steps: # get # goto # give steps = [[item], [motivated.place, motivated], [item, motivated]] Message.instruction("%s: Get the rare item '%s' for me" % (motivated, item)) return steps
def do_gather(self, args): """Gather an item. GATHER bandage""" args = parse(args) if not self.check_length(args, 1): return item = Item.get_or_none(Item.name == args[0]) if not self.set_inputs(action=T.gather, args=[item]): return found = terminals.gather(item_to_gather=item) if not found: print("failed!") return print("player's belongings updated.") print_player_belongings() self.last_action_doable = True
def do_use(self, args): """Use an item (tool) on an NPC. USE potion Lempeck""" args = parse(args) if not self.check_length(args, 2): return item = Item.get_or_none(Item.name == args[0]) npc = NPC.get_or_none(NPC.name == args[1]) if not self.set_inputs(action=T.use, args=[item, npc]): return found = terminals.use(item_to_use=item, target=npc) if not found: print("failed!") return print("NPC health meter:", npc.health_meter) self.last_action_doable = True
def do_read(self, args): """Read a piece of intel from a book (intel: type intel_value). READ location goblin_place address_book""" args = parse(args) if not self.check_length(args, 3): return intel = Intel.find_by_name(args[0], [args[1]]) readable = Item.get_or_none(Item.name == args[2]) if not self.set_inputs(action=T.read, args=[intel, readable]): return found = terminals.read(intel=intel, readable=readable) if not found: print("failed!") return print("Player intel updated.") print_player_intel() self.last_action_doable = True
def do_give(self, args): """Give an NPC something. GIVE bandage Goblin""" args = parse(args) if not self.check_length(args, 2): return item = Item.get_or_none(Item.name == args[0]) npc = NPC.get_or_none(NPC.name == args[1]) if not self.set_inputs(action=T.give, args=[item, npc]): return found = terminals.give(item=item, receiver=npc) if not found: print("failed!") return print("player's belongings updated.") print_player_belongings() print("NPC belongings updated.") print_npc_belongings(npc) self.last_action_doable = True
def get_3(item_to_fetch: Item): """ Go someplace and pick something up that’s lying around there :param item_to_fetch: :return: """ if item_to_fetch.place: dest = item_to_fetch.place elif item_to_fetch.belongs_to: # someone took it and put it in a specific place, put it where the NPC is Message.event( "Someone has stolen the item '%s' and put it somewhere else" % item_to_fetch) dest = item_to_fetch.belongs_to.place item_to_fetch.belongs_to = None item_to_fetch.place = dest item_to_fetch.save() else: # player already has the item or no one has it, put it in a random place Message.event( "Someone has stolen the item '%s' and has put it in a random place" % item_to_fetch) dest = Place.select().order_by(fn.Random()).get() item_to_fetch.place = dest item_to_fetch.save() player = Player.current() player.next_location = dest player.save() intel = Intel.construct(item_place=item_to_fetch) PlayerKnowledgeBook.get_or_create(player=player, intel=intel) Message.achievement("Intel '%s' learned" % intel.detail()) # steps: # goto # gather steps = [[dest, None, item_to_fetch], [item_to_fetch]] Message.instruction("Gather '%s' from '%s'" % (item_to_fetch, dest)) return steps
def do_explore(self, args): """Explore current location to find an NPC or Item. EXPLORE item potion | EXPLORE npc goblin""" args = parse(args) if not self.check_length(args, 2): return dest = Player.current().place if args[0] == 'item': item = Item.get_or_none(Item.name == args[1]) if not self.set_inputs(action=T.explore, args=[dest, '', item]): return found = terminals.explore(area_location=dest, item=item) else: npc = NPC.get_or_none(NPC.name == args[1]) if not self.set_inputs(action=T.explore, args=[dest, npc, '']): return found = terminals.explore(area_location=dest, npc=npc) if not found: print("failed!") return self.last_action_doable = True
def read(intel: Intel, readable: Item): # check if the readable has the intel if not ReadableKnowledgeBook.get_or_none( ReadableKnowledgeBook.intel == intel, ReadableKnowledgeBook.readable == readable): Message.debug( "Readable '%s' does not contain the intel '%s', ReadableKnowledgeBook not found" % (readable, intel)) Message.error( "Readable '%s' does not contain the intel player looking for" % readable) return False player = Player.current() # check if player owns the readable if readable.belongs_to_player != player: Message.debug("Player doesn't have the readable '%s'" % readable) if readable.place_() == player.place: readable.belongs_to = None readable.belongs_to_player = player readable.save() Message.debug( "Player didn't own the readable (%s) but at its place so he take it" % readable) else: Message.debug( "Player neither own the readable (%s), nor at the item's place_location (%s)" % (readable, readable.place_())) Message.error( "You neither own the readable (%s), nor are at the item's location" % readable) return False # update Player's intel NarrativeHelper.add_intel(intel) Message.achievement("By reading '%s', intel '%s' has been learned" % (readable, intel.detail())) return True
def get_4(item_to_fetch: Item): """ an NPC have the item, but you need to give the NPC something in an exchange :param item_to_fetch: :return: """ player = Player.current() # find an NPC who has the needed item, and has it in exchange list exchanges = Exchange.select().join(Item) if item_to_fetch.is_singleton(): exchanges = exchanges.where(Exchange.item == item_to_fetch) else: exchanges = exchanges.where( Exchange.item.generic == item_to_fetch.generic) if exchanges: locations_scores = [ player.distance(exc.need.npc.place) for exc in exchanges ] exchanges = sort_by_list(exchanges, locations_scores) exchange = exchanges[0] item_to_give = exchange.need.item item_holder = exchange.need.npc else: # no one wants to offer the "item_to_fetch" in exchange for something else # you can even trade with your enemies so alliance doesn't really matters here # first try find someone who needs this item Message.debug( "No one wants to offer the '%s' in exchange for something else" % item_to_fetch) needs = Need.select().where(Need.item == item_to_fetch).group_by( Need.npc) if needs.count(): need = needs.order_by(fn.Random()).get() item_holder = need.npc Message.debug( "NPC '%s' needs something, ideal to create an exchange motive with the item '%s' for him" % (item_holder, item_to_fetch)) else: # no one need anything create a need for coin for an NPC results = NPC.select() locations_scores = [player.distance(npc.place) for npc in results] results = sort_by_list(results, locations_scores) item_holder = results[0] Message.debug( "No one need anything create a need for 'coin' for a close-by NPC '%s'" % item_holder) need = Need.create( npc=item_holder, item=Item.get(generic=GenericItem.get(name='coin'))) Exchange.create(need=need, item=item_to_fetch) if item_to_fetch.belongs_to != item_holder: # ensure NPC has the item item_to_fetch.belongs_to = item_holder item_to_fetch.save() item_to_give = need.item player.next_location = item_holder.place player.save() intel = Intel.construct(item_place=item_to_fetch) PlayerKnowledgeBook.get_or_create(player=player, intel=intel) Message.achievement("Intel '%s' learned" % intel.detail()) # check for player belonging for the exchange item if item_to_give.is_singleton(): player_owns = (item_to_give.belongs_to_player == player) else: player_owns = Item.select().where(Item.generic == item_to_give.generic, Item.belongs_to_player == player) is True if player_owns: # player has the item # goto # exchange steps = [[], [], [], [item_holder.place, item_holder], [item_holder, item_to_give, item_to_fetch]] Message.instruction( "Do a sub-quest, meet '%s' and exchange '%s' with '%s'" % (item_holder, item_to_give, item_to_fetch)) return steps # steps: # goto # get # sub-quest # goto # exchange steps = [[item_to_give.place_(), None, item_to_give], [item_to_give], [], [item_holder.place, item_holder], [item_holder, item_to_give, item_to_fetch]] Message.instruction( "Get '%s', do a sub-quest, meet '%s' and exchange '%s' with '%s'" % (item_to_give, item_holder, item_to_give, item_to_fetch)) return steps
def protection_2(NPC_protection_motivated: NPC) -> list: # find an NPC, who is ally to motivated_NPC and needs something, as well as an NPC who has that thing # and that need is not part of an exchange results = NPC.select(NPC, Need.item_id.alias('needed_item_id'))\ .join(Need) \ .join(Exchange, JOIN.LEFT_OUTER)\ .where( NPC.clan == NPC_protection_motivated.clan, Need.item.is_null(False), )\ .group_by(Need)\ .having(fn.COUNT(Exchange.id) == 0)\ .objects() if not results: # select any kind of need including an exchange need results = NPC.select(NPC, Need.item_id.alias('needed_item_id'))\ .join(Need) \ .where( NPC.clan == NPC_protection_motivated.clan, Need.item.is_null(False), )\ .objects() player = Player.current() if results: # sort by distance locations_scores = [player.distance(res.place) for res in results] results = sort_by_list(results, locations_scores) npc_in_need = results[0] needed_item = Item.get_by_id(npc_in_need.needed_item_id) del npc_in_need.needed_item_id else: # no need found, have to create one # just select an ally, create a need for him results = NPC.select().where(NPC.clan == NPC_protection_motivated.clan) if not results: # just select an NPC, including enemies results = NPC.select() # sort by distance locations_scores = [player.distance(res.place) for res in results] results = sort_by_list(results, locations_scores) npc_in_need = results[0] # select an NPC to hold the item, enemies preferred holders = NPC.select().where(NPC.id != npc_in_need, NPC.clan != player.clan) if not holders: holders = NPC.select().where(NPC.id != npc_in_need) # sort by distance locations_scores = [player.distance(res.place) for res in holders] holders = sort_by_list(holders, locations_scores) holder = holders[0] needed_item = Item.create(type=ItemTypes.tool.name, generic=GenericItem.get_or_create( name=ItemTypes.singleton.name)[0], name='arbitrary_item_potion_' + str(randint(100, 999)), place=None, belongs_to=holder, usage=T.treat.value, impact_factor=0.5) Need.create(npc=npc_in_need, item=needed_item) # get # goto NPC in need # use steps = [[needed_item], [npc_in_need.place, npc_in_need], [needed_item, npc_in_need]] Message.instruction("%s: Treat my friend '%s' using '%s'" % (NPC_protection_motivated, npc_in_need, needed_item)) return steps
def print_npc_belongings(npc: NPC, debug=False): if debug or Params.debug_mode: results = Item.select().where(Item.belongs_to == npc) print("items:") print_indented(results)