File: Static_Move.cpp

package info (click to toggle)
atlas-cpp 0.5.98-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,652 kB
  • ctags: 2,284
  • sloc: sh: 8,305; cpp: 6,372; python: 1,471; makefile: 216
file content (149 lines) | stat: -rw-r--r-- 2,713 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "timer.h"

#include <iostream>
#include <cassert>

using namespace std;

class Object {
public:
  int id;
  Object *parent;
  int objtype;
};

class Entity : public Object {
public:
  Object *loc;
  double pos[3];
  double velocity[3];
};

class Operation : public Object {
public:
  Object *from, *to;
  Object* arg;
};

enum {
  OBJECT,
  OP
};

Entity human;
Operation move;
Operation sight;

class NPC
{
public:
  NPC() : id(123) {x=y=z = vx=vy=vz = 0.0;}
  Operation *move(const Operation *op);
  int getId() {return id;}
private:
  int id;
  double x,y,z;
  double vx,vy,vz;
};

Operation *NPC::move(const Operation *op)
{
  double *new_vel = ((Entity*)op->arg)->velocity;
  vx = new_vel[0];
  vy = new_vel[1];
  vz = new_vel[2];

  x += vx;
  y += vy;
  z += vz;

  //human:
  static Entity human_ent;
  human_ent.parent = &human;
  human_ent.id = getId();
  human_ent.pos[0] = x;
  human_ent.pos[1] = y;
  human_ent.pos[2] = z;
  human_ent.velocity[0] = vx;
  human_ent.velocity[1] = vy;
  human_ent.velocity[2] = vz;
  
  //move:
  static Operation move_op;
  move_op.objtype = OP;
  move_op.parent = &::move;
  move_op.arg = &human_ent;
  
  //sight:
  static Operation sight_op;
  sight_op.objtype = OP;
  sight_op.parent = &sight;
  sight_op.from = &human_ent;
  sight_op.arg = &move_op;

  return &sight_op;
}

int main(int argc, char** argv)
{
  double i;
  TIME_ON;
  for(i=0; i<10000000.0; i+=1.0) {
    //human:
    Entity ent;
    ent.objtype=OBJECT;
    ent.parent=&human;
    ent.pos[0] = i;
    ent.pos[1] = i-1.0;
    ent.pos[2] = i+1.0;
    ent.velocity[0] = i;
    ent.velocity[1] = i-1.0;
    ent.velocity[2] = i+1.0;

    //move:
    Operation move_op;
    move_op.objtype=OP;
    move_op.parent=&move;
    move_op.arg=&ent;
    
    //sight:
    Operation sight_op;
    sight_op.objtype=OP;
    sight_op.parent=&sight;
    sight_op.from=&ent;
    sight_op.arg=&move_op;
  }
  TIME_OFF("Plain creating of sight operation");


  NPC npc1;
  double x,y,z;
  TIME_ON;
  for(i=0; i<10000000.0; i+=1.0) {
    //human:
    Entity human_ent;
    human_ent.parent = &human;
    human_ent.id = npc1.getId();
    human_ent.velocity[0] = i;
    human_ent.velocity[1] = i-1.0;
    human_ent.velocity[2] = i+1.0;

    //move:
    Operation move_op;
    move_op.objtype = OP;
    move_op.parent = &move;
    move_op.arg = &human_ent;

    Operation *res_sight = npc1.move(&move_op);
    Operation *res_move = (Operation*)(res_sight->arg);
    Entity *res_ent = (Entity*)res_move->arg;
    double *new_pos = res_ent->pos;
    x = new_pos[0];
    y = new_pos[1];
    z = new_pos[2];
  }
  TIME_OFF("NPC movements");
  cout<<"Resulting position: ("<<x<<","<<y<<","<<z<<")"<<endl;

  return 0;
}