git.fiddlerwoaroof.com
edwlan authored on 29/07/2013 04:55:36
Showing 6 changed files
... ...
@@ -6,14 +6,34 @@ from .combat_parts import skills
6 6
 from .combat_parts import constants as const
7 7
 
8 8
 import collections
9
+import random
10
+
11
+def get_stats(sum_):
12
+	stats = [sum_/4]*4
13
+	if sum(stats) != sum_:
14
+		stats[0] += sum_ - sum(stats)
15
+
16
+	mod = +1
17
+	for x in range(100):
18
+		idx = random.choice([x for x in range(len(stats)) if stats[x] > 3])
19
+		stats[idx] += mod
20
+		mod *= -1
21
+	return stats
9 22
 
10 23
 class Attributes(object):
24
+	@classmethod
25
+	def randomize(cls, sum_):
26
+		chosen_stats = get_stats(sum_)
27
+		return cls(*chosen_stats)
28
+
11 29
 	def __init__(self, strength, intellect, dexterity, spirit):
12 30
 		self.str = strength
13 31
 		self.int = intellect
14 32
 		self.dex = dexterity
15 33
 		self.spt = spirit
16 34
 		self.state = const.HEALTHY
35
+	def __str__(self):
36
+		return '%s %s %s %s %s' % (self.str, self.int, self.dex, self.spt, self.state)
17 37
 
18 38
 
19 39
 @equipment.Slot.add_slot('weapon', equipment.Weapon)
... ...
@@ -28,9 +48,26 @@ class Adventurer(object):
28 48
 			raise ValueError('Value for state invalid')
29 49
 		self.attributes.state = val
30 50
 
31
-	def __init__(self, name, race, str, int, dex, spt):
51
+	@classmethod
52
+	def randomize(cls, stat_sum=28):
53
+		name = '%s %s%s' % (
54
+			random.choice(['Bill', 'Bob', 'Jerry']),
55
+			random.choice(['Longnose', 'Short-Tail', 'Squint']),
56
+			random.choice(['', ' the Great', ' The Cross-eyed', ' The Magnificent'])
57
+		)
58
+		race = races.Race.random_race()
59
+		attr = Attributes.randomize(stat_sum)
60
+		print attr
61
+		return cls(name, race, attr)
62
+
63
+	@classmethod
64
+	def with_stats(cls, name, race, str, int, dex, spt):
65
+		attr = Attributes(str, int, dex, spt)
66
+		return cls(name, race, attr)
67
+
68
+	def __init__(self, name, race, attr):
32 69
 		self.name = name
33
-		self.attributes = Attributes(str, int, dex, spt)
70
+		self.attributes = attr
34 71
 		self.skills = skills.Skills(self.attributes)
35 72
 		self.race = races.Race.registry[race](self.attributes)
36 73
 
... ...
@@ -73,20 +110,9 @@ class Adventurer(object):
73 110
 
74 111
 	def die(self): pass
75 112
 
76
-def get_stats():
77
-	stats = [7,7,7,7]
78
-	import random
79
-	mod = +1
80
-	for x in range(100):
81
-		idx = random.choice([x for x in range(len(stats)) if stats[x] > 5])
82
-		stats[idx] += mod
83
-		mod *= -1
84
-	print sum(stats), stats
85
-	return stats
86
-
87 113
 if __name__ == '__main__':
88
-	a = Adventurer('bob', 'elf', *get_stats())
89
-	b = Adventurer('bill', 'elf', *get_stats())
114
+	a = Adventurer.randomize(28)
115
+	b = Adventurer.randomize(28)
90 116
 
91 117
 	while const.KNOCKOUT not in {a.state, b.state}:
92 118
 		a.attack(b)
... ...
@@ -8,7 +8,8 @@ class Race(object):
8 8
 
9 9
 	@classmethod
10 10
 	def random_race(cls):
11
-		return random.choice(self.registry.values())
11
+		return random.choice(cls.registry.keys())
12
+
12 13
 	@property
13 14
 	def name(self):
14 15
 		return self.__class__.__name__.lower()
... ...
@@ -34,13 +35,13 @@ class Elf(Race):
34 35
 @Race.register
35 36
 class Dwarf(Race):
36 37
 	allowed_professions = {'fighter', 'Priest', 'thief'}
37
-	def mod(self, atr):
38
+	def mod(self, attr):
38 39
 		attr.str += 1
39 40
 
40 41
 @Race.register
41 42
 class Hobbit(Race):
42 43
 	allowed_professions = {'thief', 'barbarian'}
43
-	def mod(self, atr):
44
+	def mod(self, attr):
44 45
 		attr.dex += 1
45 46
 
46 47
 
... ...
@@ -25,8 +25,13 @@ class Skills(object):
25 25
 		self.training = Trainer(self)
26 26
 		self.train = self.training.select
27 27
 
28
-	def check(self, skill, dieroll):
29
-		dieroll.dice = dice.DieJar().d20
28
+	def check(self, skill, dieroll=None):
29
+		die = dice.DieJar().d20
30
+		if dieroll is None:
31
+			dieroll = die
32
+		else:
33
+			dieroll.dice = dice.DieJar().d20
34
+
30 35
 		roll = dieroll.roll()
31 36
 		if roll == 1:
32 37
 			result = False
... ...
@@ -1,7 +1,8 @@
1 1
 import abc
2
+from . import combat
2 3
 
3 4
 class Overlay(object):
4
-	__metaclass__ = abs.ABCMeta
5
+	__metaclass__ = abc.ABCMeta
5 6
 
6 7
 	char = abc.abstractproperty()
7 8
 	color = (0,0,0)
... ...
@@ -29,6 +30,9 @@ class Actor(Overlay):
29 30
 		self.x = x
30 31
 		self.y = y
31 32
 		self.map = map
33
+		if adventurer is None:
34
+			adventurer = combat.Adventurer.randomize()
35
+		self.adventurer = adventurer
32 36
 
33 37
 	def draw(self):
34 38
 		self.map.add(self)
... ...
@@ -42,9 +46,22 @@ class Actor(Overlay):
42 46
 		pass
43 47
 
44 48
 
49
+	def ishostile(self, other):
50
+		return True #TODO: implement factions
51
+
45 52
 	def bump(self, other):
46 53
 		print '%s bumped %s' % (type(self).__name__, type(other).__name__)
54
+		if isinstance(other, Actor) and other.ishostile(self):
55
+			self.adventurer.attack(other.adventurer)
56
+			other.attacked_by(self)
47 57
 		other.bumped_by(self)
58
+
59
+	def attacked_by(self, other):
60
+		if self.adventurer.state >= 2:
61
+			self.char = '%'
62
+		elif self.adventurer.skills.check('agility'):
63
+			self.adventurer.attack(other.adventurer)
64
+
48 65
 	def bumped_by(self, other):
49 66
 		print '%s was bumped by %s' % (type(self).__name__, type(other).__name__)
50 67
 
... ...
@@ -5,9 +5,10 @@ random.seed(2)
5 5
 import libtcodpy as tc
6 6
 import numpy as np
7 7
 
8
+from libs import overlay
9
+from libs import combat
8 10
 from src import events
9 11
 from src import player
10
-from libs import actor
11 12
 from src import console
12 13
 from src import map
13 14
 
... ...
@@ -20,17 +21,17 @@ class Application(object):
20 21
 		self.terrain_registry = map.TerrainRegistry()
21 22
 		self.terrain_registry.load_from_file('data/terrain.yml')
22 23
 		self.map = map.Map(100,63, self.terrain_registry)
23
-		self.player = player.Player(4,5, self.map)
24
+		self.player = player.Player(4,5, self.map, combat.Adventurer.randomize())
24 25
 		self.events = events.EventHandler()
25 26
 		player.ArrowHandler(self.player, self.events)
26 27
 
27 28
 		self.actors = []
28 29
 		for x in range(40):
29
-			self.actors.append(actor.Actor(random.randrange(100), random.randrange(63), self.map))
30
+			self.actors.append(overlay.Actor(random.randrange(100), random.randrange(63), self.map))
30 31
 
31 32
 		self.objects = []
32 33
 		for x in range(50):
33
-			self.objects.append(
34
+			self.objects.append(overlay.Potion(random.randrange(100), random.randrange(63), self.map))
34 35
 
35 36
 		tc.sys_set_fps(60)
36 37
 
... ...
@@ -1,16 +1,16 @@
1 1
 import libtcodpy as tc
2 2
 import libs.combat
3
-import libs.actor
3
+import libs.overlay
4 4
 
5
-class Player(libs.actor.Actor):
5
+class Player(libs.overlay.Actor):
6 6
 	char = ord('@')
7 7
 	color = (255,255,255)
8 8
 	light_radius = 10
9
-	def __init__(self, x,y, map):
10
-		libs.actor.Actor.__init__(self, x,y, map)
9
+	def __init__(self, x,y, map, adventurer):
10
+		libs.overlay.Actor.__init__(self, x,y, map, adventurer)
11 11
 		self.map.set_pov((self.pos, self.light_radius))
12 12
 	def move(self, dx, dy):
13
-		libs.actor.Actor.move(self, dx,dy)
13
+		libs.overlay.Actor.move(self, dx,dy)
14 14
 		self.map.set_pov((self.pos, self.light_radius))
15 15
 
16 16
 class ArrowHandler(object):