File: server_world.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (97 lines) | stat: -rw-r--r-- 2,577 bytes parent folder | download
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
#ifdef WIN32
#pragma warning (disable:4786)
#endif

#include <ClanLib/core.h>

#include "netobjects.h"
#include "server_world.h"
#include "fighter.h"
#include "player.h"
#include "players.h"
#include "gameobject.h"
#include "Lobby/lobby_players.h"
#include "Lobby/lobby_player.h"

/////////////////////////////////////////////////////////////////////////////
// ServerWorld construction:

ServerWorld::ServerWorld(CL_NetSession *session, const LobbyPlayers &lobby_players)
:
	World(session),
	max_tick_elapsed(0.05f)
{
	CL_NetGroup all_players;

	// Create player structures for all initial players:
	int player_count = 0;
	LobbyPlayers::const_iterator it;
	for (it = lobby_players.begin(); it != lobby_players.end(); it++)
	{
		LobbyPlayer *const lobby_player = it->second;
		Player *player = new Player(lobby_player);
		player->ID = player_count++;
		players.insert(std::pair<CL_NetComputer, Player *>(player->computer, player));

		all_players.get_computers().push_back(player->computer);

		session->set_access(World::NETCHANNEL_WORLD, player->computer, ACCESS_CHANNEL_READ|ACCESS_CHANNEL_WRITE);
	}

	// Replicate player structures to all computers:
	players.replicate(session, all_players);

	// Create each players fighter object on server:
	Players::iterator player_it;
	for (player_it = players.begin(); player_it != players.end(); player_it++)
	{
		Player *player = player_it->second;
		Fighter *fighter = new Fighter(this, player);
		add_object(fighter);
	}

	// Send initial replication information:
	std::list<GameObject *> &objects = get_objects();
	std::list<GameObject *>::iterator objects_it;
	for (objects_it = objects.begin(); objects_it != objects.end(); objects_it++)
	{
		GameObject *obj = *objects_it;
		obj->send_full(NULL/*all_players*/);
	}
}

ServerWorld::~ServerWorld()
{
}

/////////////////////////////////////////////////////////////////////////////
// ServerWorld attributes:

/////////////////////////////////////////////////////////////////////////////
// ServerWorld operations:

void ServerWorld::dedicated()
{
	float last_time = CL_System::get_time();

	while (true) // todo: find a better break rule :)
	{
		float delta_time = (CL_System::get_time() - last_time);

		for (; delta_time > 0; delta_time -= max_tick_elapsed)
		{
			update(delta_time);
		}

		CL_System::keep_alive();
		CL_System::sleep(10); // todo: find a better sleeping technique.
	}
}

void ServerWorld::update(float delta_time)
{
	World::update(delta_time);
}

/////////////////////////////////////////////////////////////////////////////
// ServerWorld implementation: