Static C++ Plugins
Listing Three is the initialization code of the static plugin. It is also a C++ plugin, but it is initialized differently. All dynamic plugins (both C and C++) must implement the well-known entry point function PF_initPlugin. This is what the PluginManager is looking to initialize. Static plugins, on the other hand, are statically linked to the application. That means that if two plugins implement the same function name, a name clash occurs and the application fails to link. So static plugins must have a unique initialization function and the main application must know about it to initialize it at the beginning of the program. Once the initialization function has been called, static plugins are indistinguishable from any other plugin. The last line defines a Plugin Register instance. This non-portable technique works on non-Windows platforms and allows static plugins to register themselves. This is nice and saves the application code from tight coupling with static plugins (other than linking). It makes it easy to add new static plugins just by changing the build system and without recompiling existing code.
Static plugin objects look just dynamic plugin objects and implement the IActor interface. Example 3 contains the play() method of the FidgetyPhantom class that the static plugin registers with the PluginManager. The FidgetyPhantom is actually doing something in its play() method. It is a good example of a plugin object using objects created and managed by the application via the object model interfaces. FidgetyPhantom gets the first foe (usually the Hero), moves towards him, and attacks, if possible. It is uses the findClosest() utility function to find the closest point (restricted by its movement points) to its foe and moves towards this point. If it reaches its enemy, it attacks.
void FidgetyPhantom::play( ITurn * turnInfo) { // Get self const ActorInfo * self = turnInfo->getSelfInfo(); // Get first foe IActorInfoIterator * foes = turnInfo->getFoes(); ActorInfo * foe = foes->next(); // Move towards and attack the first foe (usually the hero) Position p1(self->location_x, self->location_y); Position p2(foe->location_x, foe->location_y); Position closest = findClosest(p1, p2, self->movement); turnInfo->move(closest.first, closest.second); if (closest == p2) turnInfo->attack(foe->id); }