Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
asteroids
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hexboard-games
asteroids
Commits
fed01471
Commit
fed01471
authored
9 years ago
by
Daniel Berio
Browse files
Options
Downloads
Patches
Plain Diff
...
parent
148e0411
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/battle/DaniBattleTest.java
+1
-1
1 addition, 1 deletion
src/battle/DaniBattleTest.java
src/battle/controllers/DaniController.java
+86
-28
86 additions, 28 deletions
src/battle/controllers/DaniController.java
with
87 additions
and
29 deletions
src/battle/DaniBattleTest.java
+
1
−
1
View file @
fed01471
...
...
@@ -21,7 +21,7 @@ public class DaniBattleTest {
SimpleBattle
battle
=
new
SimpleBattle
();
BattleController
fire1
=
new
DaniController
();
BattleController
fire2
=
new
DaniController
();
BattleController
fire2
=
new
RotateAndShoot
();
battle
.
playGame
(
fire1
,
fire2
);
}
...
...
This diff is collapsed.
Click to expand it.
src/battle/controllers/DaniController.java
+
86
−
28
View file @
fed01471
...
...
@@ -28,8 +28,13 @@ public class DaniController implements RenderableBattleController
double
thrustAmt
=
1.5
;
double
rotAmt
=
1.5
;
int
shotWait
=
0
;
Vector2d
posSum
;
Vector2d
dirSum
;
Vector2d
meanMissilePos
;
Vector2d
meanMissileDir
;
Vector2d
missileLineA
;
Vector2d
missileLineB
;
Vector2d
segp
;
boolean
anyMissiles
=
false
;
Vector2d
targetPosition
;
...
...
@@ -122,48 +127,92 @@ public class DaniController implements RenderableBattleController
return
p
.
dist
(
closestPointOnSegment
(
p
,
a
,
b
)
);
}
Vector2d
getPerp
(
Vector2d
v
)
{
return
new
Vector2d
(-
v
.
y
,
v
.
x
);
}
@Override
public
Action
getAction
(
SimpleBattle
gstate
,
int
playerId
)
Vector2d
keepDistanceVector
(
SimpleBattle
gstate
,
Vector2d
shipPos
,
Vector2d
enemyPos
)
{
Action
res
=
new
Action
(
0
,
0
,
false
);
NeuroShip
ship
=
gstate
.
getShip
(
playerId
);
NeuroShip
enemy
=
gstate
.
getShip
((
playerId
==
1
)?
0
:
1
);
if
(
shipPos
.
dist
(
enemyPos
)
<
200
)
return
Vector2d
.
multiply
(
Vector2d
.
subtract
(
shipPos
,
enemyPos
),
0.3
);
Vector2d
enemyPos
=
enemy
.
s
;
Vector2d
thisPos
=
ship
.
s
;
Vector2d
d
=
new
Vector2d
(
enemyPos
.
x
-
thisPos
.
x
,
enemyPos
.
y
-
thisPos
.
y
);
double
rot
=
angleBetween
(
ship
.
d
,
d
)*
rotAmt
;
return
new
Vector2d
(
0
,
0
);
}
Vector2d
avoidBulletsVector
(
SimpleBattle
gstate
,
Vector2d
shipPos
)
{
ArrayList
<
Missile
>
M
=
getMissiles
(
gstate
);
posSum
=
new
Vector2d
(
0
,
0
,
true
);
dirSum
=
new
Vector2d
(
0
,
0
,
true
);
meanMissilePos
=
new
Vector2d
(
0
,
0
,
true
);
meanMissileDir
=
new
Vector2d
(
0
,
0
,
true
);
int
c
=
0
;
for
(
Missile
m
:
M
)
{
posSum
.
add
(
m
.
s
);
dirSum
.
add
(
m
.
v
);
meanMissilePos
.
add
(
m
.
s
);
meanMissileDir
.
add
(
m
.
v
);
c
++;
}
if
(
c
!=
0
)
{
//System.out.println("Missiles: " + c);
dirSum
.
normalise
();
posSum
.
multiply
(
1.0
/
c
);
anyMissiles
=
true
;
}
else
// no missiles bail out
if
(
c
==
0
)
{
anyMissiles
=
false
;
System
.
out
.
println
(
"No missiles"
);
return
new
Vector2d
(
0
,
0
,
true
);
}
meanMissilePos
.
divide
(
c
);
meanMissileDir
.
normalise
();
// project onto main missile line
double
min
=
1000000
;
double
max
=
-
1000000
;
for
(
Missile
m
:
M
)
{
Vector2d
p
=
new
Vector2d
(
m
.
s
,
true
);
p
.
subtract
(
meanMissilePos
);
double
d
=
dot
(
p
,
meanMissileDir
);
if
(
d
<
min
)
min
=
d
;
if
(
d
>
max
)
max
=
d
;
}
missileLineA
=
Vector2d
.
add
(
meanMissilePos
,
Vector2d
.
multiply
(
meanMissileDir
,
min
)
);
missileLineB
=
Vector2d
.
add
(
meanMissilePos
,
Vector2d
.
multiply
(
meanMissileDir
,
max
)
);
anyMissiles
=
true
;
segp
=
closestPointOnSegment
(
shipPos
,
missileLineA
,
missileLineB
);
if
(
segp
.
dist
(
shipPos
)
<
100
)
{
Vector2d
vavoid
=
Vector2d
.
subtract
(
shipPos
,
segp
);
return
Vector2d
.
multiply
(
vavoid
,
1
)
;
}
return
new
Vector2d
(
0
,
0
,
true
);
}
@Override
public
Action
getAction
(
SimpleBattle
gstate
,
int
playerId
)
{
Action
res
=
new
Action
(
0
,
0
,
false
);
NeuroShip
ship
=
gstate
.
getShip
(
playerId
);
NeuroShip
enemy
=
gstate
.
getShip
((
playerId
==
1
)?
0
:
1
);
Vector2d
enemyPos
=
enemy
.
s
;
Vector2d
thisPos
=
ship
.
s
;
// Direction towards enemy
Vector2d
d
=
new
Vector2d
(
enemyPos
.
x
-
thisPos
.
x
,
enemyPos
.
y
-
thisPos
.
y
,
true
);
Vector2d
vavoid
=
avoidBulletsVector
(
gstate
,
thisPos
);
d
.
add
(
vavoid
);
d
.
add
(
keepDistanceVector
(
gstate
,
thisPos
,
enemyPos
)
);
double
rot
=
angleBetween
(
ship
.
d
,
d
)*
rotAmt
;
if
(
inView
(
ship
,
enemy
)
&&
shotWait
<=
0
)
{
res
.
shoot
=
true
;
...
...
@@ -185,6 +234,11 @@ public class DaniController implements RenderableBattleController
g
.
drawOval
((
int
)(
center
.
x
-
r
),
(
int
)(
center
.
y
-
r
),
(
int
)(
r
*
2
),
(
int
)(
r
*
2
));
}
public
void
drawLine
(
Graphics2D
g
,
Vector2d
a
,
Vector2d
b
)
{
g
.
drawLine
((
int
)
a
.
x
,
(
int
)
a
.
y
,
(
int
)
b
.
x
,
(
int
)
b
.
y
);
}
@Override
public
void
render
(
Graphics2D
g
,
NeuroShip
s
)
{
...
...
@@ -195,10 +249,14 @@ public class DaniController implements RenderableBattleController
if
(
anyMissiles
)
{
drawCircle
(
g
,
posSum
,
4.0
);
int
dx
=
(
int
)(
dirSum
.
x
*
100
);
int
dy
=
(
int
)(
dirSum
.
y
*
100
);
g
.
drawLine
((
int
)
posSum
.
x
-
dx
,
(
int
)
posSum
.
y
-
dy
,
(
int
)
posSum
.
x
+
dx
,
(
int
)
posSum
.
y
+
dy
);
drawCircle
(
g
,
meanMissilePos
,
4.0
);
// int dx = (int)(meanMissileDir.x * 100);
// int dy = (int)(meanMissileDir.y * 100);
drawLine
(
g
,
Vector2d
.
subtract
(
meanMissilePos
,
Vector2d
.
multiply
(
meanMissileDir
,
100
)),
Vector2d
.
add
(
meanMissilePos
,
Vector2d
.
multiply
(
meanMissileDir
,
100
))
);
// g.drawLine((int)meanMissilePos.x - dx, (int)meanMissilePos.y - dy, (int)meanMissilePos.x + dx, (int)meanMissilePos.y + dy);
drawCircle
(
g
,
missileLineA
,
7.0
);
drawCircle
(
g
,
missileLineB
,
7.0
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment