• Sign Up
• Login
• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
55%
SHARE
TWEET

# Kruno

a guest Jan 4th, 2013 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import scala.math._
2. import scala.util._
3.
4. import scala.language.postfixOps
5.
6.
7.
8. /////////////////////////////////////////Classes
9. implicit class Point(p: (Int, Int)) {
10.     def +(o: (Int, Int)) = (o._1 + p._1, o._2 + p._2)
11.     def -(o: (Int, Int)) = (o._1 - p._1, o._2 - p._2)
12.     def *(o: (Int, Int)) = (o._1 * p._1, o._2 * p._2)
13.     def /(o: (Int, Int)) = (o._1 / p._1, o._2 / p._2)
14.     def >(o: (Int, Int)) = (o._1 < p._1 && o._2 < p._2)
15.     def <(o: (Int, Int)) = (o._1 > p._1 && o._2 > p._2)
16.     def ==(o: (Int, Int)) = (o._1 == p._1 && o._2 == p._2)
17.     def length = sqrt(p._1 * p._1 + p._2 * p._2)
18.     def scale(s: Double): (Int, Int) = (p._1 * s toInt, p._2 * s toInt)
19. }
20.
21.
22. object Math {
23.
24.     def inside(x: (Int, Int), y: (Int, Int), p: (Int, Int)) = {
25.         (x < p) && (y > p)
26.     }
27.
28. }
29.
30.
31.
32. ///////////////////////////////////////Util
33.
34. def rand(o: Int, e: Int) = o + Random.nextInt(e)
35. def randPoint(width: Int, height: Int, p0: (Int, Int)=(0, 0)) = {
36.     (rand(p0._1, width), rand(p0._2, height))
37. }
38.
39.
40.
41. ///////////////////////////////////////Entity
42.
43.
44. trait EntityStats {
45.     var curHp = 10
46.     var maxHp = 10
47.     var defense = 1
48.     var offense = 2
49.
50.     def hp = curHp
51.     def hpRatio = curHp / maxHp
52.
53.     def reduceHp(amt: Int) = {
54.         val hpleft = (-amt + defense) + curHp
55.         curHp = max(min(hpleft, maxHp), 0)
56.     }
57. }
58.
59.
60. class Positional(xc: Int, yc: Int) {
61.     var x = xc
62.     var y = yc
63.
64.     def location = (x, y)
65.
66.     def move(dx: Int, dy: Int): Unit = {
67.         x += dx
68.         y += dy
69.     }
70. }
71.
72.
73. class Entity(
74.         x: Int,
75.         y: Int,
76.         reprc: Char)
77.         extends Positional(x, y)
78.         with EntityStats {
79.     def repr = reprc
80.     def attack(other: Entity): Unit = other.reduceHp(offense)
81.
82.     override def toString = s"\$reprc => (\$x, \$y)"
83. }
84.
85.
86.
87.
88. /////////////////////////////////////////Controllers
89. abstract class Message
90. case class Attack(me: Entity, enemy: Entity) extends Message
91.
92.
93. trait AI {
94.     var messages = List[Message]()
95.
96.     def see(entities: Seq[Entity]): AI
97.     def act: Seq[Message]
98. }
99.
100. class DumbAI extends Entity(0, 0, 'z') with AI {
101.     var target: Entity = null
102.
103.
104.     def see(entities: Seq[Entity]) = {
105.         val radius = 5
106.         val fn = (e: Entity) => {(e.location - location).length <= radius}
107.         val validEnts = entities filter fn
108.         if (validEnts.length > 0)
109.             target = validEnts(0)
110.         this
111.     }
112.
113.     def act = {
114.         var msgs = List[Message]()
115.         if (target != null)
116.             msgs ::= new Attack(this, target)
117.         msgs
118.     }
119. }
120.
121.
122. val zombie = new DumbAI
123. val others = List(
124.     new Entity(3, 3, '@'),
125.     new Entity(10, 3, 'c'),
126.     new Entity(1, 3, 'l'))
127. println(zombie.see(others).act)
128.
129.
130.
131.
132. /////////////////////////////////////////Action Processor
133.
134.
135.
136. /////////////////////////////////////////Map gen
137.
138.
139. def genEntities(maxx: Int, maxy: Int) = {
140.     def make(c: Char) = new Entity(rand(0, maxx), rand(0, maxy), c)
141.     make('@') :: (1 to 12 map {_ => make('z')} toList)
142. }
143.
144.
145.
146.
147.
148.
149. /////////////////////////////////////////Display
150.
151. object Display {
152.     def width = 25
153.     def height = 80
154.
155.     def dimensions = (width, height)
156.
157.     def draw(es: Seq[Entity]) = {
158.         for (x <- 0 to width) {
159.             for (y <- 0 to height) {
160.                 val pt = (x, y)
161.                 val c = es dropWhile { _.location != pt }
162.
163.                 if (c == Nil) print(' ')
164.                 else print(c(0).repr)
165.             }
166.             println(' ')
167.         }
168.     }
169. }
170.
171.
172.
173. /////////////////////////////////////////Main
174. val entities = genEntities(20, 20)
175. val pc = (entities takeWhile {_.repr == '@'})(0)
176. Display.draw(entities)
177.
178.
179. /////////////////////////////////////////Tests
180.
181.
182. def testAttack = {
183.     val p1 = new Entity(0, 0, '@')
184.     val p2 = new Entity(0, 0, '@')
185.     (1 to 12) foreach {_ => p1.attack(p2)}
186.     assert( p1.hp == 0 )
187.     p2.offense = -2
188.     (1 to 12) foreach {_ => p2.attack(p1)}
189.     assert( p1.hp == p1.maxHp )
190. }
191.
192.
193. def testMath = {
194.     val a = (0, 0)
195.     val b = (10, 10)
196.     val p = (5, 5)
197.     assert( Math inside (a, b, p) )
198. }
199.
200.
201. def testPoint = {
202.     val a = (0, 0)
203.     val b = (1, 1)
204.     assert( b - a == (-1, -1) )
205.     assert( b.length - 1.414 <= 1e-3 )
206.     assert( ((10, 10) scale 0.3) == (3, 3) )
207.     assert( ((10, 10) scale 3) == (30, 30) )
208.     assert( (5, 5) > (3, 3) )
209. }
RAW Paste Data
Top